Eddy’s research II
找规律
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int m, n;
LL solve(int m, int n) {
if (m == 1)
return n + 2;
if (m == 2)
return 2 * n + 3;
if (m == 3 && n == 0)
return 5;
if (m == 3 && n)
return 2 * solve(3, n - 1) + 3;
}
int main() {
while (cin >> m >> n) {
cout << solve(m, n) << endl;
}
return 0;
}
找新朋友
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 32780;
bool st[N];
int main() {
int t;
cin >> t;
while (t -- ) {
int n;
cin >> n;
int ans = n - 1;
memset(st, false, sizeof st);
for (int i = 2; i < n; i ++ )
if (n % i == 0)
for (int j = i; j < n; j += i)
st[j] = true;
for (int i = 1; i <= n; i ++ )
if (st[i])
ans -- ;
cout << ans << endl;
}
return 0;
}
Eddy’s research I
埃式筛法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int primes[N];
int cnt;
bool st[N];
int a[N];
void get_primes() {
for (int i = 2; i < N; i ++ ) {
if (st[i])
continue;
primes[cnt ++ ] = i;
for (int j = i + i; j < N; j += i)
st[j] = true;
}
}
int main() {
get_primes();
int n;
while (cin >> n) {
int j = 0;
for (int i = 0; i < cnt;) {
if (n == primes[i]) {
a[j] = primes[i];
break;
} else {
if (n % primes[i] == 0) {
n /= primes[i];
a[j ++ ] = primes[i];
} else
i ++ ;
}
}
if (j == 0)
printf("%d\n", a[j]);
else {
for (int i = 0; i < j; i ++ )
printf("%d*", a[i]);
printf("%d\n", a[j]);
}
}
return 0;
}
线性筛法
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 65550;
const int M = 100010;
int primes[N], cnt;
bool st[N];
int a[M];
void get_primes() {
for (int i = 2; i < N; i ++ ) {
if (!st[i])
primes[cnt ++ ] = i;
for (int j = 0; primes[j] <= N / i; j ++ ) {
st[primes[j] * i] = true;
if (i % primes[j] == 0)
break;
}
}
}
int main() {
get_primes();
int x;
while (cin >> x) {
int j = 0;
for (int i = 0; i < cnt;) {
if (x == primes[i]) {
a[j] = x;
break;
}
if (x % primes[i] == 0) {
a[j ++ ] = primes[i];
x /= primes[i];
} else
i ++ ;
}
if (j == 0)
printf("%d\n", x);
else {
for (int i = 0; i < j; i ++)
printf("%d*", a[i]);
printf("%d\n", a[j]);
}
}
return 0;
}
七夕节
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500010;
int a[N] = {1};
void init() {
for (int i = 1; i <= N / 2; i ++ )
for (int j = i + i; j <= N; j += i)
a[j] += i;
}
int main() {
int t;
cin >> t;
init();
while (t -- ) {
int n;
cin >> n;
cout << a[n] << endl;
}
return 0;
}