1. 程式人生 > >hdu 5512 Pagodas【暴力打表+找規律】

hdu 5512 Pagodas【暴力打表+找規律】

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

題目大意:

一共有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");
    }
}