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
 

 

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

posted @ 2014-04-17 21:06  Freecode#  阅读(274)  评论(0编辑  收藏  举报