bzoj4407 于神之怒加强版

题目描述:

bz

luogu

题解:

莫比乌斯反演。

能搞出来这个式子:$\sum\limits _{T=1}^{min(n,m)} \lfloor \frac{n}{T} \rfloor \lfloor \frac{m}{T} \rfloor \sum\limits_{g|T} g^K \mu (\frac{T}{g})$

后面积性函数线性筛。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 10000050;
const int MOD = 1000000007;
template<typename T>
inline void read(T&x)
{
    T f = 1,c = 0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
    x = f*c;
}
template<typename T>inline void Mod(T&x){if(x>=MOD)x-=MOD;}
ll fastpow(ll x,int y)
{
    ll ret = 1;
    while(y)
    {
        if(y&1)ret=ret*x%MOD;
        x=x*x%MOD;y>>=1;
    }
    return ret;
}
bool vis[N];
int pri[N/10],pnt,K;
ll s[N];
inline void init()
{
    s[1] = 1;
    for(register int i=2;i<=5000000;++i)
    {
        if(!vis[i])
        {
            pri[++pnt]=i;
            s[i] = fastpow(i,K)-1;
        }
        for(register int j=1;i*pri[j]<=5000000;++j)
        {
            int now = i*pri[j];
            vis[now]=1;
            if(i%pri[j])s[now] = s[i]*s[pri[j]]%MOD;
            else
            {
                s[now] = s[i]*(s[pri[j]]+1)%MOD;
                break;
            }
        }
    }
    for(register int i=1;i<=5000000;++i)Mod(s[i]+=s[i-1]);
}
int T,n,m;
inline void work()
{
    read(n),read(m);
    ll ans = 0;int mn = min(n,m);
    for(register int i=1,j;i<=mn;i=j+1)
    {
        j = min(n/(n/i),m/(m/i));
        Mod(ans+=1ll*(n/i)*(m/i)%MOD*(s[j]+MOD-s[i-1])%MOD);
    }
    printf("%lld\n",ans);
}
int main()
{
    freopen("tt.in","r",stdin);
    read(T),read(K);init();
    while(T--)work();
    return 0;
}
View Code

 

posted @ 2019-07-08 11:10  LiGuanlin  阅读(235)  评论(0编辑  收藏  举报