题目连接

/*
 求所有小于N且与N不互质的数的和。
 若:gcd(n,m)=1,那么gcd(n,n-m)=1;
 sum(n)=phi(n)*n/2; //sum(n)为小于n的所有与n互质的数的和
                    //phi(n)为小于n的所有与n互质的数的个数
*/
#include<cmath>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long LL;
using namespace std;
LL euler(LL n)
{
    LL m=(int)sqrt(n+0.5);
    LL ans=n;
    for(LL i=2;i<=m;i++)
        if(n%i==0)
        {
            ans=ans/i*(i-1);
            while(n%i==0)
                n/=i;
        }
    if(n>1)
        ans=ans/n*(n-1);
    return ans;
}
int main ()
{
    LL n;
    while(scanf("%lld",&n),n)
    {
        LL sum=n*(n-1)>>1;
        LL t=euler(n)*n>>1;
        sum-=t;
        printf("%lld\n",sum%1000000007);
    }
}

相关文章:

  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2021-05-02
  • 2021-09-22
  • 2022-12-23
猜你喜欢
  • 2022-01-07
  • 2021-12-17
  • 2021-08-12
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
相关资源
相似解决方案