博弈——Doubloon Game

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LSC_333/article/details/77655086

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?
InputThe 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. OutputFor 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

题意:给两个数s和k,代表有s个金币,每回合只能从这堆金币中取走k的自然数次幂个,谁取走最后一个金币,谁就赢。对于所给的s和n,求一开始最少拿多少个可以必胜,若没有必胜的走法输出0。

思路:

看这数据也是找规律,所以先打表

打表代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long ll;
const int maxn=10;
ll a[maxn];   //存每次能取的石头数
int dfs(int x)
{
    if(x<0)
        return 0;
    for(int i=0; i<maxn; i++)
    {
        if(x<a[i])
            return 0;
        if(dfs(x-a[i])==0)
            return 1;
    }
}
int main()
{
    int k;
    while(~scanf("%d", &k))   //输入k
    {
        a[0]=1;
        for(int i=1; i<maxn; i++)
            a[i]=a[i-1]*k;
        for(int i=0; i<=15; i++)   //对s从0到15进行打表
        {
            if(dfs(i))
            {
                for(int j=0; j<maxn; j++)
                    if(dfs(i-a[j])==0)
                    {
                        printf("%d ", a[j]);
                        break;
                    }
            }
            else
                printf("0 ");
        }
        printf("\n");
    }
    return 0;
}

打表后发现规律:

令x=s%(k+1);

如果k是奇数,判断s的奇偶性,如果是奇数则输出1,否则输出0

如果k是偶数,判断x是否等于k,是就输出k,否则判断x的奇偶性, 奇数输出1, 偶数输出0


AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        int x=a%(b+1);
        if(b&1)
        {
            if(a&1)
                printf("1\n");
            else
                printf("0\n");
        }
        else
        {
            if(x<b)
            {
                if(x&1)
                    printf("1\n");
                else
                    printf("0\n");
            }
            else
                printf("%d\n", b);
        }
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/LSC_333/article/details/77655086