【组合数学--容斥】CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) D. Count GCD

大概思路想出来了,就差不会写容斥了....

题意:

 思路:

这是一开始的思路:

 推到最后就是求在区间[1,m/a[i+1]中有多少k2满足以下条件,gcd(k1,k2)=1,k1是一个定值

这就是容斥原理的板子了

Code:

#include <bits/stdc++.h>

#define int long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

using namespace std;

const int mxn=1e6+10;
const int mxe=1e6+10;
const int mod=998244353;

int N,M;
int a[mxn];

int calc(int n,int top){
	vector<pair<int,int> > v;
	for(int i=2;i<=n/i;i++){
		if(n%i==0){
			int s=0;
			while(n%i==0){
				n/=i;
				s++;
			}
			v.push_back({i,s});
		}
	}
	if(n>1) v.push_back({n,1});
	int res=0,m=v.size();
	for(int x=1;x<(1<<m);x++){
		int s=0,t=1;
		for(int j=0;j<m;j++){
			if((x>>j)&1){
				if(t*v[j].first>top){
					t=-1;
					break;
				}
				t*=v[j].first;
				s++;
			}
		}
		if(t!=-1){
			if(s%2) res=(res+top/t)%mod;
			else res=((res-top/t)%mod+mod)%mod;
		}
	}
	return ((top-res)%mod+mod)%mod;
}
void solve(){
	cin>>N>>M;
	for(int i=1;i<=N;i++) cin>>a[i];
	for(int i=2;i<=N;i++){
		if(a[i-1]%a[i]!=0){
			cout<<0<<'\n';
			return;
		}
	}
	int ans=1;
	for(int i=2;i<=N;i++){
		if(a[i]==a[i-1]){
			ans=ans*(M/a[i])%mod;
		}else{
			int t=calc(a[i-1]/a[i],M/a[i])%mod;
			ans=ans*t%mod;
		}
	}
	cout<<ans%mod<<'\n';
}
signed main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int __=1;cin>>__;
    while(__--)solve();return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值