省选专练之数学[POI2007] Zap

版权声明:LeoJAM Presents https://blog.csdn.net/fcb_x/article/details/81988656

傻逼反演

\sum_{i=1}^{n}\sum_{j=1}^{m}[GCD(i,j==d)]

不妨 n=\left \lfloor \frac{n}{d} \right \rfloor m=\left \lfloor \frac{m}{d} \right \rfloor

变成了傻逼题

\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{d|i,j}\mu (d)

交换枚举顺序

\sum_{d}\mu (d)\left \lfloor \frac{n}{d} \right \rfloor\left \lfloor \frac{m}{d} \right \rfloor

#include<bits/stdc++.h>
using namespace std;
typedef int INT;
#define int long long


const int N=1e5+100;
inline void read(int &x){
    x=0;
    char ch=getchar();
    int f=1;
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    x*=f;
}
int cnt=0;
int Prime[N]={};
int vis[N]={};
int mu[N]={};
int G[N]={};
void Pre(){
	mu[1]=1;
	for(int i=2;i<N;i++){
		if(!vis[i]){
			mu[i]=-1;
			cnt++;
			Prime[cnt]=i;
		}
		for(int j=1;j<=cnt&&Prime[j]*i<N;j++){
			vis[Prime[j]*i]=1;
			if(i%Prime[j]==0){
				mu[i*Prime[j]]=0;
				break;
			}
			mu[Prime[j]*i]=-mu[i];
		}
	}
	for(int i=1;i<N;i++)G[i]=mu[i]+G[i-1];
}
void Solve(){
	int n,m,d,ans=0;
	read(n);
	read(m);
	read(d);
	n/=d;
	m/=d;
	if(n>m)swap(n,m);
	for(int t=1,last;t<=n;t=last+1){
		last=min(n/(n/t),m/(m/t));		
		ans+=(G[last]-G[t-1])*(n/t)*(m/t);
	}
	cout<<ans<<'\n';
}
INT main(){
//	freopen("test.in","r",stdin);
	int Cas;
	Pre();
	read(Cas);
	while(Cas--){
		Solve();
	}
}

猜你喜欢

转载自blog.csdn.net/fcb_x/article/details/81988656