洛谷P1290 歐幾裏德的遊戲 數學 博弈論 模擬
阿新 • • 發佈:2017-06-26
== %d 等於 沒有 utili 黃金 最優 cst pan
洛谷P1290 歐幾裏德的遊戲
數學 博弈論 模擬
這道題我們因為當 x 大於 y 時 你也只能在合法範圍 內取 1 個 y 兩個 y
也就是說 能取的y大於等於2時,則你本質不同的取法共有兩種,此時你必定獲勝,
因為本質不同,而在最優策略下,則說明勝利者也不同,也就是說這時候你可以
決定自己的輸贏 ,我們稱這種必勝局為 v 局
2、但是如果 v 局後面還有v 局怎麽辦,這個不必擔心,因為先拿到 v局的人,
有兩種本質不同的取法,也就是說 他可以控制自己下次必定拿到 v 局,這樣就 能確保勝利了
所以說誰先到 x>=2*y 或者 y==0 的局面誰就勝利了
1 #include <cstdio> 2#include <cmath> 3 #include <cstdlib> 4 #include <cstring> 5 #include <string> 6 #include <iostream> 7 #include <iomanip> 8 using namespace std ; 9 10 int T,x,y,now,t ; 11 12 inline int work(int x,int y) 13 { 14 now = 1 ; 15 while( 1 ) 16 {17 now^=1 ; 18 if( x >= 2*y ) return now ; 19 t = x ; x = y ; y = t % y ; 20 if(y==0) return now ; 21 } 22 } 23 24 int main() 25 { 26 scanf("%d",&T) ; 27 while(T--) 28 { 29 scanf("%d%d",&x,&y) ; 30 if(x < y) swap(x,y) ;31 work(x,y) ; 32 if( now==0 ) 33 printf("Stan wins\n") ; 34 else 35 printf("Ollie wins\n") ; 36 37 } 38 return 0 ; 39 }
另外還有一種做法是用黃金比來做
黃金比例
如果兩個數相等,或者兩數之比大於斐 波拉契數列相鄰兩項之比的極限((sqrt(5)+1)/2),則先手勝,否則後手勝。
我是這樣理解的 設 x 永遠大於 y
那麽 若x 與 y 的比值 等於 黃金比的倒數 換句換說 就是 y 與x 的比值等於黃金比時 則 x 與 y 的比值永遠不會變
這時候我們的判斷條件還是一樣 但稍有不同 如果比值 到 2 則先手勝 若比值 為 0.5 則後手勝
這時候 我們把黃金比稱為平衡狀態 x增大則破壞了平衡 使 比值趨向於 2
於是 先手勝 同理如果y變大 則 比值趨向0.5 所以後手勝
所以我們直接比較 比值與黃金比的關系就行了
當然,我的理解有許多是感性的理解,並沒有理性的證明,純屬自己瞎理解,
如果哪位大神有嚴格證明的話,歡迎來告訴我
代碼
1 #include<set> 2 #include<map> 3 #include<list> 4 #include<queue> 5 #include<stack> 6 #include<string> 7 #include<math.h> 8 #include<time.h> 9 #include<vector> 10 #include<bitset> 11 #include<memory> 12 #include<utility> 13 #include<stdio.h> 14 #include<sstream> 15 #include<iostream> 16 #include<stdlib.h> 17 #include<string.h> 18 #include<algorithm> 19 #define LL unsigned long long 20 using namespace std; 21 int main() 22 { 23 int i,c,a,b,m,n,k; 24 cin>>c; 25 for (i=1;i<=c;i++) 26 { 27 scanf("%d %d",&m,&n); 28 if (m==n) printf("Stan wins\n"); 29 else 30 { 31 if (m<n) 32 { 33 if ((n*1.0)/m>(sqrt(5)+1)/2) 34 printf("Stan wins\n"); 35 else 36 printf("Ollie wins\n"); 37 } 38 else 39 { 40 if ((m*1.0)/n>((sqrt(5)+1)/2)) 41 printf("Stan wins\n"); 42 else 43 printf("Ollie wins\n"); 44 } 45 } 46 } 47 return 0; 48 }
洛谷P1290 歐幾裏德的遊戲 數學 博弈論 模擬