天天看點

hdu 1286:找新朋友(數論,歐拉函數)找新朋友

找新朋友

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7001    Accepted Submission(s): 3643

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 2 25608 24027  

Sample Output 7680 16016  

Author SmallBeer(CML)  

Source 杭電ACM集訓隊訓練賽(VII)  

Recommend lcy   |   We have carefully selected several similar problems for you:   1215  1406  1787  1211  3792      數論,歐拉函數。   題意是給你一個數,求與這個數互質的數的個數。   這是歐拉函數的應用,歐拉函數的作用就是求一個數與它互質的數的個數。   聽了我們實驗室某貨講的歐拉函數原理,遂自己動手實作了一遍,結果發現掉坑裡了。遂……直接套模闆過吧,反正知道原理了。   相關知識:   積性函數     歐拉函數

  代碼:

1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 int Ealer(int x)
 5 {
 6     int i,res = x;
 7     for(i=2;i<(int)sqrt(x*1.0)+1;i++)
 8         if(x%i==0){
 9             res = res / i * (i-1);
10             while(x%i==0)
11                 x/=i;    //保證i一定是素數
12         }
13     if(x>1) res = res/x*(x-1);
14     return res;
15 }
16 int main()
17 {
18     int cn;
19     cin>>cn;
20     while(cn--){
21         int n;
22         cin>>n;
23         cout<<Ealer(n)<<endl;
24     }
25     return 0;
26 }      

Freecode : www.cnblogs.com/yym2013