hdu 2588 欧拉函数的应用

/*
* hdu2588/linux.c
* Created on: 2011-8-2
* Author : ben
*/
#include
<stdio.h>
#include
<math.h>

int phi(int x) {
int i, res = x, temp = (int) sqrt(x);
for (i = 2; i < temp + 1; i++) {
if (x % i == 0) {
res
= res / i * (i - 1);
while (x % i == 0) {
x
/= i;
}
}
}
if (x > 1) {
res
= res / x * (x - 1);
}
return res;
}

void work() {
int T, N, M, i, sqrtn, ans;
scanf(
"%d", &T);
while (T--) {
scanf(
"%d%d", &N, &M);
sqrtn
= sqrt(N);
ans
= 0;
for (i = 1; i < sqrtn; i++) {
if (N % i != 0) {
continue;
}
if (i >= M) {
ans
+= phi(N / i);
}
if (N / i >= M) {
ans
+= phi(i);
}
}
if (sqrtn * sqrtn == N && sqrtn >= M) {
ans
+= phi(sqrtn);
}
printf(
"%d\n", ans);
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
work();
return 0;
}
posted @ 2011-08-02 11:56  moonbay  阅读(200)  评论(0编辑  收藏  举报