?
Hakase and Nano
時間限制: 1 Sec??內存限制: 128 MB
提交: 533??解決: 155
[提交] [狀態] [命題人:admin]
?
題目描述
Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). There are n packs?of pebbles, and the i-th pack contains ai pebbles. They take turns to pick up pebbles. In each turn,?they can choose a pack arbitrarily and pick up at least one pebble in this pack. The person who?takes the last pebble wins.
This time, Hakase cheats. In each turn, she must pick pebbles following the rules twice continuously.
Suppose both players play optimally, can you tell whether Hakase will win?
?
輸入
The first line contains an integer T (1≤T≤20) representing the number of test cases.
For each test case, the fi rst line of description contains two integers n(1≤n≤106) and d (d = 1 or d = 2). If d = 1, Hakase takes first and if d = 2, Nano takes first. n represents the number of pebble packs.
The second line contains n integers, the i-th integer ai (1≤ai≤109) represents the number of pebbles in the i-th pebble pack.
?
輸出
For each test case, print “Yes” or “No” in one line. If Hakase can win, print “Yes”, otherwise, print “No”.
?
樣例輸入
復制樣例數據
2
3 1
1 1 2
3 2
1 1 2
樣例輸出
Yes
No
題目大意:有兩個人Hakase和Nano,先輸入一個整數t,代表有t組數據,每組數據先輸入兩個整數n,d,代表有n堆石子,d為一代表Hakase先手,d為2代表Nano先手,下面一行輸入n個整數,代表每堆石子的個數,由于Hakase作弊,所以每次Hakase都拿兩次石子,而Nano每次拿一次,每次取石子最少取一個,最先拿完所有石子的人獲勝,若最終Hakase獲勝,則輸出Yes,否則輸出No
解決方法:博弈題目,對于n==1的情況,誰先手則誰獲勝,對于n==2的情況,怎么都是Hakase獲勝,對于n大于3的情況,當Hakase先手時,只要不是遇到n%3==0&&n堆石子個數均為1的情況,那么Hakase一定獲勝,否則Nano獲勝;當Nano先手時,若Nano想獲勝,他只有想辦法讓情況變為n%3==0&&全為1,所以分類討論即可。
代碼:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int t,n,d;int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);cin>>t;while(t--){int x,num1=0,num2=0;cin>>n>>d;for(int i=1;i<=n;i++){cin>>x;if(x==1)num1++;else num2++;}if(n==1) {if(d==1) cout<<"Yes"<<endl;else cout<<"No"<<endl;}else if(n==2) {cout<<"Yes"<<endl;}else {if(d==1) {if(num1==n&&n%3==0) cout<<"No"<<endl;else cout<<"Yes"<<endl;}else {if(n%3==0) {if(num1==n-1) cout<<"No"<<endl;else cout<<"Yes"<<endl;}else if(n%3==1) {if(num1==n||num1==n-1) cout<<"No"<<endl;else cout<<"Yes"<<endl;}else {cout<<"Yes"<<endl;}}}}return 0;
}
?