标签:
2 25608 24027
7680 16016我的超时了~#include<iostream> #include<stdio.h> #include<math.h> #include<string.h> using namespace std; int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } int main() { int T,n,i,count; cin>>T; while(T--) { count=0; cin>>n; for(i=2;i<n;i++) { if(gcd(i,n)>1) count++; } cout<<(n-count-1)<<endl; } return 0; }看了一个大神的博客,感觉写的很好。http://www.cnblogs.com/chuanlong/archive/2012/10/18/2728893.html<pre name="code" class="cpp">#include <stdio.h> #include <string> #define MAXN 32768 int a[MAXN + 5]; int main() { int i, j, k, CN, num, count; scanf("%d", &CN); while (CN--) { //initialize the array memset(a, 0, sizeof(a)); scanf("%d", &num); //if the number have common divisor with the input, make the value = 1 for (j = 2; j < num; j++) if (num % j == 0) for (k = j; k < num; k += j) a[k] = 1; count = 0; //numbers of zero is numbers of new friends, pay attention to k = 1 for (k = 1; k < num; k++) if (a[k] == 0) count++; printf("%d\n", count); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zuguodexiaoguoabc/article/details/47279371