天天看点

POJ 1284 Primitive Roots (欧拉函数&原根定理)

http://poj.org/problem?id=1284

参考《数论概论》定理21.2:

POJ 1284 Primitive Roots (欧拉函数&原根定理)

完整代码:

/*16ms,652KB*/

#include <cstdio>
const int mx = 65536;

int phi[mx];

void phi_table()
{
	int i, j;
	for (i = 2; i < mx; ++i)
		if (!phi[i])
			for (j = i; j < mx; j += i)
			{
				if (!phi[j]) phi[j] = j;
				phi[j] -= phi[j] / i;
			}
}

int main()
{
    phi_table();
	int p;
	while(~scanf("%d",&p))
		printf("%d\n",phi[p-1]);
	return 0;
}
           

继续阅读