题目大意:
给出一个正整数N(1<=N<=10^4),求出不大于N并且和N互质的所有正整数的个数。
又是水题。。。枚举N范围内的所有正整数,通过gcd来算出两数是否互质,若不互质,把它的倍数全部筛掉,若互质,统计一下就可以了。
代码如下:
/*
ID: Sunshine_cfbsl
LANG: C++
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 10010;
int n, ans = 1, p[MAXN];
int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b, a % b);
}
int main() {
int i = 2, j;
scanf("%d", &n);
while(i < n) {
int res = gcd(n, i);
if(res == 1) ans++;
else
if(!p[res]) for(j = res; j < n; j += res) p[j] = true;
i++;
while(p[i] && i < n) i++;
}
printf("%d\n", ans);
return 0;
}