1. 程式人生 > >HDU 1527 取石子遊戲(威佐夫博弈)

HDU 1527 取石子遊戲(威佐夫博弈)

其中 main AC strong 大於 ron center bmi Go

取石子遊戲

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8514 Accepted Submission(s): 4837

Problem Description

有兩堆石子,數量任意,可以不同。遊戲開始由兩個人輪流取石子。遊戲規定,每次有兩種不同的取法,一是可以在任意的一堆中取走任意多的石子;二是可以在兩堆中同時取走相同數量的石子。最後把石子全部取完者為勝者。現在給出初始的兩堆石子的數目,如果輪到你先取,假設雙方都采取最好的策略,問最後你是勝者還是敗者。

Input

輸入包含若幹行,表示若幹種石子的初始情況,其中每一行包含兩個非負整數a和b,表示兩堆石子的數目,a和b都不大於1,000,000,000。

Output

輸出對應也有若幹行,每行包含一個數字1或0,如果最後你是勝者,則為1,反之,則為0。

Sample Input

2 1 8 4 4 7

Sample Output

0 1 0

code

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4
#include<cmath> 5 #include<cstring> 6 7 using namespace std; 8 9 int main () { 10 int n,m; 11 while (cin >> n >> m) { 12 if (n > m) swap(n,m); 13 int k = m - n; 14 15 if (n==(int)(k*(1+sqrt(5))/2)) puts("0"); 16 else
puts("1"); 17 } 18 return 0; 19 }

HDU 1527 取石子遊戲(威佐夫博弈)