码迷,mamicode.com
首页 > 其他好文 > 详细

Calculation 2

时间:2019-11-22 19:42:38      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:The   while   etc   freopen   return   ++   get   def   follow   

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;
}

Calculation 2

标签:The   while   etc   freopen   return   ++   get   def   follow   

原文地址:https://www.cnblogs.com/SYDevil/p/11913368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!