HDU1286新朋友欧拉函数版

找新朋友

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10401    Accepted Submission(s): 5493


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 1164 1787 1211 


i代表质数 非质数不跑 res代表剩下的数 就是答案的数 x代表质因子分解的数 从x中分解出质因子进行运用
用完2要把2的倍数去掉所以有while那句话。

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
int euler(int x)
{// 就是公式
int i, res=x;
for (i = 2; i < (int)sqrt(x * 1.0) + 1; i++)
if(x%i==0)
  {
      cout<<"start i:"<<i<<" res:"<<res<<" x:"<<x<<endl;
res = res / i * (i - 1);
cout<<"delete i:"<<i<<" res:"<<res<<" x:"<<x<<endl;
while (x % i == 0) {x /= i;cout<<"x%i==0 i:"<<i<<" res:"<<res<<" x:"<<x<<endl;} // 保证i一定是素数
cout<<endl;
}

if (x > 1) {res = res / x * (x - 1);cout<<"end i:"<<i<<" res:"<<res<<" x:"<<x<<endl;}
return res;
}
int main()
{
    int n,t;
    cin>>t;
    while(t--)
    {cin>>n;
    cout<<euler(n)<<endl;}
    return 0;
}
原文地址:https://www.cnblogs.com/Ritchie/p/5425329.html