Calculation 2

Calculation 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.

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

方法:
\(ans=\frac{n*(n-1)}{2}-\frac{n\phi(n)}{2}\)


证明:
\(1.\)总情况为\(\frac{n*(n-1)}{2}\)
\(2.\)不合法为\(\frac{n\phi(n)}{2}\)
结论\(1:\)\((a,n)=1,\)\((n-a,n)=1\)

证明:

\((a,n)=(n-a,a)=(n-a,n)\)

结论\(2:\)不合法和为\(\frac{n\phi(n)}{2}\)

\(\ \ \ \ \ \ 1.\phi(n) \% 2=0\)

不合法的数列\(a_1,a_2,a_3...(n-a_{\phi(n)-2})-(n-a_{\phi(n)-1})-(n-a_{\phi(n)})\)

\(\sum_{i=1}^na_i=\frac{n\phi(n)}{2}\)


\(\ \ \ \ \ \ 2.\phi(n) \% 2=1\)

不合法的数列\(a_1,a_2,a_3...\frac{n}{2}...(n-a_{\phi(n)-2})-(n-a_{\phi(n)-1})-(n-a_{\phi(n)})\)

\(\sum_{i=1}^na_i=\frac{n\phi(n)}{2}\)

综上\(:\)不合法为\(\frac{n\phi(n)}{2}\)

\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define read read1<int>()
Type inline T read1()
{
    T t=0;
    bool ty=0;
    char k;
    do k=getchar(),(k=='-')&&(ty=1);while('0'>k||k>'9');
    do t=(t<<3)+(t<<1)+(k^'0'),k=getchar();while('0'<=k&&k<='9');
    return ty?-t:t;
}
# define int long long
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int work(int n)
{
    int tn=n;
    for(int i=2;i*i<=n;++i)
        if(!(n%i))
        {
            while(!(n%i))n/=i;
            tn=tn/i*(i-1);
        }
    if(n!=1)tn=tn/n*(n-1);
    return tn;
}
signed main()
{
    for(int n;n=read;)
        printf("%lld\n",(n*(n-1)-n*work(n))/2ll%1000000007ll);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/SYDevil/p/11913368.html