HDU3501欧拉函数极重要推论

推论:小于N且与N互质的所有数的和(包括1)为 N*vola(N)/2

   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.

InputFor 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.OutputFor each test case, you should print the sum module 1000000007 in a line.Sample Input

3
4
0

Sample Output

0
2
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
LL n;
LL vola(LL x)
{
    LL res = 1;
    for(LL i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            x/=i;
            res*=(i-1);
            while(x%i==0){x/=i;res*=i;}
        }
    }
    if(x>1)
        res*=(x-1);
    return res;
}
int main()
{
    LL n;
    while(cin>>n&&n)
    {
        LL sum = (n*(n-1))/2;
        sum-=n*vola(n)/2;
        sum%=1000000007;
        cout<<sum<<endl;
    }
    return 0;
}

 

posted @ 2018-01-27 01:10  TTTCoder  阅读(209)  评论(0编辑  收藏  举报