HDU/HDOJ 4203 找规律

题目:题目链接


Problem Description
Being a pirate means spending a lot of time at sea. Sometimes, when there is not much wind, days can pass by without any activity. To pass the time between chores, pirates like to play games with coins.

An old favorite of the pirates is a game for two players featuring one stack of coins. In turn, each player takes a number of coins from the stack. The number of coins that a player takes must be a power of a given integer K (1, K, K^2, etcetera). The winner is the player to take the last coin(s).
Can you help the pirates gure out how the player to move can win in a given game situation?

Input
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
One line with two integers S and K, satisfying 1 <= S <= 10^9 and 1 <= K <= 100: the size of the stack and the parameter K, respectively.

Output
For every test case in the input, the output should contain one integer on a single line: the smallest number of coins that the player to move can take in order to secure the win. If there is no winning move, the output should be 0.

Sample Input
  
  
5 5 1 3 2 8 2 50 3 100 10

Sample Output
  
  
1 0 2 0 1

数据量大太大,我们可以在最开始进行一下打表,找一部分数据出来:

#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <queue>

using namespace std;

int sg[110], k;

int find(int i)
{
    if(sg[i] != -1)
        return sg[i];
    int use[100], j;
    memset(use, 0, sizeof(use));
    for(j = 1; j <=i; j *= k)
        use[find(i-j)] = 1;
    j = 0;
    while(use[j])
        j++;
    return sg[i] = j;
}

int main()
{
    int n, i;
    scanf("%d", &k);
    memset(sg, -1, sizeof(sg));
    for(i = 1; i <=100; ++i)
    {
        printf("%d %d\n", i, find(i));
    }
}

此时,我们可以发现:当k为奇数时,1,0,1,0,1,0。。。。这样变化,故n为奇数只需移走1个石头,而n为偶数必败
当k为偶数时,SG表是一个k+1的循环,比如k=4时,1,0,1,2,0,1,0,1,2,0.。。。故分析可知sg值为0的必败,sg值为1的只需要移走1个石头,sg值为2时,如果移走一个,则走到sg为1的点上还是必败,而移动k个石子易知可到达必胜态


所以直接判断打表就行了:

#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <queue>
using namespace std;


int main()
{
    int i, t, T, k, s;
    scanf("%d", &T);
    for(t= 1; t <= T; ++t)
    {
        scanf("%d%d", &s, &k);
        if(k & 1)
        {
            if(s&1)
            {
                printf("1\n");
            }
            else
                printf("0\n");
        }
        else
        {
            int tp = s%(k+1);
            if(tp == k)
                printf("%d\n", k);
            else
            {
                if(tp &1)
                    printf("1\n");
                else
                    printf("0\n");
            }
        }
    }
}

努力努力...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值