hdu 4958(博弈论+概率DP)

Poor Rukaw

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 223    Accepted Submission(s): 102


Problem Description
Last time, Hanamichi lost the basketball battle between him and Rukaw. So these days he had a burning desire to wreak revenge. So he invented a new game and asked Rukaw to play with him. What’s more, the loser should confess his ignorance and on the other hand the winner can go on a trip with Haruko.

Hanamichi knows the game well (as he invented it), and he always chooses the best strategy, however Rukaw is not so willing to play this game. He just wants it to be finished as soon as possible so that he can go to play basketball. So he decides not to think about the best strategy and play casually.

The game’s rules are here. At first, there are N numbers on the table. And the game consists of N rounds. Each round has a score which is the number of the numbers still left on the table. And Each round there will be one number to be removed from the table. In each round, two players take turns to play with these numbers.To be fair, Rukaw plays first in the first round. If there’s more than 1 numbers on the table, players can choose any two numbers they like and change them to a number abs(x-y). This round ends when there’s only one number left on the table, and if this number is an odd number, Rukaw wins, otherwise Hanamichi wins. The score of this round will be add to the winner. After that, all numbers will be recovered to the state when this round starts. And the loser of this round has the right to remove one number and he also has the right to play first in the next round. Then they use the remaining numbers to start next round. After N rounds, all numbers removed and this game ends. The person who has more scores wins the whole game.

As you know, Rukaw has already decided to play casually, that is to say, in his turn, he chooses numbers randomly, each numbers left on the table has the same possibility to be chosen. When a round ends, if Rukaw is the loser, he also randomly chooses a number to remove. And Hanamichi will always choose numbers or remove numbers to maxmium his final total score. Here comes the question:
Given the N numbers on the table at the beginning, can you calculate the expectation of the final score of Hanamichi. (We don’t care about who wins the whole game at all.)
 
Input
This problem contains multiple tests.
In the first line there’s one number T (1 ≤ T ≤ 200) which tells the total number of test cases. For each test case, there a integer N (1 ≤ N ≤ 1000) in the first line, and there are N intergers Ai , i = 1, 2, … , N (1 ≤ Ai ≤ 100000), in the second line which are the numbers at the beginning.
 
Output
This problem is intended to use special judge. But so far, BestCoder doesn’t support special judge. So you should output your answer in the following way. If the expectation you get is X, output [3×X+0.5] in a line. Here, [A] means the largest integer which is no more than A.
 
Sample Input
2
2
2 4
2
1 2
 
Sample Output
9
3
 
析:桌子上有n个数,两个玩家甲和乙进行n轮游戏,每轮游戏的分数为该轮数字个数,甲随便玩,乙采取最优策略,当桌面数字多于2个时,玩家可任选两个数进行|(X-Y)|运算,并将结果放入桌面剩下的数中,一直到只剩下一个数,为奇数,甲赢,否则乙赢,输的选手可在下一轮开始时从桌面任意移除一个数字并获得优先进行游戏的权力,问最终乙的得分期望
  对甲来说,最后一轮为奇数时是必胜的,对于每一轮的转移,有 拿走两个奇数变为偶数,一奇一偶变奇数,两偶数变偶数,可知奇数的变化只能为0或2,所以如果开始有奇数个奇数,那么甲一定获胜
  因此,判断每一轮中奇数个数,若当前轮奇数个数为奇数,那么乙输,一定会拿走一个奇数,改变当前状态,否则甲输,随机选取,即求选取奇数和偶数的概率
 
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 1005;
int t, n, x;
double dp[N][N];
double dfs(int o, int e){
    if(o+e <= 0)
        return 0;
    if(dp[o][e])    //剪枝
        return dp[o][e];
    if(o&1)         // 奇数个数为奇数,Hanamichi输,拿走一个奇数
        return dp[o][e] = dfs(o-1, e);
    if(o){
        if(e)       // 奇数个数为偶数,Rukaw输,随机拿走一个
            return dp[o][e] = (o*dfs(o-1, e)+e*dfs(o, e-1))/(o+e)+o+e;
        else
            return dp[o][e] = dfs(o-1, e)+o;
    }
    return dp[o][e] = dfs(o, e-1)+e;
}
int main(){
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        int odd = 0, even = 0;
        memset(dp, 0, sizeof(dp));
        for(int i = 0; i < n; i ++){
            scanf("%d", &x);
            if(x&1)
                odd ++;
            else
                even ++;
        }
        int res = 3*dfs(odd, even)+0.5;
        printf("%d
",res);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/microcodes/p/12876961.html