题目链接:https://vjudge.net/problem/LightOJ-1274

1274 - Beating the Dataset
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\no时(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.由于数组太大,所以要用滚动数组。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 5e3+; double dp[][MAXN][];
int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
int n, s;
scanf("%d%d", &n, &s);
int yes = s - *n;
int no = *n - s;
dp[n%][yes][] = dp[n%][yes][] = ;
for(int i = n-; i>=; i--)
{
int now = i%, nex = (i+)%;
int minYes = max(,i-no), maxYes = min(i,yes);
for(int j = minYes; j<=maxYes; j++)
{
double py = 1.0*(yes-j)/(n-i);
double pw = 1.0*(no-(i-j))/(n-i);
if(j+<=yes)
{
dp[now][j][] = py*dp[nex][j+][] + pw*(dp[nex][j][]+);
dp[now][j][] = py*(dp[nex][j+][]+) + pw*dp[nex][j][];
}
else
{
dp[now][j][] = pw*(dp[nex][j][]+);
dp[now][j][] = pw*dp[nex][j][];
}
}
}
printf("Case %d: %.10lf\n", ++kase, dp[][][]);
}
}

LightOJ - 1274 Beating the Dataset —— 期望的更多相关文章

  1. 【非原创】LightOJ-1274 Beating the Dataset【期望dp】

    学习博客:戳这里

  2. LightOJ 1030 Discovering Gold(期望)

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

  3. LightOj:1030-Discovering Gold(期望dp模板)

    传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...

  4. LightOJ 1030 Discovering Gold (概率/期望DP)

    题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...

  5. LightOJ 1287 Where to Run(期望)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1287 题意:给定一个n个点的无向图(0到n-1),你开始在0.你开始遍历这个图 ...

  6. LightOj_1274 Beating the Dataset

    题目链接 题意: 给一个文档, 这个文档由yes .no 组成, 共有s个byte, 共有n个yes.no. 假设yes的个数为yes_num, no的个数为no_num. 将这n个数进行排列, 对于 ...

  7. LightOJ 1030 Discovering Gold (期望)

    https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...

  8. LightOJ - 1287 Where to Run —— 期望、状压DP

    题目链接:https://vjudge.net/problem/LightOJ-1287 1287 - Where to Run    PDF (English) Statistics Forum T ...

  9. LightOJ - 1027 A Dangerous Maze —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous Maze    PDF (English) Statistics For ...

随机推荐

  1. 贴几个spark提交任务的小脚本

    spark单个master节点的提交方式 spark-submit --master spark://hadoop-namenode-02:7077 \ --class com.dinpay.bdp. ...

  2. Libimseti推荐系统

    技术:easyUI.jQuery.Spring.Struts.Hibernate.Mahout.MySQL 本Libimseti推荐系统使用数据.代码參考<Mahout in action> ...

  3. 拿wordpress站的一个小技巧

    记得09年时wp爆过一个重置管理口令的漏洞, 现在用法差不多, 也是我刚刚发现, 网上也没找到有讲述关于这个的. 前提:是在有注入点(注入点的话可以通过寻找插件漏洞获得.), 密码解不开, 无法out ...

  4. PHP 变量定义及使用

    php的变量前面必须有$符号,而且是解释型的弱类型语言,定义的时候不需要定义变量值的类型. $str="这是个变量"; 1.输出的时候可以用拼接字符串的方法 如:echo" ...

  5. 《Linux设备驱动开发具体解释(第3版)》(即《Linux设备驱动开发具体解释:基于最新的Linux 4.0内核》)网购链接

    <Linux设备驱动开发具体解释:基于最新的Linux 4.0内核> china-pub   spm=a1z10.3-b.w4011-10017777404.30.kvceXB&i ...

  6. 网站添加ico图标

    打开某一个网页会在浏览器的标签栏处显示该网页的标题和图标,当网页被添加到收藏夹或者书签中时也会出现网页的图标,怎么在网页title左边显示网页的logo图标呢? 方法一(被动式): 制作一个ico格式 ...

  7. Swift初窥----语法进阶

    缺省绑定(Optional Binding 自己主动置空) 通过在类型变量后,加上?,能够实现缺省绑定为nil var window: UIWindow? 就是说,假设不正确window赋值,则win ...

  8. eeplat开发平台概念理解

    近期在学习eeplat的开发.发现其中有非常多概念实在让人easy忘记,所以谨以此文记录一笔. eeplat的开发文档里说eeplat是元数据驱动的,这个元数据什么意思.在我理解就是后台的数据库里面的 ...

  9. java集合归纳

    学习自: http://android.blog.51cto.com/268543/400557/ MAP Collection 堆栈队列操作尽可能考虑 linkedlist 多线程同步操作尽可能考虑 ...

  10. Apc缓存Opcode(转)

    1.PHP执行 PHP的运行阶段也分成三个阶段: Parse.语法分析阶段. Compile.编译产出opcode中间码. Execute.运行,动态运行进行输出.                   ...