最喜欢做这种很有意思的数学题了虽然数学很垃圾
但是这个网站的提交方式好鬼畜啊qwq
1.Multiples of 3 and 5
直接枚举
2.Even Fibonacci numbers
直接枚举
3.Largest prime factor
$sqrt(n)$枚举
#include<cstdio> #include<vector> #include<set> #include<algorithm> #define sit #define LL long long using namespace std; const int MAXN = 1e6 + 10; inline int read() { char c = getchar(); LL x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } LL ans = 600851475143; int main() { LL out = 0; for(LL i = 2; i * i <= ans; i++) { if(ans % i == 0) { out = max(out, i); while(ans % i == 0) ans /= i; } } printf("%I64d", max(out, ans)); return 0; } /* */
4.Largest palindrome product
暴力枚举
#include<cstdio> #include<vector> #include<set> #include<algorithm> #define sit #define LL long long using namespace std; const int MAXN = 1e6 + 10; inline int read() { char c = getchar(); LL x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int ans; int main() { int ans = 0; for(int i = 100; i <= 999; i++) { for(int j = 100; j <= 999; j++) { int x = i * j; int a[20], tot = 0, flag = 0; while(x) a[++tot] = x % 10, x /= 10; for(int k = 1; k <= tot; k++) if(a[k] != a[tot - k + 1]) {flag = 1; break;} if(flag == 0) ans = max(ans, i * j); } } printf("%d", ans); return 0; } /* */
5.Smallest multiple
这个就比较有意思了。
首先我们吧所有的数质因数分解,取每个质数的最大指数,乘起来
update:woc?好像求个最小公倍数就行了??
#include<cstdio> #include<algorithm> using namespace std; int mx[21], prime[21] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; void getmax(int x) { for(int i = 0; prime[i] <= x && i <= 20; i++) { int cur = 0; if(x % prime[i] == 0) while(x % prime[i] == 0) cur++, x /= prime[i]; mx[i] = max(mx[i], cur); } } int main() { int N = 20; for(int i = 1; i <= N; i++) getmax(i); int ans = 1; for(int i = 0; i <= N; i++) for(int j = 1; j <= mx[i]; j++) ans = ans * prime[i]; printf("%d", ans); }
6.Sum square difference
直接枚举
#include<cstdio> #include<algorithm> #define LL long long using namespace std; int N = 100; int main() { LL ans = 5050 * 5050; for(int i = 1; i <= N; i++) ans = ans - i * i; printf("%d", ans); }
7.10001st prime
直接一波线性筛
#include<cstdio> #include<algorithm> #define LL long long using namespace std; const int MAXN = 1e6 + 10; int N = 1e6; int prime[MAXN], vis[MAXN], tot = 0; int main() { for(int i = 2; i <= N; i++) { if(!vis[i]) prime[++tot] = i; for(int j = 1; j <= tot && i * prime[j] <= N; j++) { vis[i * prime[j]] = 1; if(!(i % prime[j])) break; } } printf("%d", prime[10001]); }
2018-07-21 07:37:01
8.Largest product in a series
这题目真鬼畜,首先把所有的数copy到一个txt里
然后暴力枚举就行了
2018-07-21 08:01:17
#include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; int N; char s[1050]; int main() { freopen("a.in", "r", stdin); scanf("%s", s + 1); int N = strlen(s + 1); LL ans = 0; for(int i = 1; i <= N; i++) { LL now = 1; for(int j = i; j <= i + 12 && j <= N; j++) { LL x = s[j] - '0'; now = now * x; } ans = max(ans, now); } printf("%I64d", ans); }
9.Special Pythagorean triplet
大力枚举
#include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; int main() { for(int i = 1; i <= 1000; i++) for(int j = 1; j <= 1000; j++) for(int k = 1; k <= 1000; k++) { if((i * i + j * j == k * k) && (i + j + k == 1000)) { printf("%d %d %d", i * j * k); } } }
10.Summation of primes
大力枚举、、
#include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; const int MAXN = 1e7 + 10; int N = 2000001; LL prime[MAXN], vis[MAXN], tot; int main() { for(int i = 2; i <= N; i++) { if(!vis[i]) prime[++tot] = i; for(int j = 1; j <= tot && i * prime[j] <= N; j++) { vis[i * prime[j]] = 1; if(!(i % prime[j])) break; } } for(int i = 1; i <= tot; i++) prime[i] += prime[i - 1]; printf("%I64d", prime[tot]); }
2018-07-21 08:08:50
11.Largest product in a grid
直接枚举,一个小优化:考虑到枚举的对称性,每个点都往同一个方向枚举即可
#include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; const int MAXN = 1e7 + 10; int N = 20; LL a[28][28]; int main() { LL ans = 0; for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) scanf("%I64d", &a[i][j]); for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) { ans = max(ans, a[i][j] * a[i + 1][j] * a[i + 2][j] * a[i + 3][j]); ans = max(ans, a[i][j] * a[i + 1][j + 1] * a[i + 2][j + 2] * a[i + 3][j + 3]); ans = max(ans, a[i][j] * a[i + 1][j - 1] * a[i + 2][j - 2] * a[i + 3][j - 3]); printf("%I64d ", ans); } }
2018-07-21 08:17:13
12.Highly divisible triangular number
不会做,然后暴力枚举了一发过了qwq。
2018-07-21 09:15:32
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #define LL long long #define int long long using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int a[MAXN], sum[MAXN]; int prime[MAXN], vis[MAXN], tot = 0; void GetPrime(int N) { vis[1] = 1; prime[0] = 1; for(int i = 2; i <= N; i++) { if(!vis[i]) prime[++tot] = i; for(int j = 1; j <= tot && prime[j] * i <= N; j++) { vis[i * prime[j]] = 1; if(!i % prime[j]) break; } } } int pd(int val) { int ans = 1; for(int i = 1; prime[i] <= val; i++) { if(val % prime[i] == 0) { int now = 0; while(val % prime[i] == 0) now++, val /= prime[i]; ans = ans * (now + 1); } } return ans + (val != 0); } main() { #ifdef WIN32 //freopen("a.in", "r", stdin); #endif GetPrime(1e6 + 10); a[1] = 1; for(int i = 2; i <= 100000; i++) { a[i] = a[i - 1] + i; sum[i] = pd(a[i]); if(sum[i] >= 500) {printf("%I64d", a[i]); return 0;} //printf("%d ", sum[i]); } return 0; }
13.Large sum
直接上高精
2018-07-21 09:56:14
14.Longest Collatz sequence
直接一波记忆化搜索
#pragma comment(linker,"/STACK:1024000000,1024000000") #include<cstdio> #include<map> #define LL long long using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; LL N = 1e6; map<LL, LL> val; int dfs(LL i) { if(val[i]) return val[i]; if(i & 1) val[i] = dfs(3 * i + 1) + 1; else val[i] = dfs(i / 2) + 1; return val[i]; } main() { //freopen("a.in", "r", stdin); val[1] = 1; int ans = 1, out = 1; for(int i = 2; i <= N; i++) { int now = dfs(i); if(now > ans) ans = now, out = i; } printf("%d", out); return 0; }
2018-07-21 10:07:51
15.Lattice paths
再一波记忆化搜索。。
#pragma comment(linker,"/STACK:1024000000,1024000000") #include<cstdio> #include<map> #define LL long long using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; LL N = 1e6; LL ans[21][21]; LL dfs(int x, int y) { if(ans[x][y]) return ans[x][y]; if(x - 1 >= 0) ans[x][y] += dfs(x - 1, y); if(y - 1 >= 0) ans[x][y] += dfs(x, y - 1); return ans[x][y]; } main() { //freopen("a.in", "r", stdin); ans[0][0] = 1; printf("%I64d", dfs(20, 20)); return 0; }
2018-07-21 10:14:35
16.Power digit sum
高精度
#pragma comment(linker,"/STACK:1024000000,1024000000") #include<cstdio> #include<map> #define LL long long using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; LL N = 1e6; LL ans[21][21]; LL dfs(int x, int y) { if(ans[x][y]) return ans[x][y]; if(x - 1 >= 0) ans[x][y] += dfs(x - 1, y); if(y - 1 >= 0) ans[x][y] += dfs(x, y - 1); return ans[x][y]; } main() { //freopen("a.in", "r", stdin); ans[0][0] = 1; printf("%I64d", dfs(20, 20)); return 0; }
21.Amicable numbers
暴力。。
#pragma comment(linker,"/STACK:1024000000,1024000000") #include<cstdio> #include<map> #define LL long long using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 10; int vis[MAXN]; int get(int x) { int ans = 0; for(int i = 1; i < x; i++) if(x % i == 0) ans += i; return ans; } main() { int ans = 0, N = 10000; for(int i = 1; i <= N; i++) { if(vis[i]) continue; int x = get(i), y = get(x); //printf("%d %d %d ", i, x, y); if(x <= N && y == i && i != x) ans += i + x, vis[i] = 1, vis[x] = 1; } printf("%d", ans); return 0; }
50.Consecutive prime sum
开始以为有单调性,也就是如果长度为$x$的能构成素数,那$x - 1$一定能构成素数
但是这样枚举是错的qwq。比如$18$不是素数,但是$29$是素数
然后把二分改成枚举就过了。。
2018-07-21 09:15:19
#include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; const LL MAXN = 1e6 + 10; LL N = 1000000; LL prime[MAXN], vis[MAXN], tot = 0; LL ans = 0; LL pd(LL num) { if(num==2||num==3) return 1; if(num%6!=1&&num%6!=5) return 0; for(register LL i=5;i*i<=num;i+=6) if(num%i==0||num%(i+2)==0) return 0; return 1; } int len = 0; bool check(LL num) { for(LL i = 1; i <= tot; i++) { LL r = prime[i + num - 1], l = prime[i - 1]; if(r - l > N) return 0; if(pd(r - l) == 1 && (i + num - 1 <= tot) && (r - l <= N) && num > len) { ans = r - l; len = num; return 1; } } return 0; } int main() { for(LL i = 2; i <= N; i++) { if(!vis[i]) prime[++tot] = i; for(LL j = 1; j <= tot && i * prime[j] <= N; j++) { vis[i * prime[j]] = 1; if(!(i % prime[j])) break; } } for(LL i = 1; i <= tot; i++) prime[i] += prime[i - 1]; for(int i = 1; i <= tot; i++) check(i); printf("%I64d ", ans); }