标签:style color strong width os cti
定义
欧拉函数f(n)表示小于n并且与n互质的数的个数
f(n)=n(1?1p1)(1?1p2)…(1?1pk)(pi为n的质因子)
C++ 单个处理int eulerPhi(int n) {
int m = (int)sqrt(n+0.5);
in ans = n;
for (int i = 2; i <= m; i++) {
if (n % i == 0) {
ans = ans / i * (i-1);
while (n%i==0)
n /= i;
}
}
if (n > 1)
ans = ans / n * (n - 1);
}
C++ 欧拉函数表void phiTable(int n, int* phi) {
for (int i = 2; i <= n; i++)
phi[i] = 0;
phi[1] = 1;
for (int i = 2; i <= n; i++) {
if (!phi[i]) {
for (int j = i*2; j <= n; j += i) {
if (!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}
}
标签:style color strong width os cti
原文地址:http://blog.csdn.net/keshuai19940722/article/details/36221277