题目:https://nanti.jisuanke.com/t/31448
题意:打表发现,求
思路:正着求不好求,我们可以求 总和减去与m不互质的数的贡献
与m不互质的数我们可以分解m的质因子,对于当前的因子,我们会有这些数与m不互质
对于因子的贡献就为
该式就等于
然后因子个数奇数个加偶数个减容斥一下就可以了。
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <stdio.h>
#define LL long long
#define ll LL
#define mem(arry,value) memset(arry,value,sizeof(arry))
using namespace std;
const int mod=1e9+7;
int fac[20],tot;
void deal(int m)///分解质因数
{
int tmp=m;
for(int i=2;i*i<=m;i++)
{
if(tmp%i==0)
{
fac[tot++]=i;
while(tmp%i==0)
tmp/=i;
}
}
if(tmp>1)
fac[tot++]=tmp;
return ;
}
LL fastpow(LL a, LL b, LL mod)
{
LL ret = 1;
while(b)
{
if(b&1)
ret=(ret * a)%mod;
a=(a*a)%mod;
b>>= 1;
}
return ret;
}
int main()
{
int n,m;
while(~scanf("%lld%lld",&n,&m))
{
tot=0;
deal(m);
LL sum=0;
for(int i=1;i<(1<<tot);i++)///容斥
{
LL cnt=0,tmp=1;
for(int j=0;j<tot;j++)
{
if(i&(1<<j))
{
tmp*=fac[j];
cnt++;
}
}
if(cnt%2==0)
{
LL t=n/tmp;
sum-=tmp*tmp%mod*t%mod*(t+1)%mod*(2*t+1)%mod*fastpow(6,mod-2,mod);
sum-=tmp*t%mod*(t+1)%mod*fastpow(2,mod-2,mod);
}
else
{
LL t=n/tmp;
sum+=tmp*tmp%mod*t%mod*(t+1)%mod*(2*t+1)%mod*fastpow(6,mod-2,mod);
sum+=tmp*t%mod*(t+1)%mod*fastpow(2,mod-2,mod);
}
}
LL ans=n*(n+1)%mod*(2*n+1)%mod*fastpow(6,mod-2,mod);
ans+=n*(n+1)%mod*fastpow(2,mod-2,mod);
printf("%lld\n",((ans-sum)%mod+mod)%mod);
}
}