D. 1-2-K Game

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).

Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can't make a move loses the game.

Who wins if both participants play optimally?

Alice and Bob would like to play several games, so you should determine the winner in each game.

Input

The first line contains the single integer T (1 ≤ T ≤ 100) — the number of games. Next T lines contain one game per line. All games are independent.

Each of the next T lines contains two integers n and k (0 ≤ n ≤ 109, 3 ≤ k ≤ 109) — the length of the strip and the constant denoting the third move, respectively.

Output

For each game, print Alice if Alice wins this game and Bob otherwise.

Example

inputCopy

4

0 3

3 3

3 4

4 4

outputCopy

Bob

Alice

Bob

Alice

题意:

当前在n位置,每一次可以向左走1,2,或者k步,最左的位置是0,不能走到0的左边, 二人博弈问题,谁没法再走的时候就输掉,问先手必赢还是后手必赢。

思路:

首先确定的是 0位置是必输位置,因为 1 2 和k这三个位置可以一步就走到0位置,所以这3个位置是必赢位置,以此规律,我们可以递推出sg函数。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
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;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int sg[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\code_stream\\out.txt","w",stdout); int n,k;
n=100;
cin>>k;
sg[0]=0;
sg[1]=1;
sg[2]=1;
repd(i,3,n)
{
if((i-k)>=0)
{
if(sg[i-1]==0||sg[i-2]==0||sg[i-k]==0)
{
sg[i]=1;
}
}else
{
if(sg[i-2]==0||sg[i-1]==0)
{
sg[i]=1;
}
}
}
repd(i,0,n)
{
cout<<i<<" "<<sg[i]<<endl; } return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
通过sg函数打表分析可以得出, 有以下规律
如果k是3的倍数,那么sg函数是k+1长度的循环节,对循环节取模后,判断n是否是k,如果是k,那么k位置必赢,否则判断是否是3的倍数。
如果k不是3的倍数,那么判断n是否是3的倍数即可。 比赛时看错表,推出个错规律,wa了3次,深夜写这个博客算给自己一个记性吧。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
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;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\code_stream\\out.txt","w",stdout); int t;
ll n, k;
gbtb;
cin >> t;
while (t--)
{
cin >> n >> k;
if (k % 3 == 0)
{
n %= (k + 1);
if (n == k)
{
cout << "Alice" << endl;
} else
{
if (n % 3 == 0)
cout << "Bob" << endl;
else
cout << "Alice" << endl;
}
// return 0;
continue;
} if ((n % 3) == 0 )
{
cout << "Bob" << endl;
} else
{
cout << "Alice" << endl;
}
} return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Educational Codeforces Round 68 (Rated for Div. 2) D. 1-2-K Game (博弈, sg函数,规律)的更多相关文章

  1. Educational Codeforces Round 68 (Rated for Div. 2)---B

    http://codeforces.com/contest/1194/problem/B /* */ # include <bits/stdc++.h> using namespace s ...

  2. Educational Codeforces Round 68 (Rated for Div. 2)补题

    A. Remove a Progression 签到题,易知删去的为奇数,剩下的是正偶数数列. #include<iostream> using namespace std; int T; ...

  3. Educational Codeforces Round 68 (Rated for Div. 2) C. From S To T (字符串处理)

    C. From S To T time limit per test1 second memory limit per test256 megabytes inputstandard input ou ...

  4. Educational Codeforces Round 68 (Rated for Div. 2)D(SG函数打表,找规律)

    #include<bits/stdc++.h>using namespace std;int sg[1007];int main(){ int t; cin>>t; while ...

  5. Educational Codeforces Round 68 (Rated for Div. 2)-D. 1-2-K Game

    output standard output Alice and Bob play a game. There is a paper strip which is divided into n + 1 ...

  6. Educational Codeforces Round 68 (Rated for Div. 2)-C-From S To T

    You are given three strings ss, tt and pp consisting of lowercase Latin letters. You may perform any ...

  7. Educational Codeforces Round 90 (Rated for Div. 2) B. 01 Game(字符串博弈)

    题目链接:https://codeforces.com/contest/1373/problem/B 题意 给出一个二进制串 $s$,Alica 和 Bob 每次可以选择移去 $s$ 中的一个 $10 ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) B. Game with Telephone Numbers 博弈思维+模拟+贪心思维

    题意:博弈题面  给出一个数字序列 (>=11)  有两个人任意删除数字 直到 数字只剩下11位 如果删除后的数字串开头是8那么就是第一个赢 否则就是第二个人赢 第一个人先手  数字序列一定是奇 ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. 自动轮播切换tab

    一个项目中需要用到类似的功能,自己手写一个轮播切换,不足之处见谅.代码如下 <!DOCTYPE html> <html lang="en"> <hea ...

  2. WKWebView使用指南|功能丰富的JXBWKWebView

    github地址:JXBWKWebView,如果觉得项目不错可以点个star支持一下,谢谢~ 前言 目前iOS系统已经更新到iOS11,大多数项目向下兼容最多兼容到iOS8,因此,在项目中对WebVi ...

  3. IView入门练习~CDN模式全局加载JS

    关于 iView iView 是一套基于 Vue.js 的开源 UI 组件库,主要服务于 PC 界面的中后台产品. 特性 高质量.功能丰富 友好的 API ,自由灵活地使用空间 细致.漂亮的 UI 事 ...

  4. Linux_LEMP

    目录 目录 LEMP Nginx mysql PHP php-fpm Script LEMP Nginx是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器,因它的稳 ...

  5. webpack中的 chunk,module,bundle的区别,以及hidden modules是什么

    hidden modules是什么: chunk,module,bundle的区别 总结: module是指任意的文件模块,等价于commonjs中的模块 chunks是webpack处理过程中被分组 ...

  6. 阶段3 1.Mybatis_02.Mybatis入门案例_3.mybatis注解开发和编写dao实现类的方式

    注解的用法 直接创建一个新的项目 下一步直接next 然后finish即可 把之前项目01里面的代码直接复制过来 复制到我们02的注解的工程中 把01项目导入的依赖也都粘贴过来 再把测试类复制过去 复 ...

  7. 举例讲解Python中的死锁、可重入锁和互斥锁

    举例讲解Python中的死锁.可重入锁和互斥锁 一.死锁 简单来说,死锁是一个资源被多次调用,而多次调用方都未能释放该资源就会造成死锁,这里结合例子说明下两种常见的死锁情况. 1.迭代死锁 该情况是一 ...

  8. Jmeter 后置处理器--jp@gc - JSON/YAML Path Extractor & JSON Extractor

    后置处理器--jp@gc - JSON/YAML Path Extractor 1.需要下载插件,地址: 解压后把对应jar包放置对应的lib和lib/ext目录下,重启Jmeter: 2.在需要提取 ...

  9. idea的掌握

    1:idea的界面了解(一般都会勾选这两项,编码比较方便) 2: 如何配置sdk(jdk,最后一个生成的是.class文件的位置) 3: 如何单个项目配置和全局配置 4:如何配置项目的jdk编译版本和 ...

  10. 复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中

    cp -r /etc/p*[a-Z] /tmp/mytest1