取石子遊戲 BZOJ1874 博弈
阿新 • • 發佈:2018-12-05
ring void 我們 div gcd 第一步 取石子遊戲 pac als 小H和小Z正在玩一個取石子遊戲。 取石子遊戲的規則是這樣的,每個人每次可以從一堆石子中取出若幹個石子,
每次取石子的個數有限制,誰不能取石子時就會輸掉遊戲。 小H先進行操作,他想問你他是否有必勝策略,如果有
,第一步如何取石子。
Input 輸入文件的第一行為石子的堆數N 接下來N行,每行一個數Ai,表示每堆石子的個數 接下來一行為每次取石子個數的種類數M 接下來M行,每行一個數Bi,表示每次可以取的石子個數, 輸入保證這M個數按照遞增順序排列。 N≤10 Ai≤1000
對於全部數據,M≤10,Bi≤10
Output
輸出文件第一行為“YES”或者“NO”,表示小H是否有必勝策略。
若結果為“YES”,則第二行包含兩個數,第一個數表示從哪堆石子取,第二個數表示取多少個石子,
若有多種答案,取第一個數最小的答案,
若仍有多種答案,取第二個數最小的答案。
Sample Input4
7
6
9
3
2
1
2
首先算出sg函數;
數據範圍較小,直接計算即可;
如果所有a[ i ] 的sg函數=0,那麽此時就NO;
否則就為 YES,這時我們只需枚舉遍歷即可;
註意一點:運算符優先級問題,^的時候要加();
Sample OutputYES 1 1 Hint 樣例中共有四堆石子,石子個數分別為7、6、9、3,每人每次可以從任何一堆石子中取出1個或者2個石子,小H有 必勝策略,事實上只要從第一堆石子中取一個石子即可。
Input 輸入文件的第一行為石子的堆數N 接下來N行,每行一個數Ai,表示每堆石子的個數 接下來一行為每次取石子個數的種類數M 接下來M行,每行一個數Bi,表示每次可以取的石子個數, 輸入保證這M個數按照遞增順序排列。 N≤10 Ai≤1000
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#pragma GCC optimize(2) //#include<cctype> //#pragma GCC optimize("O3") using namespace std; #define maxn 100005 #define inf 0x3f3f3f3f #define INF 9999999999 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-3 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() { ll x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == ‘-‘) f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll sqr(ll x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ ll qpow(ll a, ll b, ll c) { ll ans = 1; a = a % c; while (b) { if (b % 2)ans = ans * a%c; b /= 2; a = a * a%c; } return ans; } int n, m; int a[maxn], b[maxn]; bool vis[maxn]; int sg[maxn]; void SG() { for (int i = 1; i <= 2000; i++) { ms(vis); for (int j = 1; j <= m; j++) { if (i - b[j] >= 0)vis[sg[i - b[j]]] = 1; } for (int j = 0; j <= 10; j++) if (vis[j] == 0) { sg[i] = j; break; } } } int main() { //ios::sync_with_stdio(0); rdint(n); for (int i = 1; i <= n; i++)rdint(a[i]); rdint(m); for (int i = 1; i <= m; i++)rdint(b[i]); SG(); int ans = 0; for (int i = 1; i <= n; i++)ans ^= sg[a[i]]; if (ans == 0)cout << "NO" << endl; else { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i] - b[j] >= 0) { if ((ans ^ (sg[a[i]]) ^ (sg[a[i] - b[j]])) == 0) { cout << "YES" << endl; cout << i << ‘ ‘ << b[j] << endl; return 0; } } } } } return 0; }
取石子遊戲 BZOJ1874 博弈