LightOJ1364---Expected Cards(확률 dp+3진수형)
14208 단어 dp
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
/************************************************************************* > 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
", icase++);
continue;
}
double res = dfs(0, 0, 0, 0, 0);
printf("Case %d: %.12f
", icase++, res);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【경쟁 프로 전형적인 90문】008의 해설(python)의 해설 기사입니다. 해설의 이미지를 봐도 모르는 (이해력이 부족한) 것이 많이 있었으므로, 나중에 다시 풀었을 때에 확인할 수 있도록 정리했습니다. ※순차적으로, 모든 문제의 해설 기사를 들어갈 예정입니다. 문자열...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.