Calculation 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1241 Accepted Submission (s): 518
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 limit T 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 shocould 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
Question:
The sum of the numbers in which n is less than n and n are not mutually dependent is % 1000000007.
[Cpp]
/* If gcd (n, I) = 1 then gcd (n, n-I) = 1
Therefore, for n, there are phi (n) (Euler's function) numbers less than n and n ing
From the above formula, we can know that if one of them is I, one is n-I.
Then the sum of the two is a total of phi (n)/2 pairs like n.
Therefore, the sum of all numbers in the mass with n is n * phi (n)/2.
The numbers that do not interwork with n are (1 + N-1) * (n-1)/2-n * phi (n)/2.
*/
# Include <stdio. h>
# Include <math. h>
# Define mod 1000000007.
_ Int64 get_phi (_ int64 x) // It is the formula.
{
_ Int64 I, res = x;
For (I = 2; I <(_ int64) sqrt (x * 1.0) + 1; I ++)
If (x % I = 0)
{
Res = res/(_ int64) I * (I-1 );
While (x % I = 0) x/= I; // ensure that I must be a prime number.
}
If (x> 1) res = res/(_ int64) x * (x-1); // be careful not to overflow here
Return res;
}
Int main ()
{
_ Int64 n, ans;
While (scanf ("% I64d", & n )! = EOF)
{
If (! N) break;
Ans = (_ int64) (1 + N-1) * (n-1)/2-(_ int64) n * get_phi (n)/2) % mod; // do not divide the value by 2 and then multiply
Printf ("% I64d \ n", ans % mod );
}
Return 0;
}