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
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT  

 

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;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/09/06/2673734.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞