In poj3922, hdu2486, and hdu2580 games, the average person cannot think of it for five hours !!

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/cxb569262726/article/details/7841521

Question link:

Http://poj.org/problem? Id = 3922

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2486

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2580

A simple stone game

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 174 accepted submission (s): 79


Problem descriptionafter he has learned how to play Nim game, Mike begins to try another stone game which seems much easier.

The game goes like this: two players start the game with a pile of N stones. they take stones from the pile in turn and every time they take at least one stone. the one who goes first can take at most n-1 stones for his first move. from then on a player can
Take at most K times as your stones as his opponent has taken last time. for example, if one player takes m stones in his turn, then the other player can take at most k × m stones next time. the player who takes the last stone wins the game. suppose that those
Two players always take the best moves and never make mistakes, your job is to find out who will definitely win the game.


Inputthe first line contains a integer T, indicating that there are t test cases following. (T <= 20 ).

Each test case is a line consisting of two integer N and K. (2 <= n <= 10 ^ 8, 1 <= k <= 10 ^ 5 ).


Outputfor each test case, output one line starting with "Case N:", n is the case number. and then, if the first player can ensure a winning, print the minimum number of stones he shoshould take in his first turn. otherwise, print "lose ". please note that there is
Blank following the colon.


Sample inpu

516 111 132 234 219 3
 


Sample output

Case 1: loseCase 2: 1Case 3: 3Case 4: loseCase 5: 4

Given integers N and K. There are n stones for two people to fetch. The first hand can take 1 ~ N-1. Next, you can get up to K times of the previous person each time, that is, 1 ~ K * n; could you win the first hand! You can win and output at least a few results for the first time. Otherwise, the lose is output.

PS: I think the reason for this abnormal question is: no one made it during the competition this morning. It seems that no one will do it at night. Unfortunately, I won the lottery. I will explain this question .... O m g !! Refer to the online code for a long time! Finally, I came up with it. It is a pity that the preparation is not sufficient and it cannot be clearly stated...

Thought: Find the rule and then play the game. There is also the idea of DP...

The following code and solutions are provided:

# Include <iostream> # include <algorithm> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <queue> # include <math. h >#include <set ># include <vector> # define maxn 15 # define INF 1000 using namespace STD; int no [2000000], maxok [2000000]; // No stores the number of lose. Maxok [I] stores the largest number that can be formed before I; int main () {int I, j, n, m, K, T, C, CA = 0; For (scanf ("% d", & T); t --;) {ca ++; scanf ("% d", & N, & K); If (n <= k + 1) // n <= k + 1 must be input; {printf ("case % d: lose \ n ", CA); continue;} int x = 0, y = 0; no [0] = maxok [0] = 1; while (no [x] <n) {x ++; no [x] = maxok [x-1] + 1; // before X, the maximum number of can match is maxok [x-1], after adding 1 does not match. While (no [Y + 1] * k <no [x]) // find the maximum number smaller than no [X]. In this example, if k = 3, 15 times get 4 Y ++; if (no [y] * k <no [x]) // The preceding number may not meet the condition maxok [x] = No [x] + maxok [y]; // if no [x] + maxok [y] is met, 15 + maxok [3] elsemaxok [x] = No [x] is pushed in this example. // otherwise, the maximum number is itself.} If (no [x] = N) // if n is composed, this number belongs to the No array. That is, it cannot be composed of numbers that meet the conditions. Lose! {Printf ("case % d: lose \ n", CA); continue;} int ans; while (n) {If (n> = No [x]) // subtract the largest number continuously, and the smallest number is left. {Ans = No [X]; n-= No [X];} X --;} printf ("case % d: % d \ n", CA, ANS );} return 0;}/* k = 3: No = {, maxok = */

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.