跳到正文

个人记录

T4

huo1234约 5 分钟
原文

这是 2026年10月17日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月17日
收录
2026年10月17日
更新
2026年11月13日
篇幅
约 5 分钟

版本

  1. v3当前
  2. v2
  3. v1

单独对于数xi∤y1x_i\nmid y1,则y1y_1没有意义。 总会存在xi∣y2x_i\mid y2使得⌊y1xi⌋=⌊y2xi⌋\lfloor\frac{y1}{x_i}\rfloor=\lfloor\frac{y2}{x_i}\rfloor。 问题变成了:在一个区间内,是x1,x2,x3,…,xnx_1,x_2,x_3,…,x_n的倍数的数有多少。 对于区间,可以用前缀和简化。 二分最大ymaxy_{max}。 考虑到有重复,使用容斥。 ∑S⊆{1,2,3,…,n}(−1)∣S∣−1ymaxlcm(xi(i∈S))\displaystyle\sum_{S\subseteq \{1,2,3,…,n\}}(-1)^{|S|-1}\frac{y_{max}}{lcm(x_i(i\in S))}

C++
#include<bits/stdc++.h>
using namespace std;

long long n,l,r,x[30];
long long lowbit(long long x){
	return x&(-x);
}
__int128 Lcm[(1<<25)];
__int128 f(__int128 y){__int128 ans=0;
	for(int i=1;i<=n;i++) ans+=y/x[i];
	return ans;
}
__int128 lcm(__int128 x,__int128 y){
	return x/__gcd(x,y)*y;
}
int W(__int128 s){
	int ans=0;
	for(int i=30;i>=0;i--){
		if((s>>i)&1){
			ans++;
		}
	}
	return ans;
}
long long solve(long long m){
	long long ans=0;
	__int128 L=0,R=2e18,max_y=0;
	while(L<=R){
		__int128 mid=(L+R)>>1;
		if(f(mid)<=m){
			max_y=mid;
			L=mid+1;
		}else{
			R=mid-1;
		}
	}
	for(long long s=1;s<(1ll<<n);s++){
		if(W(s)&1){
			ans+=max_y/Lcm[s];
		}else{
			ans-=max_y/Lcm[s];
		}
	}
	return ans;
}
signed main(){
	
	cin>>n>>l>>r;
	for(int i=1;i<=n;i++){
		cin>>x[i];
	}
//	cout<<lowbit(8ll)<<'\n';
	for(int i=0;i<n;i++){
		Lcm[(1ll<<i)]=x[i+1];
	}
	for(int i=1;i<(1<<n);i++){
		if(Lcm[i]) continue;
		for(int j=0;;j++){
			if(i&(1ll<<j)){
				Lcm[i]=lcm(x[j+1],Lcm[i-(1<<j)]);
				break;
			}
		}
	}
	cout<<solve(r)-solve(l-1);
	return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;

long long n,l,r,x[30];
long long f(long long y){long long ans=0;
	for(int i=1;i<=n;i++) ans+=y/x[i];
	return ans;
}
__int128 lcm(__int128 x,__int128 y){
	return x/__gcd(x,y)*y;
}
int W(long long s){
	int ans=0;
	for(int i=30;i>=0;i--){
		if((s>>i)&1){
			ans++;
		}
	}
	return ans;
}
long long solve(int m){
	long long ans=0;
	long long L=0,R=2e18,max_y=0;
	while(L<=R){
		long long mid=(L+R)>>1;
		if(f(mid)<=m){
			max_y=mid;
			L=mid+1;
		}else{
			R=mid-1;
		}
	}
	for(int s=1;s<(1ll<<n);s++){
		__int128 lc=1;
		for(int i=1;i<=n;i++){
			if(s&(1ll<<i-1)){
				lc=lcm(lc,x[i]);
				if(lc>max_y){
					break;
				}
			}
		}
		if(W(s)&1){
			ans+=max_y/lc;
		}else{
			ans-=max_y/lc;
		}
	}
	return ans;
}
signed main(){
//	freopen("B.in","r",stdin);
//	freopen("B.out","w",stdout);
	cin>>n>>l>>r;
	for(int i=1;i<=n;i++){
		cin>>x[i];
	}
	cout<<solve(r)-solve(l-1);
	return 0;
}