# 【BZOJ4407】于神之怒加强版（莫比乌斯反演）

## 题面

BZOJ

$\sum_{i=1}^n\sum_{j=1}^mgcd(i,j)^k$

## 题解

$\sum_{d=1}^nd^k\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)==d]$

$\sum_{d=1}^nd^k\sum_{i=1}^{n/d}\sum_{j=1}^{m/d}[gcd(i,j)==1]$

$\sum_{d=1}^nd^k\sum_{i=1}^{n/d}\mu(i)[\frac{n/d}{i}][\frac{m/d}{i}]$

$d$除在上面太丑了

$\sum_{d=1}^nd^k\sum_{i=1}^{n/d}\mu(i)[\frac{n}{id}][\frac{m}{id}]$

$T=id$

$\sum_{d=1}^nd^k\sum_{i=1}^{n/d}\mu(i)[\frac{n}{T}][\frac{m}{T}]$

$T$给拎出来

$\sum_{T=1}^n[\frac{n}{T}][\frac{m}{T}]\sum_{d|T}d^k\mu(\frac{T}{d})$

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MOD 1000000007
#define MAX 5000000
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int fpow(int a,int b)
{
int s=1;
while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
return s;
}
int n,m,K;
int pri[MAX],tot;
int sum[MAX+1000],s[MAX];
bool zs[MAX+1000];
void pre()
{
zs[1]=true;sum[1]=1;
for(int i=2;i<=MAX;++i)
{
if(!zs[i])pri[++tot]=i,s[tot]=fpow(i,K),sum[i]=s[tot]-1;
for(int j=1;j<=tot&&i*pri[j]<=MAX;++j)
{
zs[i*pri[j]]=true;
if(i%pri[j]==0){sum[i*pri[j]]=1ll*sum[i]*s[j]%MOD;break;}
else sum[i*pri[j]]=1ll*sum[i]*sum[pri[j]]%MOD;
}
}
for(int i=1;i<=MAX;++i)sum[i]=(sum[i]+sum[i-1])%MOD;
}
int main()
{
pre();
while(T--)
{
int i=1,j;
long long ans=0;
while(i<=n)
{
j=min(n/(n/i),m/(m/i));
ans+=1ll*(n/i)*(m/i)%MOD*(sum[j]-sum[i-1])%MOD;
ans%=MOD;
i=j+1;
}
printf("%lld\n",(ans+MOD)%MOD);
}
return 0;
}


posted @ 2018-01-11 08:43  小蒟蒻yyb  阅读(400)  评论(4编辑  收藏  举报