UVA 10368 - Euclid's Game(數論+博弈)

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

10368 - Euclid‘s Game

題目連結

題意:Stan和Ollie玩遊戲,有兩個數字a,b,每次可以選擇較小數位倍數,把另一個數字-去這個數,要保證>= 0,最後誰那步能得出0誰就贏了,問誰會贏。

思路:其實這個相減的過程就是一個輾轉相除的過程,考慮每一次輾轉相除,如果只有1倍的數可以減,那麼必須到下一步,如果有多步,先手的就有機會選擇是自己到下一步或者讓對方到下一步,這樣先手的就必勝了,於是利用輾轉相除,求出誰能先掌控局面,就是誰贏了。

代碼:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int n, m;void solve(int a, int b, int who) {if (!b || a / b > 1 || a == b) {printf("%s wins\n", who == 0? "Stan" : "Ollie");return;}solve(b, a % b, !who);}int main() {while (~scanf("%d%d", &n, &m) && n + m) {if (n < m) swap(n, m);solve(n, m, 0); }return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.