HDU 4815 Little Tiger vs. Deep Monkey(母函数)

来源:互联网 发布:js身份证号正则表达式 编辑:程序博客网 时间:2024/06/05 21:09

Little Tiger vs. Deep Monkey

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3061 Accepted Submission(s): 1036

Problem Description
A crowd of little animals is visiting a mysterious laboratory – The Deep Lab of SYSU.

“Are you surprised by the STS (speech to speech) technology of Microsoft Research and the cat face recognition project of Google and academia? Are you curious about what technology is behind those fantastic demos?” asks the director of the Deep Lab. “Deep learning, deep learning!” Little Tiger raises his hand briskly. “Yes, clever boy, that’s deep learning (深度学习/深度神经网络)”, says the director. “However, they are only ‘a piece of cake’. I won’t tell you a top secret that our lab has invented a Deep Monkey (深猴) with more advanced technology. And that guy is as smart as human!”

“Nani ?!” Little Tiger doubts about that as he is the smartest kid in his kindergarten; even so, he is not as smart as human, “how can a monkey be smarter than me? I will challenge him.”

To verify their research achievement, the researchers of the Deep Lab are going to host an intelligence test for Little Tiger and Deep Monkey.

The test is composed of N binary choice questions. And different questions may have different scores according to their difficulties. One can get the corresponding score for a question if he chooses the correct answer; otherwise, he gets nothing. The overall score is counted as the sum of scores one gets from each question. The one with a larger overall score wins; tie happens when they get the same score.

Little Tiger assumes that Deep Monkey will choose the answer randomly as he doesn’t believe the monkey is smart. Now, Little Tiger is wondering “what score should I get at least so that I will not lose in the contest with probability of at least P? ”. As little tiger is a really smart guy, he can evaluate the answer quickly.

You, Deep Monkey, can you work it out? Show your power!

Input
The first line of input contains a single integer T (1 ≤ T ≤ 10) indicating the number of test cases. Then T test cases follow.

Each test case is composed of two lines. The first line has two numbers N and P separated by a blank. N is an integer, satisfying 1 ≤ N ≤ 40. P is a floating number with at most 3 digits after the decimal point, and is in the range of [0, 1]. The second line has N numbers separated by blanks, which are the scores of each question. The score of each questions is an integer and in the range of [1, 1000]

Output
For each test case, output only a single line with the answer.

Sample Input
1
3 0.5
1 2 3

Sample Output
3

题意

两人进行比赛,给你n道题的分值,为a[i],对手答对某一道题的概率都是0.5,求你至少要答对多少分才能以p的概率不输

思路

求出每一种得分的方法数,这一得分的方法数除以总的方法数就是这个得分所占的概率了,然后从0开始加上概率,一直到大于等于p时跳出,这时的分数就是所求的分数了,对于某一种得分的方法数的求法可以利用母函数,可以把每一道题的分值认为是等价值的硬币,那就和1块2块5块求能构成的某一块钱的方法数是一样的了,只是硬币的个数都是一个

#include <stdio.h>#include <math.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>using namespace std;#define MAX 40005int val[45];int n;int c1[MAX],c2[MAX];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,sum=0;        double p;        scanf("%d%lf",&n,&p);        for(int i=0; i<n; i++)            scanf("%d",&val[i]),sum+=val[i];        memset(c1, 0, sizeof(c1));        memset(c2, 0, sizeof(c2));        c1[0]=1;        for (int i=0; i<n; i++)        {            for (int j=0; j<=sum; j++)            {                if (c1[j]!=0)                {                    for (int k=0; k<=val[i]; k+=val[i])                    {                        if (j + k <=sum)                            c2[j + k] += c1[j];                    }                }            }            memcpy(c1, c2, sizeof(c1));            memset(c2, 0, sizeof(c2));        }        long long num=0;        for(int i=0; i<=sum; i++)            num+=c1[i];        double ans=0;        for(int i=0; i<=sum; i++)        {            ans+=c1[i]*1.0/num;            if(ans>=p)            {                printf("%d\n",i);                break;            }        }    }    return 0;}
原创粉丝点击