First enumerate kkk , first enumerate whenkkWhen k is an odd number, whenk = 1 k=1k=At 1 , obviouslynnWhen n is an odd number, the first mover will win. At this time, you need to take 1 step to get toPPP point.
When k = 3 k=3k=At 3 :
Subscript
0
1
2
3
4
5
6
7
8
9
status
P P P
N N N
P P P
N N N
P P P
N N N
P P P
N N N
P P P
N N N
Similarly when k = 5 k=5k=At 5 , the status table is the same as the above figure; it can be found that whenkkWhen k is odd,nn isalways satisfiedWhen n is odd, the first mover will win, and one step can lead toPPP point; vice versannWhen n is an even number, the first move is defeated.
When kkWhen k is even, drawk = 2 k=2k=The figure of 2 is as follows:
Subscript
0
1
2
3
4
5
6
7
8
9
status
P P P
N N N
N N N
P P P
N N N
N N N
P P P
N N N
N N N
P P P
Similarly draw when k = 4 k=4k=The state diagram at 4 is as follows:
Subscript
0
1
2
3
4
5
6
7
8
9
10
11
status
P P P
N N N
P P P
N N N
N N N
P P P
N N N
P P P
N N N
N N N
P P P
N N N
It can be found that when kkWhen k is an even number, start from 0,k + 1 k+1k+1 length is a cycle, wheren% (k + 1) = kn\%(k+1)=kn%(k+1)=When k isNNN points, the remaining0 to (k − 1) 0 to (k-1)0 to ( k−1 ) In the case of odd numbers.
So when n% (k + 1) = kn\%(k+1)=kn%(k+1)=When k , you need to walkkkk steps to the nextPPP point, otherwise asn% (k + 1) n\%(k+1)n%(k+1 ) When it is an odd number, one step can lead to the nextPPP point.
#include<bits/stdc++.h>
using namespace std;typedeflonglong ll;typedefunsignedlonglong ull;typedefunsignedint uii;typedef pair<int, ll> pii;
template<typename T>inlinevoidrd(T& x){
int tmp =1;char c =getchar(); x =0;while(c >'9'|| c <'0'){
if(c =='-')tmp =-1; c =getchar();}while(c >='0'&& c <='9'){
x = x *10+ c -'0'; c =getchar();}
x *= tmp;}constint N =2e5+10;constint M =1e7+10;constint mod =1e9+7;constint inf =0x3f3f3f3f;int n, m, k;intmain(){
#ifdef _DEBUG
FILE* _INPUT =freopen("input.txt","r",stdin);// FILE* _OUTPUT = freopen("output.txt", "w", stdout);#endif// !_DEBUGint cas =0, T =0;rd(T);while(T--){
// while (~scanf("%d %d", &n,&k)) {
rd(n),rd(k);if(k &1){
if(n &1)puts("1");elseputs("0");}else{
int tmp = n %(k +1);if(tmp == k)printf("%d\n", k);elseif(tmp &1)puts("1");elseputs("0");}}return0;}