Hakase and Nano【博弈】
?
Hakase and Nano
時(shí)間限制: 1 Sec??內(nèi)存限制: 128 MB
提交: 533??解決: 155
[提交] [狀態(tài)] [命題人: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”.
?
樣例輸入
復(fù)制樣例數(shù)據(jù)
2 3 1 1 1 2 3 2 1 1 2樣例輸出
Yes
No
題目大意:有兩個(gè)人Hakase和Nano,先輸入一個(gè)整數(shù)t,代表有t組數(shù)據(jù),每組數(shù)據(jù)先輸入兩個(gè)整數(shù)n,d,代表有n堆石子,d為一代表Hakase先手,d為2代表Nano先手,下面一行輸入n個(gè)整數(shù),代表每堆石子的個(gè)數(shù),由于Hakase作弊,所以每次Hakase都拿兩次石子,而Nano每次拿一次,每次取石子最少取一個(gè),最先拿完所有石子的人獲勝,若最終Hakase獲勝,則輸出Yes,否則輸出No
解決方法:博弈題目,對(duì)于n==1的情況,誰(shuí)先手則誰(shuí)獲勝,對(duì)于n==2的情況,怎么都是Hakase獲勝,對(duì)于n大于3的情況,當(dāng)Hakase先手時(shí),只要不是遇到n%3==0&&n堆石子個(gè)數(shù)均為1的情況,那么Hakase一定獲勝,否則Nano獲勝;當(dāng)Nano先手時(shí),若Nano想獲勝,他只有想辦法讓情況變?yōu)閚%3==0&&全為1,所以分類(lèi)討論即可。
代碼:
#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; }?
總結(jié)
以上是生活随笔為你收集整理的Hakase and Nano【博弈】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: UVA1583 Digit Genera
- 下一篇: Problem A: 判断操作是否合法(