zoj3591 Nim(Nim博弈)
2015-05-10 18:37
225 查看
ZOJ 3591 Nim(Nim博弈)
题目意思是说有n堆石子,Alice只能从中选出连续的几堆来玩Nim博弈,现在问Alice想要获胜有多少种方法(即有多少种选择方式)。
方法是这样的,由于Nim博弈必胜的条件是所有数的抑或值不为0,证明见 点击 ,所以答案就转化为原序列有多少个区间的亦或值为0,用n*(n+1) / 2 减去这个值就可以了。
而求有多少个区间的亦或值为0,实际上就是求对于亦或值的前缀nim[i],满足nim[i] == nim[j] 的对数,这时只要对nim数组排序就可以算了
详见代码:
题目意思是说有n堆石子,Alice只能从中选出连续的几堆来玩Nim博弈,现在问Alice想要获胜有多少种方法(即有多少种选择方式)。
方法是这样的,由于Nim博弈必胜的条件是所有数的抑或值不为0,证明见 点击 ,所以答案就转化为原序列有多少个区间的亦或值为0,用n*(n+1) / 2 减去这个值就可以了。
而求有多少个区间的亦或值为0,实际上就是求对于亦或值的前缀nim[i],满足nim[i] == nim[j] 的对数,这时只要对nim数组排序就可以算了
详见代码:
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define INF 0x3f3f3f3f #define inf (-((LL)1<<40)) #define lson k<<1, L, mid #define rson k<<1|1, mid+1, R #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a, b) memset(a, b, sizeof(a)) #define FIN freopen("in.txt", "r", stdin) #define FOUT freopen("out.txt", "w", stdout) #define rep(i, a, b) for(int i = a; i <= b; i ++) template<class T> T CMP_MIN(T a, T b) { return a < b; } template<class T> T CMP_MAX(T a, T b) { return a > b; } template<class T> T MAX(T a, T b) { return a > b ? a : b; } template<class T> T MIN(T a, T b) { return a < b ? a : b; } template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL; typedef long long LL; const int MAXN = 110000; const int MAXM = 20010; const double eps = 1e-4; //LL MOD = 987654321; int n, a[MAXN], x, s, w, T; int main() { while(~scanf("%d", &T)) while(T--) { cin >> n >> s >> w; LL ans = (LL)n * (n + 1) / 2; int g = s; rep (i, 1, n) { x = g; if( x == 0 ) { x = g = w; } if( g%2 == 0 ) { g = (g/2); } else { g = (g/2) ^ w; } a[i] = a[i - 1] ^ x; if(a[i] == 0) ans --; } sort(a + 1, a + n + 1); int num = 1; rep (i, 2, n) { if(a[i] == a[i - 1]) { ans -= num; num++; } else num = 1; } cout << ans << endl; } return 0; }
相关文章推荐
- [ZOJ3591] Nim && 博弈+计数
- [博弈]ZOJ3591 Nim
- HDU 1536 S-Nim(SG经典博弈)
- HDU1536 S-nim 尼姆博弈简单题
- HDU1907 && HDU2509_Nim博弈变形
- 【博弈】poj 2960 S-Nim
- zoj 3529 Nim博弈
- HDU 3032 Nim or not Nim?(博弈,打表找规律)
- HDU 3389 Game(nim博弈)
- 博弈论之Nim博弈与sg函数(二)
- poj 2960,hdu 1536 S-NIM 博弈
- hdu1850 Being a Good Boy in Spring Festival 博弈 nim-sum定理
- [ACM] hdu 1536 S-Nim(Nim组合博弈 SG函数打表)
- NIM游戏博弈
- HDOJ 题目2076取(m堆)石子游戏(nim博弈)
- 【 数论 Nim博弈 】POJ 2234 Matches Game
- [bzoj2819][树链剖分][博弈]Nim
- HDU 5011 Game 【Nim博弈】
- 巴什博弈+威佐夫博弈+Nim博弈
- HDU 1850 NIM博弈