"HDU1538" A Puzzle for Pirates (classic pirate question)

Source: Internet
Author: User
Tags integer numbers

Topic

Description

A Bunch of pirates has gotten their hands on a hoard of gold pieces and wish to divide the loot. They is democratic pirates in their own, and it's their custom to make such divisions in the following manner:the F Iercest Pirate makes a proposal about the division, and everybody votes on it, including the proposer. If percent or more is in favor, the proposal passes and is implemented forthwith. Otherwise the proposer is thrown overboard, and the procedure are repeated with the next fiercest pirate.
All the pirates enjoy throwing one of their fellows overboard, but if given a choice they prefer cold, hard cash, the more The better. They dislike being thrown overboard themselves. All pirates is rational and know the other pirates is also rational. Moreover, no pirates is equally fierce, so there is a precise pecking order―and it's known to them all. The gold pieces is indivisible, and arrangements to share pieces is not permitted, because no pirate trusts he Fellows To stick to such an arrangement. It ' s every man for himself. Another thing about pirates was that they was realistic. They believe ' a bird in the hand are worth the bush ' which means they prefer something that's certain than take a R ISK to get more, where they might lose everything.

For convenience, number the Pirates with order of meekness, so and the least fierce is number 1, the next least fierce numb Er 2 and so on. The fiercest pirate thus gets the biggest number, and proposals proceed in the order from the biggest to the least.

The secret to analyzing all such games of strategy are to work backward from the end. The place-to-start is the "point" at which the game gets down to just of the pirates, P1 and P2. Then add in Pirate P3, P4, ..., one by one. The illustration shows the results when 3, 4 or 5 pirates a try to divide of pieces of gold.



Your task is to predict how many gold pieces a given pirate would get.

Input

The input consists of a line specifying the number of testcases, followed by one line per case with 3 integer numbers n, m , p. N (1≤n≤10^4) is the number of pirates. M (1≤m≤10^7) is the number of the gold pieces. P (1≤p≤n) indicates a pirate where P = n indicates the fiercest one.

Output

The output for each case consists of a single integer which is the minimal number of gold pieces pirate p can get. For example, if pirate P can get 0 or 1 gold pieces, output ' 0 '. If Pirate p would be thrown overboard, output ' thrown '.

Sample Input

33 100 24 100 25 100 5

Sample Output

0198
"Test Instructions" This is a classic problem, there are n pirates, cent m block of gold, which they will be in a certain order to propose their own allocation scheme, if more than 50% of the people in favor of, then the scheme through, the beginning of gold, if not, then put the proposal to throw into the sea, the next person to continue. "Analysis" oneself from small to big play once this game, can find some laws.

Two cases of bribery and bribery are not enough. Catch characteristics: If a person will die, he will support the people in front to ensure that they do not die. If he can get more money, he will be more supportive. If he gets the same money, he likes to throw the people in front of him. For

Enough money to bribe the situation, he will give himself with the odd couple. This is enough votes, and he will support you. (specifically, why think about it). For not enough money to bribe, to take into account that people do not want to die this situation, to find the law, policymakers always

2*m+2^k (k is any integer), but he specifically bribes who is unsure, so except for some in front of the mortal person, the other people's ans are 0.

The specific view of the great God blog:http://blog.csdn.net/acm_cxlove/article/details/7853916

The code is as follows:

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 #defineMAXN 10108 9 voidFfind (intXintYintz)Ten { One     if(x<=2*y+1) A     { -         if(z==x) printf ("%d\n", Y (x1)/2); -         Else if((x-z)%2==0) printf ("1\n"); the         Elseprintf"0\n"); -     } -     Else -     { +         intMX; -          for(intI=0;(2*y) + (1<<i) <=x;i++) mx=2*y+ (1<<i); +         if(Z&GT;MX) printf ("thrown\n"); A         Elseprintf"0\n"); at     } - } -  - intMain () - { -     intT; inscanf"%d",&T); -      while(t--) to     { +         intn,m,p; -scanf"%d%d%d",&n,&m,&p); the Ffind (n,m,p); *     } $     return 0;Panax Notoginseng}
[HDU1538]

2016-04-25 13:21:52

"HDU1538" A Puzzle for Pirates (classic pirate question)

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.