• POJ


    Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. 

    "Oh, I know, I know!" Longge shouts! But do you know? Please solve it. 

    Input

    Input contain several test case. 
    A number N per line. 

    Output

    For each N, output ,∑gcd(i, N) 1<=i <=N, a line

    Sample Input

    2
    6

    Sample Output

    3
    15

    题意:∑gcd(i, N) 1<=i <=N,就这个公式,给你一个n,让你求sum=gcd(1,n)+gcd(2,n)+gcd(3,n)+…………gcd(n-1,n)+gcd(n,n),(1<=n<2^31)是多少?

     

    欧拉函数

    转https://www.cnblogs.com/handsomecui/p/4755455.html

    在数论中,对于正整数N,少于或等于N ([1,N]),且与N互质的正整数(包括1)的个数,记作φ(n)。

         φ函数的值:

        φ(x)=x(1-1/p(1))(1-1/p(2))(1-1/p(3))(1-1/p(4))…..(1-1/p(n)) 其中p(1),p(2)…p(n)为x

    的所有质因数;x是正整数; φ(1)=1(唯一和1互质的数,且小于等于1)。注意:每种质因数只有一个。

         例如:

             φ(10)=10×(1-1/2)×(1-1/5)=4;

             1 3 7 9

             φ(30)=30×(1-1/2)×(1-1/3)×(1-1/5)=8;

             φ(49)=49×(1-1/7)=42;

    欧拉函数的性质:

    (1)   p^k型欧拉函数:

    若N是质数p(即N=p), φ(n)= φ(p)=p-p^(k-1)=p-1。

    若N是质数p的k次幂(即N=p^k),φ(n)=p^k-p^(k-1)=(p-1)p^(k-1)。

    (2)mn型欧拉函数

    设n为正整数,以φ(n)表示不超过n且与n互素的正整数的个数,称为n的欧拉函数值。若m,n互质,φ(mn)=(m-1)(n-1)=φ(m)φ(n)。

    (3)特殊性质:

    若n为奇数时,φ(2n)=φ(n)。

    对于任何两个互质 的正整数a,n(n>2)有:a^φ(n)=1 mod n (恒等于)此公式即 欧拉定理

    当n=p 且 a与素数p互质(即:gcd(a,p)=1)则上式有: a^(p-1)=1 mod n (恒等于)此公式即 费马小定理

    同余定理:

         如果 a mod b = c 则有(a+kb) mod b =c(k为非0整数)

         如果 a mod b = c 则有(ka) mod b =kc (k为正整数)

         (a+b) mod c =((a mod c)+(b mod c )) mod c;

         (a*b) mod c=((a mod c)*(b mod c)) mod c

     

    phi(n/i)则为和n最大公约数为i的个数 
    素数p的欧拉函数值为p-1;

    欧拉函数模板

    复制代码
    int phi(int n)//直接求 
    {
        int t=n;
        for(int i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                t=t/i*(i-1);
                
                while(n%i==0) n/=i;
            }
        }
        if(n>1) t=t/n*(n-1);
        return t;
    }
    int p[1000001];
    void init()//打表 
    {
        memset(p,0,sizeof(p));
        p[1]=1;
        for(int i=2;i<1000000;i++)
        {
            if(p[i]==0)
            {
                for(int j=i;j<1000000;j+=i)
                {
                    if(p[j]==0)
                    p[j]=j;
                    
                    p[j]=p[j]/i*(i-1);
                }
            }
        }
    }
    复制代码

    题解

    复制代码
    #include<iostream>
    #include<stdio.h>
    #define ll long long
    using namespace std;
    ll phi(int n)
    {
        ll res=n;
        for(ll i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                res=res/i*(i-1);
                while(n%i==0)n/=i;
            }
        }
        if(n>1) res=res/n*(n-1);
        return res;
     } 
     int main()
    {
        ll n;
        while(scanf("%lld",&n)!=EOF)
        {
            ll ans=0;
            for(ll i=1;i*i<=n;i++)//这里是循环了logn
            {
                if(n%i==0)
                {
                    ans+=i*phi(n/i);//phi(n/i)则为和n最大公约数为i的个数 
                    if(i*i!=n)//让其不重复
                    {
                        ans+=n/i*phi(i);//和n最大公约数为n/i的个数
                    }
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    复制代码

     

  • 相关阅读:
    Mysql 创建表时错误:Tablespace for table `tablexx` exists. Please DISCARD the tablespace before IMPORT.
    【Problem】xampp in ubuntu下命令行启动mysql报错: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (2)
    余生,一定要找一个随时能和你聊天的人
    找个可以说话的人 不容易
    我们都应该找一个,可以一辈子陪自己说话的人
    windows下安装ubuntu
    Ubuntu 修改环境变量
    Ubuntu14.04如何备份和恢复系统
    deeplearning 源码收集
    Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明2
  • 原文地址:https://www.cnblogs.com/wpbing/p/9515928.html
Copyright © 2020-2023  润新知