国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

Round D APAC Test 2017 Problem A. Vote (C++)

2019-11-08 01:33:09
字體:
供稿:網(wǎng)友

PRoblem

A and B are the only two candidates competing in a certain election. We know from polls that exactly N voters support A, and exactly M voters support B. We also know that N is greater than M, so A will win.

Voters will show up at the polling place one at a time, in an order chosen uniformly at random from all possible (N + M)! orders. After each voter casts their vote, the polling place worker will update the results and note which candidate (if any) is winning so far. (If the votes are tied, neither candidate is considered to be winning.)

What is the probability that A stays in the lead the entire time – that is, that A will always be winning after every vote?

Input

The input starts with one line containing one integer T, which is the number of test cases. Each test case consists of one line with two integers N and M: the numbers of voters supporting A and B, respectively.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the probability that A will always be winning after every vote.

y will be considered correct if y is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100. Small dataset

0 ≤ M < N ≤ 10. Large dataset

0 ≤ M < N ≤ 2000.

分析:

這道題目類似左括號(hào)右括號(hào)排列的問題。本來想用排列組合的方法來做,結(jié)果失敗了。還是用動(dòng)態(tài)規(guī)劃的方法來做了。因?yàn)橐坏┙o定M,N則返回結(jié)果也定了,因而可以對(duì)較大的M,N先將所有結(jié)果算好,然后需要時(shí)直接查表返回即可。因而開了數(shù)組dp[N][N],全部初始化為0,其中dp[i][j]即為M=j,N=i時(shí)的概率。由于顯然M==0時(shí)概率恒為1,所以第一列可以先確定都為1。遞推式為:
dp[i][j] = dp[i - 1][j] * (i/(i+j)) + dp[i][j - 1] * (j/(i+j));

解釋:

當(dāng)要求dp[i][j]時(shí):假設(shè)第(i+j)個(gè)人去投票,他可能投給A, 也可能投給B,因?yàn)橐阎衖個(gè)人投給了A,j個(gè)人投給了B,則最后這一票有i/(i+j)的概率投給了A,有j/(i+j)的概率投給了B。當(dāng)最后一票是投給A的時(shí)候,之前A一直勝的概率就為dp[i - 1][j];當(dāng)最后一票是投給B的時(shí)候,之前A一直勝的概率就為dp[i][j - 1]。合起來即為dp[i][j]。

最后只要直接查找相應(yīng)的位置即可得到每個(gè)測(cè)試用例的結(jié)果。

代碼:Github

#include <iostream>#include <cmath>#include <vector>#include <stack>#include <queue>#include <algorithm>using namespace std;typedef long long ll;errno_t err;FILE *f_input, *f_output;int T;const int N = 2001;double dp[N][N] = {0};int main() { for (int i = 1; i < N; i++) dp[i][0] = 1; for (int i = 1; i < N; i++) { for (int j = 1; j < i; j++) { double p1 = (double)i / (i + j); double p2 = 1 - p1; dp[i][j] = dp[i - 1][j] * p1 + dp[i][j - 1] * p2; } } //open files err = fopen_s(&f_input, "A-large-practice.in", "r"); err = fopen_s(&f_output, "out_put", "w"); //read fscanf_s(f_input, "%d", &T); for (int i = 1; i <= T; i++) { int a, b; fscanf_s(f_input, "%d %d", &a, &b); fprintf(f_output, "Case #%d: %.8f/n", i, dp[a][b]); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 蓝山县| 从江县| 茌平县| 肥乡县| 凉城县| 谷城县| 元江| 望城县| 运城市| 株洲县| 松原市| 九龙坡区| 密云县| 德昌县| 阳谷县| 高阳县| 澄城县| 广东省| 张家口市| 筠连县| 庆云县| 特克斯县| 长沙市| 延安市| 清镇市| 大名县| 和田县| 盈江县| 云龙县| 长岛县| 济南市| 铅山县| 文成县| 商洛市| 湖北省| 涟源市| 宝坻区| 富蕴县| 盈江县| 神农架林区| 玉林市|