AtCoder Beginner Contest 162 E Sum of gcd of Tuples (Hard) 莫比乌斯反演做法

另一种容斥做法

反演做法(复习一下反演)

n个求和

∑ i = 1 k ∑ j = 1 k . . . ∑ z = 1 k g c d ( i , j . . . z ) \sum_{i=1}^{k}\sum_{j=1}^{k}...\sum_{z=1}^{k} gcd(i,j...z) i=1kj=1k...z=1kgcd(i,j...z)

做个变换 ∑ x = 1 k x ∑ i = 1 k ∑ j = 1 k . . . ∑ z = 1 k [ g c d ( i , j . . . z ) = x ] \sum_{x = 1}^{k}x\sum_{i=1}^{k}\sum_{j=1}^{k}...\sum_{z=1}^{k} [gcd(i,j...z) = x] x=1kxi=1kj=1k...z=1k[gcd(i,j...z)=x]

f ( x ) = ∑ i = 1 k ∑ j = 1 k . . . ∑ z = 1 k [ g c d ( i , j . . . z ) = x ] f(x) = \sum_{i=1}^{k}\sum_{j=1}^{k}...\sum_{z=1}^{k} [gcd(i,j...z) = x] f(x)=i=1kj=1k...z=1k[gcd(i,j...z)=x]

g ( x ) = ∑ x ∣ d f ( d ) = ∑ x ∣ d ∑ i = 1 k ∑ j = 1 k . . . ∑ z = 1 k [ g c d ( i , j . . . z ) = d ] g(x) = \sum_{x | d}f(d) =\sum_{x | d}\sum_{i=1}^{k}\sum_{j=1}^{k}...\sum_{z=1}^{k} [gcd(i,j...z) = d] g(x)=xdf(d)=xdi=1kj=1k...z=1k[gcd(i,j...z)=d]

= [ ( i n t ) ( k x ) ] n [(int)(\frac{k}{x})]^n [(int)(xk)]n gcd(i,j,…z)为x的倍数,每个位置取值 ( i n t ) ( k x ) (int)(\frac{k}{x}) (int)(xk)

反演一手 f ( x ) = ∑ x ∣ d u ( d x ) g ( d ) = ∑ x ∣ d u ( d x ) [ ( i n t ) ( k x ) ] n f(x) = \sum_{x|d}{u(\frac{d}{x})}g(d) = \sum_{x|d}{u(\frac{d}{x})}[(int)(\frac{k}{x})]^n f(x)=xdu(xd)g(d)=xdu(xd)[(int)(xk)]n

原式就 = ∑ x = 1 k x ∑ x ∣ d u ( d x ) [ ( i n t ) ( k d ) ] n = \sum_{x=1}^{k}x\sum_{x|d}{u(\frac{d}{x})}[(int)(\frac{k}{d})]^n =x=1kxxdu(xd)[(int)(dk)]n

T = d x T = \frac{d}{x} T=xd , d = T ∗ x d = T*x d=Tx

= ∑ x = 1 k x ∑ T u ( T ) [ ( i n t ) ( k T ∗ x ) ] n \sum_{x=1}^{k}x\sum_{T}{u(T)}[(int)(\frac{k}{T*x})]^n x=1kxTu(T)[(int)(Txk)]n

D = x ∗ T D =x * T D=xT

= ∑ D = 1 k [ ( i n t ) ( k T ∗ x ) ] n ∑ T ∣ D u ( T ) ∗ D T \sum_{D=1}^{k}[(int)(\frac{k}{T*x})]^n\sum_{T|D}{u(T)}*\frac{D}{T} D=1k[(int)(Txk)]nTDu(T)TD

= ∑ D = 1 k [ ( i n t ) ( k D ) ] n p h i ( D ) \sum_{D=1}^{k}[(int)(\frac{k}{D})]^nphi(D) D=1k[(int)(Dk)]nphi(D)

然后分块+预处理phi前缀和

#pragma GCC optimize(2)
#include
using namespace std;
const int man = 2e5+10;
#define IOS ios::sync_with_stdio(0)
typedef long long ll;
const ll mod = 1e9+7;

bool vis[man];
int prime[man],phi[man];

void init(int maxn){
	phi[1] = 1;
	vis[1] = 1;
	int cnt = 0;
	for(int i = 2;i <= maxn;i++){
		if(!vis[i]){
			prime[cnt++] = i;
			phi[i] = i-1;
		}
		for(int j = 0;j < cnt && i * prime[j] <= maxn;j++){
			vis[i*prime[j]] = 1;
			phi[i*prime[j]] = phi[i]*phi[prime[j]];
			if(i%prime[j]==0){
				phi[i*prime[j]] = phi[i]*prime[j];
				break;
			}
		}
	}
	for(int i = 1;i <= maxn;i++){
		phi[i] = (phi[i-1] + phi[i])%mod;
	}
}

ll quick_mod(ll a,ll b){
	ll ans = 1;
	while(b){
		if(b&1)ans = ans * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ans;
}

int main() {
	#ifndef ONLINE_JUDGE
		//freopen("in.txt", "r", stdin);
		//freopen("out.txt","w",stdout);
	#endif
	int n,k;
	cin >> n >> k;
	init(k+5);
	ll ans = 0;
	for(int l = 1,r = 0;l <= k;l = r + 1){
		r = k / (k / l);
		ans = (ans + quick_mod(k/l,n)*(phi[r] - phi[l-1] + mod)%mod) % mod;
	}
	cout << ans << endl;
	return 0;
}

你可能感兴趣的:(atcoder,莫比乌斯反演,数学)