SGU - 102
| Time Limit: 250MS | Memory Limit: 4096KB | 64bit IO Format: %I64d & %I64u |
Description
For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprime with N. Let us call two positive integers (say, A and B, for example) coprime if (and only if) their greatest common divisor is 1. (i.e. A and B are coprime iff gcd(A,B) = 1).
Input
Input file contains integer N.
Output
Write answer in output file.
Sample Input
9
Sample Output
6
Source
简单数论题!
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 10005;
int flag[maxn];
int main()
{
int n;
scanf("%d", &n);
memset(flag, 0, sizeof(flag));
int m = n;
for(int i=2; i<=m; i++) //没加=号,WA了一次
{
if(m%i==0)
{
for(int j = i; j<=n; j+=i)
{
flag[j] = 1;
}
while(m%i==0)
{
m/=i;
}
}
}
int ans = 0;
for(int i=1; i<=n; i++)
{
if(flag[i]==0)ans++;
}
printf("%d\n", ans);
return 0;
}
原文:http://blog.csdn.net/u014355480/article/details/41868613