Calculation 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2246 Accepted Submission(s): 958
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
/* HDOJ 3501 不互质数的和 欧拉函数phi(n)求1-n中与n互质的质因子的个数, 如果gcd(n,i)==1,那么就有gcd(n,n-i)==1; 所以与n互质的数应该是成对出现的,每一对的和就是n, 所以互质总和就是n*phi(n)/2; 于是求小于n并且与n不互质的所有数的和,总和减去上面互质的和 */ #include<iostream> #include<stdio.h> using namespace std; int phi(int x) { int i,ans=x; for(i=2;i*i<=x;i++) { if(x%i==0) { ans-=ans/i; while(x%i==0)//i肯定是素数 x/=i; } } if(x>1) ans=ans/x*(x-1); return ans; } int main() { __int64 ans,n; while(scanf("%I64d",&n),n) { ans=n*(n+1)/2-n;//总和 ans-=phi(n)*n/2;//减去互质的总和公式 ans%=1000000007;//再取模 printf("%I64d\n",ans); } return 0; }