The sixth week of training the mathematics concept and method probability n question

Source: Internet
Author: User

N-Probability Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld &%llusubmit Status

Description

As Harry Potter series is over, and Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But he friends-hermione and Ron have decided upon a tolerable probabilityP of getting caught. They feel that he's safe enough if the banks he robs together give a probability less than P.

Input

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

Contains a real number P, the probability Harry needs to being below, and an integer N (0 < n≤100 ), the number of banks he has plans for. Then follow N lines, where line J gives an integer Mj (0 < mj≤100) and a real number P J . Bank J contains Mj millions, and the probability of getting caught from robbing it's Pj. A Bank goes bankrupt if it is robbed, and your may assume that all probabilities be independent as the police have very lo W funds.

Output

For each case, print the case number and the maximum number of millions he can expect to get while the probability of Gett ING caught is less than P.

Sample Input

3

0.04 3

1 0.02

2 0.03

3 0.05

0.06 3

2 0.03

2 0.03

3 0.05

0.10 3

1 0.03

2 0.02

3 0.05

Sample Output

Case 1:2

Case 2:4

Case 3:6

Test instructions: Harry Potter graduated, because he did not find a job, he wanted to rob the bank, his two base friends for him to forget a gua, tell him if he was caught in the probability of less than or equal to P, he will be very safe, otherwise will be caught.

Harry Potter then investigates the amount of money and the risk of being caught in each bank, asking you to calculate how much he can rob.

First conversion: Change the limit to not be caught the probability of greater than equal (1-P), and then turn this problem into a backpack 01 problem

DP (i) represents the probability of grabbing a big fortune

DP (i) =max {DP (I-A[J]) * (1-p[j]), Dp[i]} (i--0~ all bank money, J traverse all Banks)

#include"iostream"#include"Cstdio"#include"CString"using namespacestd;Const intmaxn= the;Doubledp[maxn*MAXN],P,B[MAXN];intN,a[maxn],sum,ca=1;voidInit () {cin>>P>>N; Sum=0;  for(intI=0; i<n;i++) {cin>>a[i]>>B[i]; Sum+=A[i]; } memset (DP,0,sizeof(DP));}voidWork () {dp[0]=1;  for(intI=0; i<n;i++)    {         for(intj=sum;j>=a[i];j--) {Dp[j]=max (dp[j-a[i]]* (1-B[i]), dp[j]); }    }}voidPrint () {cout<<" Case"<<ca++<<": ";  for(inti=sum;i>=0; i--)    {        if(Dp[i]> (1-P)) {cout<<i<<Endl;  Break; }    }}intMain () {intT; CIN>>u;  while(t--) {Init ();     Work ();    Print (); }    return 0;}

The sixth week of training the mathematics concept and method probability n question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.