# PE刷题记

PE

### 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;
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;
}
/*
*/
T3

### 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;
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;
}
/*
*/
T4

### 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);
}
T5

### 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);
}
6

### 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]);
}
7

2018-07-21 07:37:01

### 8.Largest product in a series

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);
}
8

### 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);
}
}
}
9

### 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]);
}
10

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\n", ans);
}

}
11

2018-07-21 08:17:13

### 12.Highly divisible triangular number

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;
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\n", sum[i]);

}
return 0;
}
12

### 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;
}
14

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;
}
15

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;
}
16

### 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\n", i, x, y);
if(x <= N && y == i && i != x) ans += i + x, vis[i] = 1, vis[x] = 1;
}
printf("%d", ans);
return 0;
}
21

### 50.Consecutive prime sum

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);
}
50

posted @ 2018-07-20 21:49  自为风月马前卒  阅读(212)  评论(0编辑  收藏  举报