標籤:
找新朋友Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9201 Accepted Submission(s): 4856
Problem Description新年快到了,“豬頭幫協會”準備搞一個聚會,已經知道現有會員N人,把會員從1到N編號,其中會長的號碼是N號,凡是和會長是老朋友的,那麼該會員的號碼肯定和N有大於1的公約數,否則都是新朋友,現在會長想知道究竟有幾個新朋友?請你編程式幫會長計算出來。
Input第一行是測試資料的組數CN(Case number,1<CN<10000),接著有CN行正整數N(1<n<32768),表示會員人數。
Output對於每一個N,輸出一行新朋友的人數,這樣共有CN行輸出。
Sample Input
22560824027
Sample Output
768016016我的逾時了~#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 32768int 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); }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
找新朋友