You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
37 lines
1.1 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
const int N = 1010;
|
|
int primes[N]; //保存的是每一个质数
|
|
int cnt; //cnt代表质数下标,就是到第几个了
|
|
int phi[N]; //欧拉函数值
|
|
bool st[N]; //是不是已经被筛掉了
|
|
// 求1-N之间每一个数的欧拉函数
|
|
// 线性筛法求质数的过程当中,捎带着求出每个数的欧拉函数值,其实还可以顺便求出很多东西。
|
|
void get_eulers(int n) {
|
|
phi[1] = 1;
|
|
for (int i = 2; i <= n; i++) {
|
|
if (!st[i]) {
|
|
primes[cnt++] = i;
|
|
phi[i] = i - 1;
|
|
}
|
|
for (int j = 0; primes[j] <= n / i; j++) {
|
|
int t = primes[j] * i;
|
|
st[t] = true;
|
|
if (i % primes[j] == 0) {
|
|
phi[t] = phi[i] * primes[j];
|
|
break;
|
|
} else {
|
|
phi[t] = phi[i] * (primes[j] - 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
get_eulers(1000);
|
|
for (int i = 1; i <= 1000; i++)
|
|
cout << i << ":" << phi[i] << endl;
|
|
return 0;
|
|
} |