Pagodas
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 979 Accepted Submission(s): 692
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 1≤a≠b≤n) 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 1≤i≤n) if there exist two pagodas standing erect, labelled j and k respectively, such that i=j+k or i=j−k. 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 (1≤t≤500) which is the number of test cases.
For each test case, the first line provides the positive integer n (2≤n≤20000) 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
16
2 1 2
3 1 3
67 1 2
100 1 2
8 6 8
9 6 8
10 6 8
11 6 8
12 6 8
13 6 8
14 6 8
15 6 8
16 6 8
1314 6 8
1994 1 13
1994 7 12
Sample Output
Case #1: Iaka
Case #2: Yuwgna
Case #3: Yuwgna
Case #4: Iaka
Case #5: Iaka
Case #6: Iaka
Case #7: Yuwgna
Case #8: Yuwgna
Case #9: Iaka
Case #10: Iaka
Case #11: Yuwgna
Case #12: Yuwgna
Case #13: Iaka
Case #14: Yuwgna
Case #15: Iaka
Case #16: Iaka
Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
题目大意:
一共有n座山(编号为1-n),其中初始建立好a号山和b号山,每一轮游戏都能可以任选两座山,编号分别为j,k,然后建立一座山编号为j+k,或者是j-k(当然k-j也可以),Yuwgna先手的条件下,问谁最终能够赢得比赛。
思路:
1、暴力阶段:
①首先想到这样一个问题,我们初始的时候有a号山和b号山,如果我们不考虑相减的山的建立的情况的话,我们能够慢慢建立起来这样的一些山:
a,b,a+b,a+2b,2a+b,2a+2b,3a+b.........................以xa+yb为形式的山的编号。【x>=0&&y>=0】
②之后我们再加上考虑相减的山的建立的情况的话,我们能够慢慢建立起来这样的一些山:
a-b,a-2b,2a-b,2a-2b,-a-b,-a,-b........................也是以xa+yb为形式的山的编号。只不过这个时候,前边的系数可以为负。
③那么我们就可以设定一个vis【i】数组,表示数字i是否能够建立出来。然后两层for暴力枚举x,y,对应在vis【】标记上哪些山可以建立出来。然后统计出来,一共能够建立出来多少座山,然后考虑统计出来的总和数cont的奇偶性,我们就能确定当前这种情况到底是谁赢。
④但是毕竟是暴力,时间复杂度是O(n^2),1000ms实在是抵不住。那么我们考虑找规律。
2、找规律阶段:
对应输入进来的数,我们不要只看结果,同时也要看cont的计数,因为结果是根据cont的奇偶性来判断的,所以我们着重点应该在cont的计数方面上。
这时候在输入框上随便输入几个数据,如图:
不难发现,其实cont的值就是用n/gcd(a,b);
3、找到规律之后,那么就Ac了.
暴力代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int vis[200000];
int main()
{
int n,a,b;
int kase=0;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&a,&b);
memset(vis,0,sizeof(vis));
vis[a]=1;
vis[b]=1;
for(int i=-n;i<=n;i++)
{
for(int j=-n;j<=n;j++)
{
if(i*a+j*b>n||i*a+j*b<0)continue;
else
{
vis[i*a+j*b]=1;
}
}
}
int cont=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==1)cont++;
}
printf("Case #%d: %d\n",++kase,cont);
if(cont%2==0)printf("Iaka\n");
else printf("Yuwgna\n");
}
}
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int vis[200000];
int gcd(int x,int y)
{
if(y==0)return x;
else return gcd(y,x%y);
}
int main()
{
int n,a,b;
int kase=0;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&a,&b);
int cont=n/gcd(a,b);
printf("Case #%d: ",++kase);
if(cont%2==0)printf("Iaka\n");
else printf("Yuwgna\n");
}
}