日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HD 1525 Euclid's Game

發(fā)布時(shí)間:2025/3/16 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HD 1525 Euclid's Game 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1525



Problem Description Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):?

25 7
11 7
4 7
4 3
1 3
1 0?

an Stan wins.?


Input The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.
Output For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.?


Sample Input 34 12 15 24 0 0
Sample Output Stan wins Ollie wins

題意:

兩個(gè)人,兩堆石頭,玩游戲。每次從石頭數(shù)多的那堆中拿走石頭數(shù)少的那堆石頭的個(gè)數(shù)的倍數(shù)個(gè),保證拿了過后不能為負(fù)數(shù)。誰恰好使其中一堆石頭數(shù)為0,誰使其中某一堆石頭為0,誰贏。兩人都足夠聰明。

思路:

借助歐幾里得算法的博弈。

顯然如果a>b 且a>2*b ?先拿者必贏 因?yàn)樗梢詻Q定是自己還是對(duì)手遇到狀態(tài)(a%b,b)如果狀態(tài)(a%b,b)為必輸狀態(tài),他就讓對(duì)手面對(duì)(拿走a-a%b個(gè))。如果為必贏狀態(tài),他就自己面對(duì)(拿走-a%b-b個(gè)).

如果b<a<=2*b 只能到達(dá)(a%b,b)的狀態(tài)。


#include<iostream> #include<cstdio> #define ll __int64using namespace std;int gcd(ll a,ll b) {if(a<b) //求出誰大swap(a,b);if(b==0) //終止 必輸狀態(tài)return 0;if(a>2*b) //必勝return 1;return gcd(b,a%b)^1; //和拿了之后的輸贏情況相反 }int main() {ll a,b;while(scanf("%I64d%I64d",&a,&b)!=EOF && a+b){int ans=gcd(a,b);if(ans)printf("Stan wins\n");elseprintf("Ollie wins\n");}return 0; }



總結(jié)

以上是生活随笔為你收集整理的HD 1525 Euclid's Game的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。