HDU4815 Little Tiger vs. Deep Monkey (母函数应用)

来源:互联网 发布:河北软件学院 编辑:程序博客网 时间:2024/05/16 09:42
Little Tiger vs. Deep Monkey
Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %I64d & %I64u


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!�/div>
 

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]�/div>
 

Output

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

Sample Input

13 0.51 2 3
 

Sample Output

3
 

题目意思:

给你n个题目,每个题目有相对应的分数,获得某个总分数后,才能不输掉比赛的最小概率为P。

分析:

这个题目可以用解决排列组合问题里面的母函数来解决。在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供关于这个序列的信息。我的理解是母函数就是描述构造出来函数后,每个项的系数构成的集合。

这个问题可以转换成,给你一些不用币值的硬币,每种硬币只能用一次。最后求满足条件P的方案数。  假如是,1,2,3三种币值。那么构造函数

(1+X^1)(1+X^2)(1+X^3)=1+X^1+X^2+2*X^3+X^4+X^5+X^6     展开后,幂代表的是获得的总的分数,X的系数代表的是获得这个分数的方案数。

这个问题我们就可以写一个模拟两个多项式乘法,存储下系数,还有幂值的算法。

比赛的时候最后没把BUG找出来,赛后发现系数到了最后会变得很大,要用LONG LONG存储。而方案数统计的时候也有点问题,调试了很多次没对。赛后我重新写了统计方案的部分,就把题目AC了。


#include <stdio.h>#include <iostream>#include <math.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <bitset>#include <queue>using namespace std;long long  a[40004];int t,n,T,N,k;double p;long long  mmax,i,maxmi;void xishu(){  // 这个函数模拟两个多项式乘法将系数存在a[i]中,其中i代表获得的分数    cin>>t;     // t代表幂值    a[t]++;    a[0]=1;    //零分即幂为零的时候是一种情况。所以要有个初始化    mmax=t;    maxmi=mmax;    while(N--){        cin>>n;        mmax=maxmi;        for(i=mmax;i>0;i--){            if(a[i]) a[i+n]+=a[i];     //这里代表,幂为i的X系数不为零,则与幂为i+n的            if(i+n>maxmi) maxmi=i+n;   //maxx表示X最高次幂,也就是最多获得分数        }        a[n]++;    }}int main(){    cin>>T;    while(T--){        memset(a,0,sizeof(a));               cin>>N>>p;        N--;        xishu();        b[0]=1;        long long sum1=0,sum2=0;        for(i=0;i<=maxmi;i++){             sum2+=a[i];        }//分数<=i的情况数目        for(i=0;i<=mmax;i++)        {            sum1+=a[i];            if((double)sum1/(double)sum2>=p)            {                printf("%d\n",i);                break;            }        }    }    return 0;}



0 0