跳到正文

题解

【1】做题心得 - 2026 NOICA #13 - T2 / P9489【二分】【数论】【容斥原理】

ExFish约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年10月17日
收录
2026年10月18日
更新
2026年11月20日
篇幅
约 7 分钟

版本

  1. v1当前

我怎么没做出来青题 NOOOOOOOOOOO。话说这场赛其实是相对简单的,但是我时间分配不当去冲 T3 失败然后就炸了。

赛时思路

直接对变动情况进行观察,可以看出每 y=LCM(x)y=\text{LCM}(x) 一个循环。但是我不会的处理的是跳过的数的个数。赛时其实差一点就能想到了,主要的问题是没有想到可以通过二分来找到位置。降智了属于是。

其实相当简单。首先你发现这相当于 n≤25n\le 25 个指针,每个指针各隔 xix_i 秒跳动一次(一个单位),每次一个状态的权值就是指针总运动里程(单位)。问一个区间内的数有多少个是合法的运动里程。题意是形式化后的,但是我觉得这个更好理解一点。

然后你发现,这 nn 个指针每隔 LCM(x)\text{LCM}(x) 秒同时跳动一次。以这个东西来分节,我们就可以获得一个循环的跳动情况,也就是在 y∈[0,LCM(x))y\in[0,\text{LCM}(x)) 时的,直接分讨计算即可。但是这样就做完了么?你会发现答案的量级非常的大,LCM(x)\text{LCM}(x) 可以达到 1022510^{225},所以大部分情况我们都很难完成处理(因为这些数字可以在 xx 非常大的情况下出现非常多重复的)。

那么我们考虑容斥原理。注意到答案虽然有很多重复的,但是我们只需要求出没出现的就可以了,然后我们使用前缀和转化问题,ans=[1,r]−[1,l−1]ans=[1,r]-[1,l-1]。使用观察法,你可以观察到一个数 yy 必须至少整除一个 xix_i 才是有必要统计的。这是因为 y−1→yy-1\to y 至少变动一个 xix_i,才会对答案产生贡献。

我们就发现,yy 必须是其中至少一个元素的倍数。那么问题就变为求 [1,r][1,r] 中 xix_i 的倍数个数(转化思想:用一个唯一的 yy 与 xx 建立映射关系转化)。这个东西就需要使用容斥来做了。注意到一个因子主要问题就是在 LCM 的因子中多次出现,于是我们钦定一部分因子一定出现(也就是至少),然后我们对这个东西容斥,那么因子就会被容斥成只出现一次(也就是恰好),那么我们就能得出正确答案。

难度主要在于实现代码(赛时跳了没看,悲。如果你使用枚举集合的方法会被加的数据卡常,因为你的复杂度是 O(n2nlog⁡V)\mathcal O(n2^n\log V) 的,用 dfs 可以优化到 O(2nlog⁡V)\mathcal O(2^n\log V)。

Code
C++
#define FileIO
#define RUN_DFS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define _l __int128
#define pc __builtin_popcountll
#define fi first
#define se second
#define il inline
bool stMem; double stT=clock();
const int N=25+10;
const ll P=998244353;
il ll qp(ll a,ll b=P-2){
	ll res=1;
	for(;b;b>>=1,a=(a*a)%P) if(b&1) res=(res*a)%P;
	return res;
}
// ---------------------------------------------------- //
int n; ll x[N];
il ll get_sum(ll y){ ll su=0; for(int i=0;i<n;++i) su+=y/x[i]; return su; }
il ll search_ans(ll x){
	ll l=0, r=1e18;
	while(l+1<r){
		ll mid=(l+r)/2;
		if(get_sum(mid)<=x) l=mid;
		else r=mid;
	}
	return l;
}
ll now_lmt; // 这个优化挺大的,因为 r 数取不到很大
_l my_gcd(_l x,_l y){ return y?my_gcd(y,x%y):x; }
il ll get_lcm(_l x,_l y){
	_l res=x/my_gcd(x,y)*y;
	if(res>now_lmt) return -1;
	return res;
}
il ll get_set_lcm(int state){
	ll res=1;
	for(int i=0;i<n;++i){
		if((state>>i)&1){
			res=get_lcm(x[i],res);
			if(res==-1) return -1;
		}
	}
	return res; 
}
ll ans=0, r_y;
void dfs(int p,int len,ll now_lcm){
	if(now_lcm==-1) return;
	if(p>=n){
		if(len) ans+=(len%2?+1:-1)*(r_y/now_lcm);
		return;
	}
	dfs(p+1,len+0,now_lcm);
	dfs(p+1,len+1,get_lcm(now_lcm,x[p]));
}
il ll get_ans(int r_x){
	if(r_x==0) return 0;
	r_y=search_ans(r_x); now_lmt=r_y; ans=0;
	#ifdef RUN_DFS
		dfs(0,0,1);
	#else
		for(int state=1;state<(1<<n);++state){
			ll now_lcm=get_set_lcm(state);
			if(now_lcm==-1) continue;
			ans+=(pc(state)%2?+1:-1)*(r_y/now_lcm);
		}
	#endif
	return ans;
}
int l,r;
// ---------------------------------------------------- //
il void solve(){
	cin>>n>>l>>r;
	for(int i=0;i<n;++i) cin>>x[i];
	sort(x,x+n,greater<ll>()); 
	cout<<get_ans(r)-get_ans(l-1);
	return;
}
// ---------------------------------------------------- //
bool edMem;
int main(){
	#ifdef FileIO
		freopen("B.in","r",stdin);
		freopen("B.out","w",stdout);
	#endif
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T=1; //cin>>T;
	while(T--) solve();
	cerr<<"\n";
	cerr<<"[Info] End Of Running. \n";
	cerr<<"       Time Used   : "<<(clock()-stT)/CLOCKS_PER_SEC*1000.00<<" ms\n";
	cerr<<"       Memory Used : "<<(&stMem-&edMem)/1048576.00<<" Mib\n";
	return 0;
}


原文