zoukankan      html  css  js  c++  java
  • HDU 3501 Calculation 2(欧拉函数的引申)

    Calculation 2

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1264    Accepted Submission(s): 530


    Problem Description
    Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is said to be coprime to B if A, B share no common positive divisors except 1.
     
    Input
    For each test case, there is a line containing a positive integer N(1 ≤ N ≤ 1000000000). A line containing a single 0 follows the last test case.
     
    Output
    For each test case, you should print the sum module 1000000007 in a line.
     
    Sample Input
    3 4 0
     
    Sample Output
    0 2
     
    Author
    GTmac
     
    Source
     
    Recommend
    zhouzeyong
     
     
     
    其实就是一个欧拉函数的推广。
    小于等于n,与n互质的数的个数是phi(n) 叫欧拉函数
    小于等于n,与n互质的数的和是  phi(n)*n/2;
    所以总和减掉就是答案了。
    /*
    HDU 3501
    求小于N与N不互质的数的和
    欧拉公式的引伸:小于或等于n的数中,与n互质的数的总和为:φ(x) * x / 2。(n>1)
    
    
    */
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int MOD=1000000007;
    
    //求欧拉函数
    long long eular(long long n)
    {
        long long ret=n;
        long long i;
        for(i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                ret-=ret/i;
                while(n%i==0)n/=i;
                if(n==1)break;
            }
        }
        if(n>1)ret-=ret/n;
        return ret;
    }
    int main()
    {
        long long n;
        while(scanf("%I64d",&n),n)
        {
            long long ans=(n*(n-1)/2%MOD-eular(n)*n/2%MOD+MOD)%MOD;
            printf("%I64d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    嵌入式系统的应用
    linux shell编程
    JS高级学习历程-1
    JavaScript入门
    二叉树 数据结构
    用css固定textarea文本域大小尺寸
    ie img 3px bug
    OpenCV-Python(1)在Python中使用OpenCV进行人脸检测
    教你用Python解决非平衡数据问题(附代码)
    图片人脸检测(OpenCV版)
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2673734.html
Copyright © 2011-2022 走看看