Little Tiger vs. Deep Monkey (dp)
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道题,答对一个题得到该题相应的分数,答错不加分,老虎认为猴子会随机答题,即猴子每个题答对的概率为1/2,求老虎至少得到多少分才能保证自己不以p的概率输给猴子。
不以p的概率输即以1-p的概率赢,用dp[i][j]表示前i个问题共得到了j分的概率,
则对于每一个问题有两种情况,答对或答错,
答对(该题得到了a[i]分):dp[i][j+a[i]]+=dp[i-1][j]*0.5;
答错(该题没加分):dp[i][j]+=dp[i-1][j]*0.5;
t组样例,给定问题个数n和概率p,接下来是n个问题对应的分值;
#include<bits/stdc++.h>
using namespace std;
int a[50],b[50];
double dp[50][40050];
int main()
{
int n,m,t;
double p;
scanf("%d",&t);
while(t--)
{
scanf("%d%lf",&n,&p);
b[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=b[i-1]+a[i];
}
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=b[i];j++)
{
dp[i][j]+=dp[i-1][j]*0.5;
dp[i][j+a[i]]+=dp[i-1][j]*0.5;
}
}
double g=0;
for(int i=0;i<=b[n];i++)
{
g+=dp[n][i];
if(g>=p)
{
cout<<i<<'\n';
break;
}
}
}
return 0;
}