LightOJ1364---Expected Cards(概率dp+三进制状压)

Taha has got a standard deck of cards with him. In addition to the 52 regular ones, there are 2 joker cards. Every regular card has a rank and a suit. The ranks in ascending order are: A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q and K. The suit of a card can be clubs, diamonds, hearts or spades. That means there are 13 clubs, 13 diamonds, 13 hearts and 13 spades - which adds up to 52. The joker cards have no ranks or suits.

One day, Sara gave Taha a challenge. First she randomly shuffles the 54 cards and starts placing one card after another, face-up, on a table. What is the expected number of cards Sara has to place so that there are at least C clubs, D diamonds, H hearts and S spades on the table? Whenever a joker card is encountered, Taha has to assign it to some suit so that the expected number of cards to reach the goal is minimized. The decision of assigning the joker card to some suit has to be made instantly (i.e. before Sara puts the next card on the table). Note that the assignments of the two joker cards don’t necessarily need to be the same.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing four integers in the order C, D, H and S. Each of these integers will be in the range [0, 15].

Output

For each case, print the case number first. Then output the expected number of cards Sara needs to place on the table to achieve the goal. If it’s impossible to reach the goal, irrespective of what assignments Sara opts for, output ‘-1’ (without the quotes) instead. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

4

0 0 0 0

15 13 13 13

1 2 3 4

15 15 15 15

Case 1: 0

Case 2: 54

Case 3: 16.3928186102

Case 4: -1

Notes

  1. For case 1, there is no need to place any card as all required values are 0
  2. For case 2, we must place all the 54 cards to reach the goal
  3. For case 3, note that output isn’t always an integer
  4. For case 4, 60 Cards? No way!!

    dp[i][j][k][l][m] 表示 目前有i张clubs,j张diamonds, k张hearts, l张spades, jokers的使用状态为m(三进制表示) 时,还需要牌数的期望值

/*************************************************************************
    > File Name: O.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月17日 星期日 16时47分15秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

double dp[16][16][16][16][82];
int C, D, H, S;
int bit[6];

bool judge(int a, int b, int c, int d, int e) {
    bit[0] = bit[1] = bit[2] = bit[3] = 0;
    int cnt = 0;
    while (e) {
        bit[cnt++] = e % 3;
        e /= 3;
    }
    a += bit[0];
    b += bit[1];
    c += bit[2];
    d += bit[3];
    if (a >= C && b >= D && c >= H && d >= S) {
        return 1;
    }
    return 0;
}

double dfs(int x, int y, int z, int w, int v) {
    double &res = dp[x][y][z][w][v];
    if (res != -1.0) {
        return res;
    }
    if (judge(x, y, z, w, v)) {
        return res = 0;
    }
    int e = v;
    res = 0;
    int num = 0;
    int cnt = 0;
    int bit[4];
    bit[0] = bit[1] = bit[2] = bit[3] = 0;
    bit[0] = v % 3; v /= 3; num += bit[0];
    bit[1] = v % 3; v /= 3; num += bit[1];
    bit[2] = v % 3; v /= 3; num += bit[2];
    bit[3] = v % 3; v /= 3; num += bit[3];
    int all = 54 - x - y - z - w - num;
    if (x < 13 && all) {
        double p = (13 - x) * 1.0 / all;
        res += (dfs(x + 1, y, z, w, e) + 1) * p;
    }
    if (y < 13 && all) {
        double p = (13 - y) * 1.0 / all;
        res += (dfs(x, y + 1, z, w, e) + 1) * p;
    }
    if (z < 13 && all) {
        double p = (13 - z) * 1.0 / all;
        res += (dfs(x, y, z + 1, w, e) + 1) * p;
    }
    if (w < 13 && all) {
        double p = (13 - w) * 1.0 / all;
        res += (dfs(x, y, z, w + 1, e) + 1) * p;
    }
    if (num < 2 && all) {
        double p = (2 - num) * 1.0 / all;
        v = (bit[0] + 1) + bit[1] * 3 + bit[2] * 9 + bit[3] * 27;
        double choice = dfs(x, y, z, w, v);
        v = bit[0] + (bit[1] + 1) * 3 + bit[2] * 9 + bit[3] * 27;
        choice = min(choice, dfs(x, y, z, w, v));
        v = bit[0] + bit[1] * 3 + (bit[2] + 1) * 9 + bit[3] * 27;
        choice = min(choice, dfs(x, y, z, w, v));
        v = bit[0] + bit[1] * 3 + bit[2] * 9 + (bit[3] + 1) * 27;
        choice = min(choice, dfs(x, y, z, w, v));
        res += (choice + 1) * p;
    }
    return res;
}

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        for (int i = 0; i <= 15; ++i) {
            for (int j = 0; j <= 15; ++j) {
                for (int k = 0; k <= 15; ++k) {
                    for (int l = 0; l <= 15; ++l) {
                        for (int p = 0; p <= 81; ++p) {
                            dp[i][j][k][l][p] = -1.0;
                        }
                    }
                }
            }
        }
        scanf("%d%d%d%d", &C, &D, &H, &S);
        int cnt = 0;
        cnt += (C - 13 > 0 ? C - 13 : 0);
        cnt += (D - 13 > 0 ? D - 13 : 0);
        cnt += (H - 13 > 0 ? H - 13 : 0);
        cnt += (S - 13 > 0 ? S - 13 : 0);
        if (cnt > 2) {
            printf("Case %d: -1\n", icase++);
            continue;
        }
        double res = dfs(0, 0, 0, 0, 0);
        printf("Case %d: %.12f\n", icase++, res);
    }
    return 0;
}
时间: 2024-12-06 10:06:41

LightOJ1364---Expected Cards(概率dp+三进制状压)的相关文章

HDU 3001 三进制状压DP

N个城市,M条道路,每条道路有其经过的代价,每一个城市最多能够到达两次,求走全然部城市最小代价,起点随意. 三进制状压.存储每一个状态下每一个城市经过的次数. 转移方程: dp[i+b[k]][k]=Min(dp[i+b[k]][k],dp[i][j]+dis[j][k]); #include "stdio.h" #include "string.h" const int inf=0x3f3f3f3f; int b[15],mark[60010][15],dp[60

三进制状压 HDOJ 3001 Travelling

题目传送门 题意:从某个点出发,所有点都走过且最多走两次,问最小花费 分析:数据量这么小应该是状压题,旅行商TSP的变形.dp[st][i]表示状态st,在i点时的最小花费,用三进制状压.以后任意进制状压都会了. #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 11; int dp[60000][10]; int bit[N]; int d[N][N]; int p[

三进制状压(涂抹果酱)

题意:https://www.acwing.com/problem/content/1067/ Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕. 蛋糕俯视图是一个 N×M 的矩形,它被划分成 N×M 个边长为 1×1×1 的小正方形区域(可以把蛋糕当成 N 行 M 列的矩阵). 蛋糕很快做好了,但光秃秃的蛋糕肯定不好看! 所以,Sam 要在蛋糕的上表面涂抹果酱. 果酱有三种,分别是红果酱.绿果酱.蓝果酱,三种果酱的编号分别为 1,2,3. 为了保证蛋糕的视觉效果,Admin 下

hdu 3001 Travelling 经过所有点(最多两次)的最短路径 三进制状压dp

题目链接 题意 给定一个\(N\)个点的无向图,求从任意一个点出发,经过所有点的最短路径长度(每个点至多可以经过两次). 思路 状态表示.转移及大体思路 与 poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp 相同. 但,因为是每个点 至多可以经过两次,所以应该用 三进制 来表示状态. 因为三进制不能直接通过移位来表示,所以要 预处理 出每个数字\(state\)的三进制表示中每一位\(i\)上的值\(dig[state][i]\). 注意

hdu 3001 Travelling TSP变形 三进制状压dp

// hdu 3001 TSP问题的变形 // 这次到每个点最多两次,所以可以用三进制的类推 // dp[S][u]表示当前在u点访问状态为S时所得到的最小的开销 // 采用刷表法,即用当前的状态推出它所能转移的状态 // dp[S][u] 可以到达的状态为dp[S+state[v]][v](dist[u][v]!=inf) // dp[S+state[v]][v] = max(dp[S+state[v]][v],dp[S][u]+dist[u][v]); // 其中每个点最多访问2次 // 技

HDU 3001 三进制 状压dp

Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3789    Accepted Submission(s): 1182 Problem Description After coding so many days,Mr Acmer wants to have a good rest.So travelling is

Travelling (三进制+状压dp)

题目链接 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 inline ll read(){ 5 int x=0,f=1;char ch=getchar(); 6 while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} 7 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch

HDU4336 Card Collector 概率DP求期望+状压

题目大意:要集齐N张卡片,每包干脆面出现每种卡片的概率已知,问你集齐N张卡片所需要的方便面包数的数学期望(N<=20). solution: 由于N<=20,我们可以考虑状压,设dp[S]表示牌的状态为S时的需要的方便面包数的数学期望. 那么,对于每一个状态,考虑枚举每一张牌i(摸到了i),此时: ① 如果S中不含i,dp[S]+=(dp[S|(1<<i-1)]+1)*p[i]. ② 如果S中已经包含i,那么算到下面的情况中去. 但是注意到,上述情况是已经保证了摸到牌,但是其实可以

Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]

题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5930 题意: 现有四支队伍两两打比赛,总共就是打六场比赛,每场比赛赢的队伍可得 $3$ 分,输的队伍得 $0$ 分,平局则两个队各得 $1$ 分. 现在给出四个队伍最终的积分