Calculation 2
Posted sydevil
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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{nphi(n)}{2})
证明:
(1.)总情况为(frac{n*(n-1)}{2})
(2.)不合法为(frac{nphi(n)}{2})
结论(1:)若((a,n)=1,)则((n-a,n)=1)
证明:
((a,n)=(n-a,a)=(n-a,n))(更相减损法)
结论(2:)不合法和为(frac{nphi(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{nphi(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{nphi(n)}{2})
综上(:)不合法为(frac{nphi(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-1)-n*work(n))/2ll%1000000007ll);
return 0;
}
以上是关于Calculation 2的主要内容,如果未能解决你的问题,请参考以下文章