码迷,mamicode.com
首页 > 其他好文 > 详细

SGU - 102 - Coprimes (简单数论!)

时间:2014-12-11 17:31:12      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:sgu   acm   icpc   c++   algorithm   

SGU - 102

Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u

 Status

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




SGU - 102 - Coprimes (简单数论!)

标签:sgu   acm   icpc   c++   algorithm   

原文地址:http://blog.csdn.net/u014355480/article/details/41868613

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!