跳到正文

题解

题解:CF2217F Interval Game

_cout_约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年9月7日
收录
2026年9月7日
更新
2026年9月7日
篇幅
约 3 分钟
获赞
1
题目
CF2217F

版本

  1. v1当前

审核题解,管理员辛苦了。

首先我们先考虑这个游戏本质是什么。

我们觉得这个东西很像 NimNim 游戏,我们发现这个东西不就是 44 堆石子吗,直接推出式子 (n−r)⊕(l−1)(n-r)\oplus (l-1)。

我们获得了式子,我们设四堆分别有 xx,yy,ll,rr 个石子。

我们不难发现,我们 xx 选 00 即可,反正 yy 都能选。

那么先手必输当且仅当 y<ny<n 且 l+r<ml+r<m 且 y⊕l⊕r=0y\oplus l\oplus r=0。

那么第二个式子可以化为 l+l⊕y<ml+l\oplus y<m,即 l⊕y<m−ll\oplus y<m-l。

之后我们枚举 ll 运用字典树,分讨 m−lm-l 当前位的值打一个 tagtag 就行。

C++
#include <bits/stdc++.h>
#define N 2000005
using namespace std;
int tr[N][2];
int tot;
void build(int x)
{
	int u=0;
	for(int i=19;i>=0;i--)
	{
		bool c=x&(1<<i);
		if(!tr[u][c])
			tr[u][c]=++tot;
		u=tr[u][c];
	}
} 
int m; 
int d[N],sum[N];
void update(int x)
{
	int u=0;
	for(int i=19;i>=0;i--)
	{
		bool c=x&(1<<i);
		if((m-x)&(1<<i))
		{
			if(tr[u][c])
				d[tr[u][c]]++;
			u=tr[u][!c];
		}
		else
		{
			u=tr[u][c];
		}
	}
}
int query(int x)
{
	if(x==-1)
		return 1e9;
	int u=0,dis=0;
	for(int i=19;i>=0;i--)
	{
		bool c=x&(1<<i);
		dis+=d[u];
		u=tr[u][c];
	}
	dis+=d[u];
	return dis;
}
int cnt[N];
signed main()
{
	int t;
	cin>>t;
	
	while(t--)
	{
		int n;
		cin>>n>>m;
		int ans=-1;
		for(int l=0;l<max(n,m);l++)
			build(l); 
		for(int l=0;l<m;l++)
		{
			update(l);
		}
		for(int l=0;l<n;l++)
		{
			if(query(l)<query(ans))
				ans=l;
		}
		cout<<1<<" "<<n-ans<<endl;
		for(int i=0;i<=tot;i++)
			d[i]=tr[i][0]=tr[i][1]=0;
		tot=0;
	}
} 
原文