zoukankan      html  css  js  c++  java
  • HDU 1286 找新朋友 (欧拉函数)

    找新朋友

    http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=8

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1560 Accepted Submission(s): 781
     
    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)

    求解与n(1-n-1)互质的质因子的个数

    解析:(转)

    定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质的数的数目。

        例如:φ(8)=4,因为1357均和8互质。

    性质:1.p是质数,φ(p)= p-1.

       2.n是质数pk次幂,φ(n)=(p-1)*p^(k-1)。因为除了p的倍数都与n互质

       3.欧拉函数是积性函数,若m,n互质,φ(mn)= φ(m)φ(n).

      根据这3条性质我们就可以推出一个整数的欧拉函数的公式。因为一个数总可以写成一些质数的乘积的形式。

      E(k)=(p1-1)(p2-1)...(pi-1)*(p1^(a1-1))(p2^(a2-1))...(pi^(ai-1))

        = k*(p1-1)(p2-1)...(pi-1)/(p1*p2*...*pi)

        = k*(1-1/p1)*(1-1/p2)...(1-1/pk)

    在程序中利用欧拉函数如下性质,可以快速求出欧拉函数的值(aN的质因素)

      若( N%==0&&(N/a)%==0)则有:E(N)= E(N/a)*a;

      若( N%==0&&(N/a)%!=0)则有:E(N)= E(N/a)*(a-1);

     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int Eular(int x){
        int ans=1;
        for(int i=2;i*i<=x;i++){
            if(x%i==0){
                x/=i;
                ans*=(i-1);
                while(x%i==0){
                    x/=i;
                    ans*=i;
                }
            }
        }
        if(x>1)
            ans*=(x-1);
        return ans;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            printf("%d\n",Eular(n));
        }
        return 0;
    }
     练习:POJ 2407 Relatives: http://poj.org/problem?id=2407

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int Eular(int x){
        int ans=1;
        for(int i=2;i*i<=x;i++){
            if(x%i==0){
                x/=i;
                ans*=(i-1);
                while(x%i==0){
                    x/=i;
                    ans*=i;
                }
            }
        }
        if(x>1)
            ans*=(x-1);
        return ans;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n;
        while(~scanf("%d",&n) && n){
            printf("%d\n",Eular(n));
        }
        return 0;
    }
    View Code

    练习: HDU 1787 GCD Again:http://acm.hdu.edu.cn/showproblem.php?pid=1787

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    
    using namespace std;
    
    int Eular(int x){
        int ans=1;
        for(int i=2;i*i<=x;i++){
            if(x%i==0){
                ans*=(i-1);
                x/=i;
                while(x%i==0){
                    ans*=i;
                    x/=i;
                }
            }
        }
        if(x>1)
            ans*=(x-1);
        return ans;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n;
        while(~scanf("%d",&n) && n){
            printf("%d\n",n-1-Eular(n));
        }
        return 0;
    }
    View Code

     

  • 相关阅读:
    【Linux_Unix系统编程】Chapter4 文件IO
    【Unix网络编程】chapter6 IO复用:select和poll函数
    【Unix网络编程】 chapter5 TCP客户,服务器程序实例
    【Unix网络编程】chapter3 套接字编程简介
    VS自动编译脚本
    【Python编程:从入门到实践】chapter4 操作列表
    【Python编程:从入门到实践】chapter3 列表简介
    【Python编程:从入门到实践】chapter2 变量和简单数据类型
    【Unix网络编程】chapter2传输层:TCP,UDP和SCTP
    vim配置编辑php
  • 原文地址:https://www.cnblogs.com/jackge/p/2842818.html
Copyright © 2011-2022 走看看