HDU - 5942 :Just a Math Problem (莫比乌斯)

题意:略。

思路:问题转化为1到N,他们的满足mu[d]!=0的因子d个数。  即1到N的因子的莫比乌斯系数平方和。

(经验:累加符号是累加的个数,我们把常数提到前面,然后用杜教筛累加个数即可。

https://www.cnblogs.com/clrs97/p/6012285.html

关键部分,perfectxx都给了注释的(感谢)

http://www.perfectxxlowiq.com/2018/03/22/hdu5942-%E6%95%B0%E8%AE%BA/

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000010;
const int Mod=1e9+7;
int mu[maxn],p[maxn],vis[maxn],cnt,f[maxn];
void pre()
{
    mu[1]=1;
    for(int i=2;i<maxn;i++){
        if(!vis[i]) p[++cnt]=i,mu[i]=-1;
        for(int j=1;j<=cnt&&i*p[j]<maxn;j++){
            vis[i*p[j]]=1;
            if(i%p[j]) mu[i*p[j]]=-mu[i];
            else break;
        }
    }
}
int F(ll x)
{
    if(x<maxn&&f[x]) return f[x];
    ll res=0;
    for(ll i=1,j;i<=x;i=j+1){
        j=x/(x/i); res+=(j-i+1)*(x/i);
    }
    res%=Mod; if(x<maxn) f[x]=res;
    return res;
}
int main()
{
    pre();
    int T,C=0; ll N; int ans;
    scanf("%d",&T);
    while(T--){
        scanf("%lld",&N); ans=0;
        for(int i=1;i<=N/i;i++){
            if(mu[i]) ans=(ans+F(N/i/i)*mu[i])%Mod;
        }
        printf("Case #%d: %d\n",++C,(ans+Mod)%Mod);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/9923022.html