11249-Game (Game)
Ultraviolet A 11249-Game
Question Link
Question: two piles of stones, a and B, each time can take a pile of any number, or two piles at the same time, but the absolute value difference cannot exceed k, and the last one cannot get lost, ask if the first hand can win
Idea: Assume that (a, B) Shi, a is a small pile, first it is easy to see that (1, k + 2) is defeated, set the next one to (2, x) If this status can reach (1, k + 2), it will win, and (2, x) will fail, it must be a pile of stones + k + 2-1 in the previous State. In this way, no matter how it is obtained, it cannot be changed to (1, k + 2 ), the other hand gets one from the first hand, so we can pre-process one by one to get a defeat. Then, we can directly judge each inquiry.
Code:
#include
#include
#include using namespace std;const int N = 100005;int t, k, q, a, b;int lose[N];void init(int k) { memset(lose, 0, sizeof(lose)); lose[1] = 1 + k + 1; lose[1 + k + 1] = 1; int pre = 1; for (int i = 2; i <= 100000; i++) {if (lose[i]) continue;int tmp = lose[pre] + i - pre + k + 1;if (tmp > 100000) break;pre = i;lose[i] = tmp;lose[tmp] = i; }}int main() { scanf("%d", &t); while (t--) {scanf("%d%d", &k, &q);init(k);while (q--) { scanf("%d%d", &a, &b); if (a > b) swap(a, b); if (lose[a] == b) printf("LOSING\n"); else printf("WINNING\n");}printf("\n"); } return 0;}