Calculation 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1650 Accepted Submission(s): 697
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
Sample Output
0
2
求小于n的不与n互质的数的和,可以用欧拉函数求得这样的数个数
有数学理论:grd(n,i)=1,则grd(n,n-i)=1,所以与n互质的数的和为phi(n)*n/2;
综上,不与n互质且小于n的数的和为n*(n+1)/2-phi(n)*n/2-n;
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const __int64 mod=1000000007;
__int64 phi(__int64 x)
{
__int64 i,res = x;
for(i=2;i<(__int64)sqrt(x*1.0)+1;i++)
{
if(x%i==0)
{
res = res / i * (i-1);
while(x % i == 0)
x /= i;
}
}
if(x > 1)
res = res / x * (x - 1);
return res;
}
int main()
{
__int64 n;
__int64 ans;
while(~scanf("%I64d",&n))
{
if(0==n)
break;
//cout<<phi(n)<<endl;
printf("%I64d\n",phi(n));
ans=((n-1-phi(n))*n/2);
printf("%I64d\n",ans%mod);
}
return 0;
}