HDU 5512(GCD性质)

来源:互联网 发布:ps美工教程 编辑:程序博客网 时间:2024/06/14 23:18

Pagodas

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1571    Accepted Submission(s): 1091


Problem Description
n pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, labelled from 1 to n. However, only two of them (labelled aand b, where 1abn) withstood the test of time.

Two monks, Yuwgna and Iaka, decide to make glories great again. They take turns to build pagodas and Yuwgna takes first. For each turn, one can rebuild a new pagodas labelled i (i{a,b} and 1in) if there exist two pagodas standing erect, labelled j and k respectively, such that i=j+k or i=jk. Each pagoda can not be rebuilt twice.

This is a game for them. The monk who can not rebuild a new pagoda will lose the game.
 

Input
The first line contains an integer t (1t500) which is the number of test cases.
For each test case, the first line provides the positive integer n (2n20000) and two different integers a and b.
 

Output
For each test case, output the winner (``Yuwgna" or ``Iaka"). Both of them will make the best possible decision each time.
 

Sample Input
162 1 23 1 367 1 2100 1 28 6 89 6 810 6 811 6 812 6 813 6 814 6 815 6 816 6 81314 6 81994 1 131994 7 12
 

Sample Output
Case #1: IakaCase #2: YuwgnaCase #3: YuwgnaCase #4: IakaCase #5: IakaCase #6: IakaCase #7: YuwgnaCase #8: YuwgnaCase #9: IakaCase #10: IakaCase #11: YuwgnaCase #12: YuwgnaCase #13: IakaCase #14: YuwgnaCase #15: IakaCase #16: Iaka


题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手。最后不能再生成点的一方输;

思路:由扩展欧几里得知道对于任意正整数,一定存在整数x,y使得 x*a + y*b = gcd(a,b);并且这个gcd是a,b组成的最小正整数;同时也知道了这也是两个点之间的最小距离。所以可修建塔的编号为 gcd(a,b)*k (1<=k<=n/k)。

之后直接求点的个数即可;

代码:

#include<bits/stdc++.h>using namespace std;int main(){    int T,n,l,r;    scanf("%d",&T);    for(int kase = 1;kase <= T;kase++){        scanf("%d%d%d",&n,&l,&r);        int gcd = __gcd(l,r);        int cnt = n/gcd;        printf("Case #%d: %s\n",kase,cnt&1?"Yuwgna":"Iaka");    }    return 0;}



原创粉丝点击