↓ Skip to main content
  1. Posts/

nim遊戲的必勝策略(博弈論)

·734 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

假设有n堆石子,每堆石子的个数分别如下

a1, a2, a3, … an

定义nim-sum为a1^a2^a3…an

可以证明

1. 若a1^a2^a3…an != 0

则经过一次合法的移动之后必定可变成

a1^a2….an = 0

此时留下的局面为必胜。

2. 若a1^a2^a3…an = 0

则经过一次合法的移动,局面必定成为

a1^a2^a3…an != 0

3. 只要在某回合中留下a1,a2…an

使a1^a2^a3…an = 0,此时局面必胜

证明1:假设a1^a2^a3…an != 0 = k

则查看k的最高位,假设是在第x位上,则此位上有a1x^a2x^a3x….anx = 1 (a1x表示a1写成二进制,取a1的第x位)

必然可以寻找到一个ai(至少一个),其aix = 1

此时有ai^k < ai

改变ai这堆石子使其成为ai^k

列出式子

a1^a2^a3..^ai^…an = k

则有

a1^a2^a3..^(ai^k)^…an = a1^a2^a3..^ai^…an^k = (a1^a2^a3..^ai^…an)^k = k^k = 0 即得证

证明2:使用反证法,

假设移动了ai堆石头使其成为ai'

有a1^a2^a3..ai..an = 0

即(a1^a2…a(i-1)^a(i+1)…an)^ai = 0 可得

等式 a1^a2…a(i-1)^a(i+1)…an = ai

假设a1^a2^a3…ai’….an = 0

根据前面得出的等式a1^a2…a(i-1)^a(i+1)…an = ai

即得 ai^ai’ = 0

可推出ai = ai'

此等式必然不成立,原式得证

证明3:假设游戏开始,P1 P2两名玩家,初始nim-sum != 0,使用必胜策略,P1始终能得到nim-sum != 0 的局面,而游戏过程石头始终在减少,查看最终局面:只有一行,此行可能有若干个石头,而游戏进行到最后必然会得出这个局面,这个局面是一个nim-sum != 0的局面,P1肯定能够得到这个局面,所以游戏必胜。

Related

bestcoder #56 div 2 B Clarke and problem(dp)

·437 words·1 min
果然dp還是弱項啊啊啊啊.. 不過比最開始的完全無從下手強了不少應該... 至少dp狀態表示相對了....轉移方程沒想出來嗚嗚嗚 官方題解:设d(i, j)d(i,j)表示前ii个数,模pp为jj的方案数,则容易得到d(0, 0)=1, d(i, j)=d(i-1, j)+sum_{j=0}^{p-1} d(i-1, (j-a[i]) mod p)d(0,0)=1,d(i,j)=d(i−1,j)+∑j=0p−1d(i−1,(j−a[i]) mod p),很多人没1a是因为没注意|a_i| le 10^9∣ai∣≤109

best coder #56 div 2 A Clarke and minecraft(贪心)

·303 words·1 min
贪心..尽量把一样的材料放在一起... 然后写蠢了..妈蛋... 详情见代码 代码实现 1/************************************************************************* 2 > File Name: code/bc/#56/1001.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月19日 星期六 18时55分49秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#include<cctype> 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define ms(a,x) memset(a,x,sizeof(a)) 25#define lr dying111qqz 26using namespace std; 27#define For(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef double DB; 30const int inf = 0x3f3f3f3f; 31const int N=5E2+7; 32int a[N],b[N]; 33int ans,cnt; 34int p[N]; 35int n; 36int main() 37{ 38 #ifndef ONLINE_JUDGE 39 freopen("in.txt","r",stdin); 40 #endif 41 int T; 42 cin>>T; 43 while (T--) 44 { 45 ans = 0; 46 cnt = 0; 47 ms(p,0); 48 scanf("%d",&n); 49 for ( int i = 0 ; i < n ; i++ ) 50 { 51 scanf("%d %d",&a[i],&b[i]); 52 p[a[i]]+=b[i]; 53 } 54 int kind = 0; 55 for ( int i = 1 ;i <= 500 ; i++) 56 { 57 if (p[i]!=0) 58 { 59 // cnt = cnt + (p[i]-1)/64 + 1; 60 cnt = cnt + (p[i]+63)/64; //这样写不知高到哪里去了. 61 p[i] = 0; 62 } 63 64 } 65 //ans = ans + (cnt-1)/36+1; 66 ans = (cnt+35)/36; //不知高到哪里去了... 67 printf("%d\n",ans); 68 69 70 } 71 72 73 #ifndef ONLINE_JUDGE 74 fclose(stdin); 75 #endif 76 return 0; 77}

codeforces #320 div 2A - Raising Bacteria (位运算)

·266 words·1 min
x的二进制表示中1的个数即为答案. 原因是,每天晚上糖果数量翻倍,相当于左移1位,这时候二进制表示中1的数量不变 也就是说,二进制表示中的所有的1,一定都是添加进去的 而且也只有二进制表示中的1是添加进去的

codeforces #319 div 2 E C. Points on Plane (分块)

·453 words·1 min
初识分快. 引一段题解: Let’s split rectangle 106 × 106 by vertical lines into 1000 rectangles 103 × 106. Let’s number them from left to right. We’re going to pass through points rectangle by rectangle. Inside the rectangle we’re going to pass the points in increasing order of y-coordinate if the number of rectangle is even and in decreasing if it’s odd. Let’s calculate the maximum length of such a way. The coordinates are independent. By y-coordinate we’re passing 1000 rectangles from0 to 106, 109 in total. By x-coordinate we’re spending 1000 to get to the next point of current rectangle and 2000 to get to next rectangle. That means, 2 * 109 + 2000000 in total, which perfectly fits.