Calculation 2
Time Limit: 1000MS |
|
Memory Limit: 32768KB |
|
64bit IO Format: %i64d &%i64u |
Submit Status
Description
Given a positive integer n, your task is to calculate the sum of the positive integers less than N which was not coprime t o N. A is said to being coprime to B if A, B share no common positive divisors except 1.
Input
For each test case, the 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 of module 1000000007 in a line.
Sample Input
Sample Output
Introduction of Euler functions: (formula derivation, can see relevant information, not mentioned here) first we have to know what the Euler function is.
The purpose of Euler's function is to find the number of coprime before n by a particular law.
Set φ function to Euler function, then φ (x) =x (1-1/P1) (1-1/P2) (1-1/P3) (1-1/P4) .... (1-1/PN), where P1, P2......pn is X's
All factorization (in this case, the quality factor is the type, not the number) (for example, 12=2*2*3, its factors are prime), X is not a 0 integer
So this is a good question. Simplify the above formula: if P1....PN is the mass factor of X
φ (x) =x* ((p1-1)/p1) ((p2-1)/p2) ((p3-1)/p3) ((p4-1)/p4) ..... ((pn-1)/PN) (You don't have to ignore the previous x to multiply)
So the following code is the Euler function and the next thing to say is, if we know the number of coprime before n
, then not coprime how to ask, very simple, the first n and sum (n) = (n+1) *N/2 High School knowledge Point;
Then (coprime) =sum (n)-(the number of coprime before N).
How to ask N before the number of coprime with it, first we should know that a theorem is
If GCD (n,i) = 1, then gcd (n,n-i) = 1, so the first n number if I with n coprime then n-i with N is also coprime
So come, n before with it the number of coprime and = (A1+N-A1) + (A2+N-A2) + (A3+N-A3) + (A4+N-A4) + ... (An+n-an)
This is not exactly (how many pairs I and n-i times N) is the result. And the number of coprime we
Also know, then how many pairs is φ (x)/2, the number of coprime and =φ (x)/2*n.
Next (the number of coprime and) =sum (n)-(the number of coprime before N and it) can be obtained
/*author:2486memory:1408 kbtime:15 mslanguage:g++result:acceptedvj runid:4178187real runid:14209894*/#include <c stdio> #include <cstring> #include <algorithm> #include <cmath>using namespace Std;typedef long Long Ll;const LL mod=1000000007; LL N; ll Getol (ll x) { ll res=x; for (int i=2; i<=sqrt (x); i++) { if (x%i==0) { res=res/i* (i-1); while (x%i==0) { x/=i;}} } if (x>=2) { res=res/x* (x-1); } return res;} int main () {while (~scanf ("%i64d", &n), N) { ll ans1=n* (n+1)/2-n;//n before, so N is not included in summation LL ans2=getol (N) * N/2; printf ("%i64d\n", (ANS1-ANS2)%mod); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Application of calculation 2-Euler function