Game | |
Input: Standard Input Output: Standard Output |
|
Alice and Bob are playing a 2 player game. The game has the following rules.
- It consists of 2 stone piles containing a and b number of stones.
- In each turn a player can take stones from a single pile or both pile. If she takes stones from 1 pile then she can take up to all the stones from that pile. If she wants to take stones from both piles than the absolute difference between the number of stones taken from each pile can be at most k. In both cases she should take at least one stone.
- The player who takes the last stones wins.
You are given the information about the game. Alice is the first player. You have to determine whether she will win or lose.
Input
First line of the input contains T (1≤T≤10) the number of test case. Then following lines contains T Test cases.
Each case starts with line containing 2 integers k (1≤k≤20) and q (1≤q≤10000). Each of the next q lines will contains 2 integers a and b (1≤a,b≤100000). Each of these a and b pair along with the initial k will be instance of a game.
Output
For each game output will be a single line containing a stringing “WINNING” if Alice can win that game or “LOSING” if she can not win that game. Output a blank line after each test case.
Sample Input Output for Sample Input
1 1 4 2 5 2 6 1 3 1 4
| WINNING LOSING LOSING WINNING
|
Problemsetter: Abdullah al Mahmood
Special Thanks: Derek Kisman
找规律的问题。从小的必败局面推更大的。每个样例后面要输出一空行!
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 100000 + 5;
const int INF = 1000000000;
int f[maxn*100];
void pre(int k){
memset(f, -1, sizeof f);
f[1] = k+2;
int last = 1;
f[k+2] = 0;
for(int i = 2;i < maxn;i++){
if(f[i] == 0) continue;
f[i] = f[last]+k+1+i-last;
last = i;
f[f[i]] = 0;
}
}
int main(){
int t;
cin >> t;
while(t--){
int k,q;
cin >> k >> q;
pre(k);
while(q--){
int a, b;
cin >> a >> b;
if(a > b) swap(a, b);
if(f[a] == b){
cout << "LOSING" << endl;
}
else{
cout << "WINNING" << endl;
}
}
cout << endl;
}
}