Euclid's Game(0099)

来源:互联网 发布:网络黑客联系方式 编辑:程序博客网 时间:2024/04/29 05:02

Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move first or second in this game? 



According to the above rules, there are two players play tihs game. Assumptions A write a number on the board at first, then B write it. 



Your task is write a program to judge the winner is A or B.

Description

Two unequal positive numbers M and N , M>N (M<1000000)

Input

A or B

Output
1
3 1
Sample Input
1
A

分析:

本题大意:一开始黑板上有两个不同的数,两个玩家取两数的差,如果这个差不同于黑板上任何数,就写下来,轮到对方写;不能写出数的的一方为输家:A先写
 思路:①这道题也相当于在多的一堆中取数,和上面一道题类似,那也可以做类似的互质处理,也用{21,15}讨论:

            {21,15}->{21,15,6}->{21,15,6,9}->{21,15,6,9,3}.....->{21,15,6,9,3,18,12}

            {7,5}->{7,5,2}->{7,5,2,3}->{7,5,2,3,1}......->{7,5,2,3,1,6,4},效果一样,因此可以用欧几里得算法化简

          ②罗列几种互质情况观察{m,n},m<n,且互质:

           {1,2}可以写0个:

           {1,3}可以写1个:

           {2,5}可以写3个:

           {3,7}可以写5个:

           {3,8}可以写6个:

规律可见:个数都是n-2;而对A而言,偶数个就必输,奇数个必赢,即只看n%2是否为1的问题

现在问题就是不是都满足个数n-2呢?要想个数都为n-2,只要出现1就一定满足了(为什么这么说?因为一旦出现1,那么比n小的所有数都可以被减出来得到,也就是说比n小的数有n-1个,再除去最初给出的另一个数就剩n-2个数了)

那是不是任意的两个互质数都能这样减,得到1呢?

这里引入一个互质数的性质:整数a和b互质当且仅当存在整数x,y使得xa+yb=1。 或者,一般的,有存在整数x,y使得xa+yb=d,其中d是a和b的最大公约数。(贝祖定理)

有这个定理和性质后,我们继续讨论就容易多了。由于a、b>1,则x、y一定是一正一负,那xa+yb就可以写成相减的形式,如:

{5,7}->3*5+(-2)*7=1->5-(7-5)-(7-5)=1;

{7,11}->(-3)*7+2*11=1->(11-7)-[7-(11-7)]=1;

这样看来,任何两个互质数都能按这个方法相减,得到1.

那问题就解决了,代码简单如下:

#include<iostream>using namespace std;int main(){    int n,m,r,x,y;    cin>>n>>m;    x=n;    y=m;    while(y)//先用欧几里得算法做互质处理    {        r=x%y;        x=y;        y=r;    }    n/=x;    if (n%2)        cout<<"A"<<endl;    else cout<<"B"<<endl;    return 0;}



0 0
原创粉丝点击