11249-Game (Game)

Source: Internet
Author: User

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;}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.