[Codeforces Round #659 (Div. 1)] B. GameGame (博弈论)
[Codeforces Round #659 (Div. 1)] B. GameGame (博弈论)
思路:
\(cnt_i\)代表这\(\mathit n\)个数中有多少个数在二进制表示中第\(\mathit i\)位为\(\text 1\)。
我们知道如果\(cnt_i \%2=0\),无论如何取,先手和后手在第\(\mathit i\)位的值都相等。
所以要找到最大的\(id\),使其\(cnt_{id} \%2=1\)。先手和后手的最终分数在二进制表示法中第\(id\)位一定不同。
所以玩家的胜败只在第\(id\)位(因为第\(id\)位的权值大,后面的无论咋取不影响两者大小关系。)决定。
当\((cnt_{id}-1)/2\)是偶数,那么先手必赢。
决策为:先手在该位先取一个\(\text 1\),然后跟着后手选,那么后手会选择偶数个\(\text 1\),结果为:先手在该位值为\(\text 1\),后手为\(\text 0\)。
当\((cnt_{id}-1)/2\)是奇数:
如果\(n\%2=1\),先手必输:
决策为:每一个回合后手只需要跟着先手选一样的,那么先手会选择偶数次\(\text 1\),结果为\(\text 0\),后手反之。
如果\(n\%2=0\),先手必赢:
决策为:先手第一步选一个\(\text 0\),然后将状态转为\(n\%2=1\)的局面,且轮到后手先选择,上面已经证明,这种局面先选择的必输。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
int a[maxn];
int cnt[40];
void PRINF2(ll num, int k)
{
for (int i = k; i >= 0; i--)
{
cout << (bool)(num & (1ll << i));
}
cout << endl;
}
int main()
{
#if DEBUG_Switch
freopen("C:\\code\\input.txt", "r", stdin);
#endif
//freopen("C:\\code\\output.txt","w",stdout);
int t;
t = readint();
while (t--)
{
n = readint();
repd(i, 1, n)
{
a[i] = readint();
}
// repd(i, 1, n)
// {
// PRINF2(a[i], 10);
// }
if (n == 1)
{
if (a[1] == 0)
{
printf("DRAW\n");
} else
{
printf("WIN\n");
}
} else if (n == 2)
{
if (a[1] == a[2])
{
printf("DRAW\n");
} else
{
printf("WIN\n");
}
} else
{
int id = -1;
for (int i = 0; i <= 30; ++i)
{
repd(j, 1, n)
{
if (a[j] & (1 << i))
{
cnt[i]++;
}
}
}
for (int i = 30; i >= 0; --i)
{
if (cnt[i] % 2 == 1)
{
id = i;
break;
}
}
if (id == -1)
{
printf("DRAW\n");
} else
{
if ( ((cnt[id] + 1) / 2) % 2 == 1)
{
printf("WIN\n");
} else
{
if (n & 1)
printf("LOSE\n");
else
printf("WIN\n");
}
}
}
for (int i = 30; i >= 0; --i)
cnt[i] = 0;
}
return 0;
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。