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

InputFor 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.OutputFor each test case, you should print the sum module 1000000007 in a line.Sample Input

3
4
0

Sample Output

0
2

如果gcd(n,i)==1,gcd(n,n-i)==1
证:i=1(mod n)
  -i=-1(mod n)
  n-i=-1+n (mod n)
  n-i=1 (mod n)
通过欧拉函数一个数n中存在phi(n)个与之互质的数,因为i+(n-i)为n也就是有n对。
则与n互质数目之和为res=phi(n)/2*n,
ans=(n-1)*n/2-res。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define N 1000010
#define maxn 1000010
#define mod 1000000007
using namespace std;
typedef long long ll;
int p[N];
int prime[N];
int pn=0;
bool vis[N];
int main()
{
    for (int i = 2; i < N; i++) {
        if (vis[i]) continue;
        prime[pn++] = i;
        for (int j = i; j < N; j += i)
            vis[j] = 1;
    }
    ll n;
    while(~scanf("%lld",&n),n)
    {
        ll r=n;
        ll phi=n;
        for(int i=0;prime[i]*prime[i]<=r;i++)
        {
            ll tem=0;
            while(r%prime[i]==0)
            {
                r/=prime[i];
                tem++;
            }
            if(tem)
               phi=phi-phi/prime[i];
        }
        if(r!=1)
            phi-=phi/r;
        ll rem=n*phi/2;
        ll ans=n*(n-1)/2;
        printf("%lld\n",(ans-rem)%mod);
    }
}

  

转载于:https://www.cnblogs.com/ygtzds/p/7840569.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值