天天看點

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

繼續閱讀