Codeforces 768E Game of Stones 博弈SG(打表)
2017-05-08 11:45
323 查看
点击打开链接
题意:n堆stone,每堆s[i]个 s[i]<=60,n<=1e6,若从第i堆拿了x个 则第i堆不能再取x个,无法操作则输
找到i个石头的SG值,同样i个,取的石头可能有不同的限制.
设dp[i][j] i表示石子数量 j表示能取的状态的SG值 O(n*2^n*logn)
最终游戏要求的异或和为:dp[i][2^i-1](i=1~n) 打表找到dp[i](2^i-1)规律即可
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> ii;
const int M=2e6+20;
const int N=70;
map<pair<int,ll>,int> grundy;
map<pair<int,ll>,bool> mp;
int Grundy(int i,ll j)
{
if(mp[ii(i,j)])//该状态grundy已经被计算
return grundy[ii(i,j)];
vector<bool> mk(63,false);
for(int k=0;k<i;k++)//取k+1个
{
if(((j>>k)&1)==0)//状态j第k位为0,不能取k+1个
continue;
mk[Grundy((i-k-1ll),(j^(1ll<<k)))]=true;
}
int ret;
for(int k=0;k<63;k++)
{
if(mk[k]==false)
{
grundy[ii(i,j)]=k;
ret=k;
break;
}
}
mp[ii(i,j)]=true;
return ret;
}
void calc()
{
grundy[ii(0,0)]=0;
mp[ii(0,0)]=true;
vector<int> gr(70,0);
for(int i=0;i<=60;i++)
gr[i]=Grundy(i,(1ll<<i)-1ll),cout<<gr[i]<<' ';
}
int f
;
int main()
{
//calc();
//规律 2个1,3个2,4个3.(n+1)个n
int i=0,cnt=0;
while(i<=60)
{
for(int k=0;k<=cnt;k++)
f[i++]=cnt;
cnt++;
}
int n;
while(cin>>n)
{
int x,ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
ans=ans^f[x];
}
if(ans==0)
puts("YES");//后手win
else
puts("NO");
}
return 0;
}
题意:n堆stone,每堆s[i]个 s[i]<=60,n<=1e6,若从第i堆拿了x个 则第i堆不能再取x个,无法操作则输
找到i个石头的SG值,同样i个,取的石头可能有不同的限制.
设dp[i][j] i表示石子数量 j表示能取的状态的SG值 O(n*2^n*logn)
最终游戏要求的异或和为:dp[i][2^i-1](i=1~n) 打表找到dp[i](2^i-1)规律即可
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> ii;
const int M=2e6+20;
const int N=70;
map<pair<int,ll>,int> grundy;
map<pair<int,ll>,bool> mp;
int Grundy(int i,ll j)
{
if(mp[ii(i,j)])//该状态grundy已经被计算
return grundy[ii(i,j)];
vector<bool> mk(63,false);
for(int k=0;k<i;k++)//取k+1个
{
if(((j>>k)&1)==0)//状态j第k位为0,不能取k+1个
continue;
mk[Grundy((i-k-1ll),(j^(1ll<<k)))]=true;
}
int ret;
for(int k=0;k<63;k++)
{
if(mk[k]==false)
{
grundy[ii(i,j)]=k;
ret=k;
break;
}
}
mp[ii(i,j)]=true;
return ret;
}
void calc()
{
grundy[ii(0,0)]=0;
mp[ii(0,0)]=true;
vector<int> gr(70,0);
for(int i=0;i<=60;i++)
gr[i]=Grundy(i,(1ll<<i)-1ll),cout<<gr[i]<<' ';
}
int f
;
int main()
{
//calc();
//规律 2个1,3个2,4个3.(n+1)个n
int i=0,cnt=0;
while(i<=60)
{
for(int k=0;k<=cnt;k++)
f[i++]=cnt;
cnt++;
}
int n;
while(cin>>n)
{
int x,ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
ans=ans^f[x];
}
if(ans==0)
puts("YES");//后手win
else
puts("NO");
}
return 0;
}
相关文章推荐
- CodeForces 768E Game of Stones 打表找规律
- Codeforces 768E Game of Stones 博弈
- 51nod 1714:B君的游戏(博弈 sg打表)
- codeforces 768E Game of Stones
- codeforces 399 E. Game of Stones 博弈 思维题
- HDU - 1847 Good Luck in CET-4 Everybody! 简单博弈 SG打表
- HDU 3032 Nim or not Nim?(博弈 SG打表找规律)
- UVA 1482 - Playing With Stones(SG打表规律)
- UVA 1482 - Playing With Stones(SG打表规律)
- HDU3537-Daizhenyang's Coin(博弈SG-打表)
- UESTC 1661 Playing With Stones 博弈打表
- HDU 3032 Nim or not Nim?(博弈,SG打表找规律)
- Codeforces 768 E. Game of Stones 博弈DP
- HDU 5795 A Simple Nim 详解(SG打表找规律,博弈好题)
- codeforces 768E Game of Stones
- 【HDU3032】【Lasker's Nim(一种Nim游戏)】Nim or not Nim? Multi-SG博弈、打表
- HDU3537-Daizhenyang's Coin(博弈SG-打表)
- HDU-4664 Triangulation(博弈SG打表+类似凸包性质)
- POJ 2505 博弈SG打表
- Codeforces 768E Game of Stones