hdu 4203 Doubloon Game (SG规律)

Doubloon Game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 578 Accepted Submission(s): 324

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

题意:给你 S 个石子和一个数字k,每次只能取 k 的幂次的石子数,如:1,k,k2...,谁先取完谁赢,问你最少取多少个可以获得胜利,即可以使对手面临必败面。

思路:如果数据量小的话,可以直接求其 sg 值,但是现在数据范围为 1e9 ,那么就不能那么暴力的写了,我们可以通过打表来找到规律,我们可以根据 k 的值来进行打表,会发现:
k为奇数的时候, sg 值会按照0,1这样的循环节
k 为偶数的时候,我们列举一下循环节:
k=2:1,2,0
k=4:1,0,1,2,0
k=6:1,0,1,0,1,2,0

就可以发现规律了,然后就可以判断当前局面能否到达必败面,如果可以,那么我们可以枚举选取的石子数量,时间复杂度是允许的.

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-10-10.39 Wednesday
File Name    : D:\MyCode\2016-8月\2016-8-10.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int sg[1010],vis[1010];

void getSG()
{
    sg[0]=0;
    for(int i=1;i<=1000;i++)
    {
        mem(vis,0);
        for(int j=1;j<=i;j*=6)
            vis[sg[i-j]]=1;
        for(int j=0;;j++)
        if(!vis[j])
        {
            sg[i]=j;
            break;
        }
    }
}

int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,k;cin>>n>>k;
        if(k%2)
        {
            if(n%2) printf("1\n");
            else printf("0\n");
        }
        else
        {
            int c=n%(k+1);
            if(c<=k-1)
            {
                if(c%2) printf("1\n");
                else printf("0\n");
            }
            else
            {
                int flag=0;
                for(int j=k;j<=n;j*=k)
                {
                    int x=n-j;
                    c=x%(k+1);
                    if(c%2==0)
                    {
                        flag=j;
                        break;
                    }
                }
                if(flag) printf("%d\n",flag);
                else printf("0\n");
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值