CF282D Yet Another Number Game

题意简述

n 堆石子,第 i 堆包含 ai 个,每次可以选择任意一堆取出任意数量石子,也可以选择对于所有石子堆都拿走任意数量化石子。问先手必胜还是后手必胜。

n3,ai300

解法一:动态规划

发现 ai3=2.7×107,完全能压到状态里,直接做 dp 即可。但这应该是过不了的,因为转移复杂度为 O(ai),总时间复杂度 O(ai4)。但是发现转移可以前缀和优化掉,时间复杂度 O(ai3)我不会写

解法二:博弈论

n 分类讨论:

  • n=1 时,若 ai=0 则先手败,否则先手胜。
  • n=2 时,问题是经典威佐夫博弈,威佐夫博弈模板题。不知道威佐夫博弈结论没有关系,由于堆数只有 2,套用优化前的解法一也是可以通过的,时间复杂度 O(ai3)
  • n=3 时,结论:a1a2a3=0 时先手必败,否则先手必胜。
    • 证明这个东西只需要考虑两种情况:胜态能转移到败态,败态无论如何都只能转移到胜态。
    • a1a2a30 时,考虑异或后为 1 的最高位,在三个数中必定有一个或三个数在这个数位中的值为 1,令其中该数位值为 1 的数为 a1,由于 a2a3 之后的值该数位肯定为 0,所以 a1>a2a3。将 a1a2a3,即可转移到败态。
    • a1a2a3=0 时,假设结论不成立,即存在方案使得败态能转移到败态:
      • 若此时选择取一堆,不妨令取石子的那一堆是 a1,则说明 (a1k)a2a3=0,也就是 a1k=a2a3。由于 a1a2a3=0,所以 a1=a2a3,由此 a1=a1k,但 k0,故假设不成立。
      • 若此时选择全取,则说明 (a1k)(a2k)(a3k)=0。若 ka1,a2,a3 都为 0 的数位上取 1,减去后 a1,a2,a3 在该数位上的值全为 1,不可能异或值为 0;若 ka1,a2,a3 中有两个 1 的数位上取 1,设这两个数为 a1,a2,减去后 a1,a2 在该位上为 0a31,异或值同样不为 0,故假设不成立。
点击查看代码
//#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("BitLGM")
#define PN puts("BitAryo")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
namespace Fread {
	const int SIZE=1<<21;
	char ibuf[SIZE],*S,*T;
	inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
	const int SIZE=1<<21;
	char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
	inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
	inline void putc(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=305,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,a[maxn][maxn][maxn];
void conver(int &x,int &y,int &z){
	int xx=x,yy=y,zz=z;
	x=max({xx,yy,zz}),z=min({xx,yy,zz}),y=xx+yy+zz-x-z;
}
int dfs(int x,int y,int z){
	conver(x,y,z);
	if(a[x][y][z])return a[x][y][z];
	int mn=min({x,n>=2?y:inf,n>=3?z:inf});
	rep(i,0,x-1)if(dfs(i,y,z)==-1)return a[x][y][z]=1;
	if(n>=2)rep(i,0,y-1)if(dfs(x,i,z)==-1)return a[x][y][z]=1;
	if(n>=3)rep(i,0,z-1)if(dfs(x,y,i)==-1)return a[x][y][z]=1;
	rep(i,1,mn)if(dfs(x-i,n>=2?y-i:0,n>=3?z-i:0)==-1)return a[x][y][z]=1;
	return a[x][y][z]=-1;
}
//1胜态,-1败态 
void solve_the_problem(){
	int x=0,y=0,z=0;
	n=rd(),x=rd();if(n>=2)y=rd();if(n>=3)z=rd();
	if(n==3){
		if((x^y^z)==0)PN;else PY;
		return;
	}
	a[0][0][0]=-1;
	if(dfs(x,y,z)==1)PY;else PN;
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/17999174

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示