• [uva 11426] GCD


    A - GCD - Extreme (II)
     

    Description

    Given the value of N, you will have to find the value of G. The definition of G is given below:

    Here GCD(i,j) means the greatest common divisor of integer i and integer j.

    For those who have trouble understanding summation notation, the meaning of G is given in the following code: 

    G=0;
    
    for(i=1;i<N;i++)
    
    for(j=i+1;j<=N;j++)
    
    {
    
        G+=gcd(i,j);
    
    }
    
    /*Here gcd() is a function that finds the greatest common divisor of the two input numbers*/
    Input

    The input file contains at most 100 lines of inputs. Each line contains an integer N (1<N<4000001). The meaning of N is given in the problem statement. Input is terminated by a line containing a single zero. 

    Output

    For each line of input produce one line of output. This line contains the value of G for the corresponding N. The value of G will fit in a 64-bit signed integer.

    Sample

    10            67

    100          13015

    200000     153295493160

    0

    枚举最大公约数、利用欧拉函数

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define ll long long
    #define N 4000000
    
    int tot;
    int prime[N+10];
    bool isprime[N+10];
    int phi[N+10];
    void prime_pri()
    {
        tot=0;
        phi[1]=1;
        memset(isprime,1,sizeof(isprime));
        isprime[0]=isprime[1]=false;
        for(int i=2;i<=N;i++)
        {
            if(isprime[i])
            {
                prime[tot++]=i;
                phi[i]=i-1;
            }
            for(int j=0;j<tot;j++)
            {
                if((ll)i*prime[j]>N) break;
                isprime[i*prime[j]]=0;
                if(i%prime[j]==0)
                {
                    phi[i*prime[j]]=phi[i]*prime[j];
                    break;
                }
                else
                {
                    phi[i*prime[j]]=phi[i]*(prime[j]-1);
                }
            }
        }
    }
    int n;
    ll tmp[N+10];
    ll ans[N+10];
    int main()
    {
        prime_pri();
        for(int i=1;i<=N;i++) //枚举gcd
        {
            for(int j=i+i;j<=N;j+=i)
            {
                tmp[j]+=(ll)phi[j/i]*i;
            }
        }
        for(int i=2;i<=N;i++) ans[i]=ans[i-1]+tmp[i];
        while(scanf("%d",&n),n)
        {
            printf("%lld
    ",ans[n]);
        }
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    正则表达式个人总结(二):正则表达式语法的深入理解第二部分
    正则表达式个人总结(一):正则表达式语法的深入理解第一部分
    JS中的函数(二):函数参数(你可能不知道的参数传递)
    字体属性设置(一):谷歌浏览器12px以下字体的显示;方法和原理
    树莓派4B刷固件卸载ROS包
    Kobuki入门
    locobot快速使用
    AX-18A固件修复失败
    locobot 软件配置
    曾经我认为C语言就是个弟弟
  • 原文地址:https://www.cnblogs.com/hate13/p/4443490.html
Copyright © 2020-2023  润新知