HDU5512 Pagodas(GCD+水题)

来源:互联网 发布:php微信分销开源系统 编辑:程序博客网 时间:2024/06/05 03:07

Pagodas

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


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
 
题意:给定n和a,b,分别代表有1到n所房子要建,每次只能建|a-b|或a+b编号的房子,a,b编号的房子已经存在不能再建,由Yuwgna先建,Iaka后建,谁先建不了谁输,输出赢的人的名字
思路:因为只能建a-b和a+b的房子,所以能建的房子的编号应该是gcd(a,b)的倍数那么所有能建的房子数应该是n/gcd(a,b)-2又因为是Yuwgna先建,且只有两个人建,所以n/gcd(a,b)-2对2取模,若等于0,则Yuwgna最后不能再建,Iaka赢,反之Iaka输Yuwgna赢
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int gcd(int a,int b){    return b?gcd(b,a%b):a;}int main(){    int t;    scanf("%d",&t);    int cas=1;    while(t--)    {        int n,a,b;        scanf("%d%d%d",&n,&a,&b);        if(gcd(a,b)==1)        {            if(n%2==0)                printf("Case #%d: Iaka\n",cas++);            else                printf("Case #%d: Yuwgna\n",cas++);        }        else        {            if((n/gcd(a,b)-2)%2==0)               printf("Case #%d: Iaka\n",cas++);            else               printf("Case #%d: Yuwgna\n",cas++);        }    }    return 0;}


原创粉丝点击