省选专练之数学[POI2007] Zap

傻逼反演

\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();
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值