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 of the stones that are 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 case
P is empty. Then, Carole decides what to donext: she can either
promote the subset P and
discard the remaining stones (that is, those stones notchosen by Paul in the first step); or she may
discard the subset P and
promote the 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 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]);
}
时间: 2024-10-14 07:24:05

UVa 12525 Boxes and Stones (dp 博弈)的相关文章

UVA - 1500 Alice and Bob (dp+博弈)

Description Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. the most amazing thing is that they always find the best strategy, and that's why they feel bored again and again. They just invented a new ga

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

UVa 12657 Boxes in a Line(双向链表的应用)

Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th

uva 12186 Another Crisis 树形dp

// uva 12186 Another Crisis 树形dp // // 对于一个节点u,有k个子节点,则至少有c = (k * T - 1) / 100 + 1才能 // 发信,即c / k >= T / 100,则 c 的值为 k * T /100,上取整变成上式 // 将所有的子节点d从小到大排序,取前c个就是d[u]的值 // 紫书上的一题,之前看了好久好久,觉得挺好的,然而一直没做,今天就来 // 体验体验,挺好的一题,注意一下,如果一个节点是叶节点,直接return 1就好 //

UVA 1399 - Puzzle(AC自动机+DP)

UVA 1399 - Puzzle 题目链接 题意:给定一些字符串,求一个最长的不在包含这些子串的字符串,如果可以无限长输出No 思路:建ACM自动机,把不可走结点标记构造出来,然后在这个状态图上进行dp找出最长路径即可,至于无限长的情况,只要在dp前进行一次dfs判有没有环即可 代码: #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include &

uva 417 - Word Index(数位dp)

题目连接:uva 417 - Word Index 题目大意:按照题目中的要求,为字符串编号,现在给出字符串,问说编号为多少,注意字符串必须为递增的,否则编号为0. 解题思路:其实就是算说比给定字符串小并且满足递增的串由多少个.dp[i][j]表示第i个位为j满足比给定字符串小并且满足递增的串. dp[i][j]=∑k=0j?1dp[i?1][k]. 注意每次要处理边界的情况,并且最后要加上自身串.并且在处理边界的时候dp[i][0]要被赋值为1,代表前i个为空的情况. #include <cs

UVA 10641 - Barisal Stadium(DP + 几何)

题目链接:10641 - Barisal Stadium 题意:逆时针给定n个点,在给m个灯,每个灯有一个花费,要求最小花费使得所有边能被灯照到 思路:用向量叉积判断向量的顺逆时针关系,从而预处理出每个灯能照到的边,然后由于n个点是环的,所以可以直接扩大两倍,dp时候去枚举起点即可 状态为dp[i]表示现在照到i条边之前的边全部照亮需要的最小花费 代码: #include <stdio.h> #include <string.h> const double eps = 1e-6;

UVA 14000 Lighting System Design(DP)

You are given the task to design a lighting system for a huge conference hall. After doing a lot of calculation & sketching, you have figured out the requirements for an energy-efficient design that can properly illuminate the entire hall. According

UVA 10003 Cutting Sticks(区间dp)

Description  Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they onl