题目链接:https://vjudge.net/problem/LightOJ-1274
Time Limit: 4 second(s) | Memory Limit: 32 MB |
You are in a contest, and unfortunately you don't have much time. You have one problem in hand; you just glanced at the sample output and found that it just wants 'YES' or 'NO'. So, you have made another plan instead of solving the problem as you know the system very well.
For this problem, every test case is stored in a separate file. When a submission is found, the system successively runs the solution on all tests of a problem, and for each test the checking process goes as follows. The input is copied to the file input.txt. Then the solution is launched. It reads the input from the file input.txt and writes the result to the file output.txt. When it finishes, the correct answer is copied to the file answer.txt. If the contents of the files answer.txt and output.txt match, the test is assumed to be passed; otherwise, the test is not passed.
So, you decided to write a program that would operate as follows. If the folder containing the program doesn't contain the file answer.txt (i.e. the program is run on the first test), then the program outputs "YES". Otherwise, the program outputs the contents of the file answer.txt. And before the contest, the sizes of the data files are given to you.
And it's clear that the size of the file with the answer "YES" is 3 bytes, the size of the file with the answer "NO" is 2 bytes, and all the variants of the order of tests are equally probable. Now you want to calculate the average number of tests that your solution won't pass.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 5000) and s (2n ≤ s ≤ 3n) where n denotes the number of data sets and s denotes the total size of the answer files.
Output
For each case, print the case number and the average number of tests your solution won't pass. Error less than 10-6 will be ignored.
Sample Input |
Output for Sample Input |
4 3 7 1 2 1 3 4 10 |
Case 1: 2 Case 2: 1 Case 3: 0 Case 4: 2.5000000000 |
Note
For the first case, one of the three answers is "YES" and two answers are "NO". If the order of tests is "YES-NO-NO", then your solution won't pass the second test only; if the order is "NO-YES-NO", then it will pass none of the tests; if the order is "NO-NO-YES", the solution won't pass the first and the third tests.
题意:
是一个人做ACM题,这道题的数据的字节数和提问数已经给出,答案只有YES或NO。那个人做不来这道题,只能蒙,一开始一定蒙YES,数据会告诉你这题的正确答案,之后下一题用上一题的正确答案蒙,求蒙错的数据组数的期望数量。
题解:
1.可知:
yes + no = n
3*yes + 2*no = s
因此可以联立解出yes、no的个数。
2.dp[i][j][isYes]:处理到第i个位置,前面已经有j个yes,并且第i个是yes o时(0代表yes)的情况下,后面错误次数的期望值。
3.可知第i+1个位置出现yes的概率为:py = (yes-j)/(n-i),no的概率为:pw = (no-(i-j))/(n-i)。
3.1 当j+1<=yes时,即还可以放yes,那么:
dp[i][j][0] = py*dp[i+1][j+1][0] + pw*(dp[i+1][j][1]+1);
dp[i][j][1] = py*(dp[i+1][j+1][0]+1) + pw*dp[i+1][j][1];
3.2 当j==yes时,即yes已经放完了,那么后面只能放no:
dp[i][j][0] = pw*(dp[i+1][j][1]+1);
dp[i][j][1] = pw*dp[i+1][j][1];
4.由于数组太大,所以要用滚动数组。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 5e3+10; 18 19 double dp[2][MAXN][2]; 20 int main() 21 { 22 int T, kase = 0; 23 scanf("%d", &T); 24 while(T--) 25 { 26 int n, s; 27 scanf("%d%d", &n, &s); 28 int yes = s - 2*n; 29 int no = 3*n - s; 30 dp[n%2][yes][0] = dp[n%2][yes][1] = 0; 31 for(int i = n-1; i>=0; i--) 32 { 33 int now = i%2, nex = (i+1)%2; 34 int minYes = max(0,i-no), maxYes = min(i,yes); 35 for(int j = minYes; j<=maxYes; j++) 36 { 37 double py = 1.0*(yes-j)/(n-i); 38 double pw = 1.0*(no-(i-j))/(n-i); 39 if(j+1<=yes) 40 { 41 dp[now][j][0] = py*dp[nex][j+1][0] + pw*(dp[nex][j][1]+1); 42 dp[now][j][1] = py*(dp[nex][j+1][0]+1) + pw*dp[nex][j][1]; 43 } 44 else 45 { 46 dp[now][j][0] = pw*(dp[nex][j][1]+1); 47 dp[now][j][1] = pw*dp[nex][j][1]; 48 } 49 } 50 } 51 printf("Case %d: %.10lf ", ++kase, dp[0][0][0]); 52 } 53 }