UVa 12525 Boxes and Stones (dp 博弈)
Boxes and Stones
Paul and Carole like to play a game with
S stones and B boxes numbered from 1 to
B. Beforebeginning the game they arbitrarily distribute the
S stones among the boxes from 1 to
B - 1, leavingbox B empty. The game then proceeds by rounds. At each round, first Paul chooses a subset
P
To promote a given subset means to take each stone in this subset and move it to the box with thenext number in sequence, so that if there was a stone in this subset inside box b, it is moved to boxb + 1. To discard a given subset means to remove every stone in this subset from its corresponding box, so that those stones are not used in the game for the remaining rounds. The figure below shows an example of the first two rounds of a game.
Paul and Carole play until at least one stone reaches box number B, in which case Paul wins the game, or until there are 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 is not only extremely good at this game, but also quite lucky. We would like to know who is the best player, but before that we must first understand how the outcome of a game depends on the initial distribution of the stones. In particular,we would like to know in how many ways the S stones can initially be distributed among the first B - 1 boxes so that Carole can be certain that she can win the game if she plays optimally, even if Paul never makes a mistake.
Input
Each test case is described using one line. The line contains two integers S (1S200) and B(2B100), 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 the S stonesmay be distributed among the first B - 1 boxes so that Carole is certain that she can win the game.Because this number can be very large, you are required to output the remainder of dividing it by109 + 7.
Sample Input
2 3
8 4
42 42
Sample Output
2
0
498467348
題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=441&page=show_problem&problem=3970
題目大意:P和C做遊戲。有S個石頭和B個盒子,他們把S個石頭隨意的放在1~B-1這些盒子中,P開始從1~B-1中隨意的挑選一些石頭。C能夠決定把P選的石頭丟掉讓剩下的石頭都往右移一格,或者把P選的石頭都往右移一個。其它的都丟掉。一旦有一個石頭到B盒子中。則P勝。若沒有石頭了則C勝,問有多少種初始的擺放狀態為C的必勝態
題目分析:設dp[i][j][k]為到第i個盒子。用了j個石頭,C做完決定後第i個盒子裏的石頭數為k的必勝態個數。離線計算出全部情況。O(1)查詢,先分析P的最優策略,P的目標是盡量多的讓石子右移。但是選擇權在C手上,因此P的最優策略是盡量一半一半的取,假設在這樣的情況下P還是輸,那麽就是C的必勝態了。dp[i][j][k]狀態由三部分轉移
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 long using 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 <= 100; i++) for(int j = 0; j <= 200; j++) for(int k = 0; k <= 200; 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]); }
UVa 12525 Boxes and Stones (dp 博弈)