UVa 12525 Boxes and Stones (DP game)

Source: Internet
Author: User
Tags rounds

Boxes and Stones


Paul and Carole like-to-play a game withSStones andBboxes numbered from 1 toB. Beforebeginning the game they arbitrarily distribute theSStones among the boxes from 1 to B -1, LeavingboxBEmpty. The game then proceeds by rounds. At each round, first Paul chooses a subsetPOf the stones that is in the boxes; He may choose as many stones as he wants from as many boxes as he wants, or he may choose no stones at all, in which casePis empty. Then, the Carole decides what to Donext:she can eitherPromoteThe subsetPandDiscardThe remaining stones (that's, those stones Notchosen by Paul in the first step); Or she mayDiscardThe subsetPandPromoteThe remaining stones.

To promote a given subset means to take each stone in this subset and move it to the box with thenext number in S Equence, so and if there was a stone in this subset inside boxb, it's moved to boxb + 1. Todiscard a given subset means to remove every stone in the subset from its corresponding box, so that those sto NES is not a used in the game for the remaining rounds. The figure below shows an example of the first and the rounds of a game.

Paul and Carole play until at least one stone reaches box numberB, in which case Paul wins the game, or until the Re is no more stones left in the boxes, in which case Carole wins the game. Paulis a very rational player, but Carole is a worthy rival because she's not only extremely good at the this game, but also Quite lucky. We would like to know who's the best player, but before that we must first understand how the outcome of a game depends O n the initial distribution of the stones. In Particular,we would like to know on how many ways theS stones can initially be distributed among the first B -1 boxes So Carole can is certain that she can win the game if she plays optimally, even if Paul NE Ver makes a mistake.


Input

Each test case was described using one line. The line contains integersS (1s) and B(2b), Representing respectively the number of stones and the number of boxes in the game.


Output

For each test case output a line with an integer representing the number of ways in which theS stonesmay be distr Ibuted among the first B -1 boxes so Carole was certain that she can win the game. Because This number can is very large, you is required to output the remainder of dividing it by109 + 7.


Sample Input

2 38 442 42

Sample Output

20498467348


Title Link: Https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=441&page=show_ problem&problem=3970


The main idea: P and C do the game, have s a stone and B box, they put s a stone casually in 1~b-1 these boxes, p began to choose some stones from the 1~b-1, C can decide to throw away the stone p selected to move the remaining stones to the right one, or the P-selected stones to the right one, The others are thrown away, once there is a stone to the B box, then p wins, if there is no stone, C wins, ask how many kinds of initial position is C of the winning state

Analysis: Set DP[I][J][K] for the first box, with J Stone, c after the decision of the first box in the number of stones to K to win the number of States, off-line calculation of all circumstances, O (1) query, the first analysis of the optimal strategy p, P's goal is as much as possible to let the stone right, but the choice in C Therefore, the optimal strategy of P is half as much as possible, if in this case p still lose, then C is the winning state. DP[I][J][K] state shifted by three parts

DP[I][J][K] = dp[i][j-1][k-1] + dp[i-1][j][2 * k] + dp[i-1][j][2 * k + 1]


#include <cstdio> #include <cstring> #define LL long longusing namespace Std;int Const MOD = 1e9 + 7; int const MAX = 105;ll dp[max][2 * max][4 * max];void pre () {    dp[0][0][0] = 1;       for (int i = 1; I <=, i++) for        (int j = 0; J <=; J + +) for                (int k = 0; k <=; k++)                Dp[i][j][k] = (Dp[i][j-1][k-1] + dp[i-1][j][k * 2] + dp[i-1][j][k * 2 + 1])% MOD;} int main () {      pre ();    int s, b;    while (scanf ("%d%d", &s, &b)! = EOF)            printf ("%lld\n", Dp[b][s][0]);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVa 12525 Boxes and Stones (DP game)

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.