【博弈论】POJ[2348]Euclid's Game

来源:互联网 发布:鑫众棋牌源码 编辑:程序博客网 时间:2024/06/05 21:49

好久不写博客了 最近在佳木斯培训 蒟蒻QAQ

今天来揭露一下这个虚伪的世界

【问题描述】
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): an Stan wins.
给定a,b;每次用大的数减去小的数的任意倍,保证结果是自然数,已知Stan先手,求在最优策略下,先手胜负情况。

【问题分析】

bool dfs(int a,int b){    if (a==0||b==0)        return false;    if (a<b)        swap(a,b);    for (int i=1;i*b<a;i++)        if (!dfs(a-i*b,b))            return true;    return false;       }

第一反应 但是发现RE了 这时候老师告诉我需要一些

奇淫巧技

然后我的AC代码是这样的

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int a,b;bool dfs(int a,int b){    if (a==0||b==0)        return false;    if (a<b)        swap(a,b);    for (int i=a/b;i>0;i--)        if (!dfs(a-i*b,b))            return true;    return false;       }int main(){    while(1)    {        scanf("%d%d",&a,&b);        if (a==0&&b==0)            break;        if (dfs(a,b))            printf("Stan wins\n");        else            printf("Ollie wins\n");    }    return 0;}

这个虚伪的世界 再见!

妈的一个暴搜题QAQ

正解QAQ
如果先手必胜 那么满足条件 a%b==0
所以如果 状态为 a%b,b 那么先手必胜
那么想办法将状态转移到这里 即讨论[a/b]的取值情况
不断的将(a,b)转移到(a%b,b)即可。

0 0
原创粉丝点击