找新朋友

來源:互聯網
上載者:User

標籤:

找新朋友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);    }}

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

找新朋友

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.