小志志和小峰峰的日常(SG函数)
生活随笔
收集整理的這篇文章主要介紹了
小志志和小峰峰的日常(SG函数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小志志和小峰峰的日常
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
小志志和小峰峰特別喜歡一起討論一些很好玩的問題。
小志志發現一個有趣的游戲,馬上和小峰峰進行了分享。
有 n 堆石子堆,每堆石子個數為 a[i]。
到小志志的回合:小志志可以選取其中的一堆,拿至少 1 個最多 x 個石子。
到小峰峰的回合:小峰峰可以選取其中的一堆,拿至少 1 個最多 y 個石子。
小志志先手,回合交替進行,到該玩家回合如果無法操作,該玩家輸。
Input
輸入一個 T,總共有 T 組測試數據。
每組測試數據:
第一行輸入 n 代表有 n 堆石子堆。
第二行輸入 n 堆石子堆分別的數量。
第三行輸入兩個用空格隔開的 x, y 分別代表小志志和小峰峰對于一堆石子最多能拿的數量。
(1 <= n <= 1e5, 1 <= a[i], x, y <= 1e9)
Output
小志志贏輸出 “xzz”,小峰峰贏輸出 “xff”,答案不包含 “”。
Sample Input
3
1
3
2 2
2
4 7
4 5
3
3 4 7
8 8
Sample Output
xff
xzz
xff
Nim 博弈
#include<bits/stdc++.h> using namespace std; const int N = 1e5+5; void show(int ans) {if(ans) cout << "xzz" << '\n';else cout << "xff" << '\n'; } int a[N]; int main(void) {int n, x, y, T;cin >> T;while(T--){cin >> n;for(int i = 0; i < n; i++) cin >> a[i];cin >> x >> y;if(x == y){int ans = 0;for(int i = 0; i < n; i++){ans ^= a[i]%(x+1);}show(ans);}else // x!=y{int flag = 0, ans = 0;for(int i = 0; i < n; i++){ans ^= a[i];if(a[i] > min(x, y)){flag++;}}if(!flag) //a[i] <= min(x, y){show(ans);}else //exit a[i] > min(x, y){if(x > y) cout << "xzz" << '\n';else{if(flag > 1) cout << "xff" << '\n';else{int t;for(int i = 0; i < n; i++){if(a[i] > min(x, y)){t = a[i];ans ^= a[i];break;}}if(t - ans <= x && ans <= x) {cout << "xzz" << '\n';}else cout << "xff" << '\n';}}}}}return 0; }總結
以上是生活随笔為你收集整理的小志志和小峰峰的日常(SG函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跨公司采购转储(很详细)
- 下一篇: Mysql数据库分页查询及优化