题意:给定一个数组,要求Alice和Bob两个人一前一后的选择数并删除,Alice能否选出偶数总和的数以获得胜利。
思路:Alice选偶数时,Bob总是也选偶数留下奇数使得Alice的数字改变奇偶性,Alice选奇数时,Bob总是也选奇数使得Alice选偶数不改变她的奇偶性。
于是我们把奇偶分开来看,对于奇数的数量,做分类讨论
cnt1%4==0,Alice胜
cnt1%4==1,此时cnt2&1,Bob胜,因为剩下一个奇数Alice选,反之Alice胜
cnt1%4==2,此时Bob必胜,因为最后两个奇数一人一个
cnt1%4==3,Alice必胜,因为Alice最后拿两个奇数
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>
//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 1000010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n;
int a[N];
bool dp[110][110];
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
inline void init() {}
void slove()
{
cin >> n;
pre(i, 1, n) cin >> a[i];
int cnt1 = 0, cnt2 = 0;
pre(i, 1, n)
{
if (a[i] & 1) cnt1++;
else cnt2++;
}
if (cnt1 % 4 == 0)puts("Alice");
else if (cnt1 % 4 == 1)
{
if (n & 1)puts("Bob");
else puts("Alice");
}
else if (cnt1 % 4 == 2)
puts("Bob");
else if (cnt1 % 4 == 3)
puts("Alice");
return;
}
signed main()
{
//IOS;
int _ = 1;
si(_);
init();
while (_--)
{
slove();
}
return 0;
}
/*
1
3 15
1 9
1 9
1 8
*/
dp做法
显然,该题的胜负只与奇数和偶数的数量有关,因此,我们做关于奇数和偶数的dp
用odd,even两个二维数组保存当奇数偶数分别为i,j时,先手能否一定取到奇数或者偶数。
哎,这时候就有人要问了,不是只要求能否取到偶数吗,为什么还要有奇数呢?
我们来看看推导过程,首先,当我们从ij向之前的状态转移时,于是我们要的答案变成cntodd&1==1时,even[i][j-1],even[i-1][j]均为false,cntodd&1==0时odd[i-1][j],odd[i][j-1]均为false,所以需要odd辅助,同时,我们也需要维护odd数组
还有需要注意的边界问题(i==0||j==0)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>
//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 1000010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n;
int a[N];
bool even[110][110];
bool odd[110][110];
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
inline void init() {
/*for (int i = 1; i <= 100; i++)
{
even[i][0] = (i + 1) / 2 & 1 ^ 1;
even[0][i] = 1;
odd[i][0] = (i + 1) / 2 & 1;
odd[0][i] = 0;
}*/
even[0][1] = true, even[1][0] = false;
odd[1][0] = true, odd[0][1] = false;
for(int cnt=2;cnt<=100;cnt++)
for (int i = 0; i <= cnt; i++) {
int j = cnt - i;
if (i & 1)
{
if(i) even[i][j] |= !even[i - 1][j];
if(j) even[i][j] |= !even[i][j - 1];
if(i) odd[i][j] |= !odd[i - 1][j];
if(j) odd[i][j] |= !odd[i][j - 1];
}
else
{
if(i) even[i][j] |= !odd[i - 1][j];
if(j) even[i][j] |= !odd[i][j - 1];
if(i) odd[i][j] |= !even[i - 1][j];
if(j) odd[i][j] |= !even[i][j - 1];
}
}
}
void slove()
{
cin >> n;
pre(i, 1, n) cin >> a[i];
int cnt1 = 0, cnt2 = 0;
pre(i, 1, n)
{
if (a[i] & 1) cnt1++;
else cnt2++;
}
if (even[cnt1][cnt2])puts("Alice");
else puts("Bob");
}
signed main()
{
//IOS;
int _ = 1;
si(_);
init();
while (_--)
{
slove();
}
return 0;
}
/*
1
3 15
1 9
1 9
1 8
*/
这是1500的博弈题?,这就叫惊喜!
|