hdu 1286 赤裸裸的欧拉函数

欧拉函数的应用。

/*
* hdu1286/linux.cpp
* Created on: 2011-9-1
* Author : ben
*/
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
using namespace std;

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}

unsigned euler(unsigned x) {
// 就是公式
unsigned i, res = x;
for (i = 2; i < (int) sqrt(x * 1.0) + 1; i++)
if (x % i == 0) {
res
= res / i * (i - 1);
while (x % i == 0)
x
/= i; // 保证i一定是素数
}
if (x > 1)
res
= res / x * (x - 1);
return res;
}

void work() {
int T, n, ans;
scanf(
"%d", &T);
while (T--) {
scanf(
"%d", &n);
ans
= euler(n);
printf(
"%d\n", ans);
}
}
posted @ 2011-09-01 08:24  moonbay  阅读(136)  评论(0编辑  收藏  举报