博弈——Doubloon Game

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

题意:给两个数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;
}




马步将军问题(也称为骑士巡逻问题)是一个经典的组合数学问题,它要求找到一个马在棋盘上移动的路径,使得马能够按照国际象棋中马的移动规则访问棋盘上的每一个方格恰好一次。这是一个回溯算法的典型应用。 以下是使用Python语言编写的一个简单的马步将军问题解决方案的示例代码。此代码尝试找到一个马在8x8国际象棋棋盘上的一条路径,并打印出马的移动序列。请注意,这个问题的解决方案并不是唯一的,并且在不同大小的棋盘上可能没有解。 ```python # 定义8x8棋盘的移动增量 N = 8 move_x = [2, 1, -1, -2, -2, -1, 1, 2] move_y = [1, 2, 2, 1, -1, -2, -2, -1] # 初始化棋盘 def solve_kt(n): # 创建棋盘并初始化 sol = [[-1 for i in range(n)] for j in range(n)] # 马的第一个位置 x = 0 y = 0 sol[x][y] = 0 # 马的移动列表 move序号 = [0] # 从第一个位置开始,尝试所有可能的移动 if not solveKTUtil(n, x, y, move序号, move_x, move_y, sol): print("没有解决方案") else: print_solution(sol) # 递归函数来解决骑士巡逻问题 def solveKTUtil(n, curr_x, curr_y, move序号, move_x, move_y, sol): # 如果我们已经找到了N*N-1个移动,则成功 if move序号[0] == n*n: return True # 尝试所有下一个可能的移动 for i in range(8): next_x = curr_x + move_x[i] next_y = curr_y + move_y[i] if isSafe(next_x, next_y, sol, n): sol[next_x][next_y] = move序号[0] move序号[0] += 1 if solveKTUtil(n, next_x, next_y, move序号, move_x, move_y, sol): return True # 回溯 sol[next_x][next_y] = -1 move序号[0] -= 1 return False # 检查新位置是否是一个有效的未访问位置 def isSafe(x, y, sol, n): if x >= 0 and x < n and y >= 0 and y < n and sol[x][y] == -1: return True return False # 打印解决方案 def print_solution(sol): for i in range(N): for j in range(N): print(f"{sol[i][j]:2}", end=" ") print() # 主函数 def main(): solve_kt(N) if __name__ == "__main__": main() ``` 请注意,运行上述代码可能需要一些时间,因为它尝试了棋盘上所有可能的移动,并使用回溯法找到解决方案。回溯法是一种尝试方法,它逐步构建解决方案,并在发现当前构建的部分不可能导致最终解决方案时取消之前的部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值