题目来源: http://www.codechef.com/AUG13/problems/SHIRO
题目描述:
Shiro is leading an army to save the princess of his kingdom "Abra". The princess has been made a prisoner by the neighboring kingdom "Kadabra". Kadabra is a magical land and is full of challenges. Shiro's army has to passN levels before they can get to the princess. After each level, the army gets a few flags to carry along. The flags can either all be of kindom Abra OR all of kingdom Kadabra. The magic of Kadabra forces Shiro's army to carry the flags irrespective of which kingdom they belong to. The princess doesn't know Shiro or anyone from his army. She will not escape with them unless she can trust them. She will trust them only if the number of Abra's flags they are carrying is atleast as much as the number of Kadabra's flags.
The army gets ai flags at the end of the ith level. Probability that flags received at the end of the ith level will be Abra's flags ispi. Your task is to tell Shiro what is the probability that the princess will trust him once they reach her.
Input:
First line of input contains a single integer T, the number of test cases.
Each test starts with a single line having an integer, N, the number of levels in Kadabra.
Next line contains contains N integers with the ith integer beingai as described above.
Next line contains contains N integers with the ith integer beingpi as described above. Note that the probabilities given will be in percents.
Output:
For each test case, output a line containing the required probability. The answer will be accepted if the relative error is not more than 10-6.
Constraints:
1 ≤ T ≤ 100 1 ≤ N ≤ 100 1 ≤ ai ≤ 100 0 ≤ pi ≤ 100
Example:
Input:
2 5 1 2 3 4 4 0 100 100 0 50 2 5 5 50 60
Output:
0.5000000 0.8000000题目大概意思:求Abra旗帜的个数大于等于总旗帜一半的概率,如第二组数据,一共有0,5,10,三种可能性,则p=0.5*0.6+0.5*0.4+0.5*0.6=0.8;
所以可以有一个递归式 Pro[j]=Pro[j]*(1-p[i])+Pro[j-a[i]]*p[i] 也就是DP。
最后求Pro[sum/2]~Pro[sum]的和
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 10010
using namespace std;
double Pro[MAX];
int a[110];
double p[110];
int N;
int sum=0;
void input()
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
for(int i=1;i<=N;i++)
{
int c;
scanf("%d",&c);
p[i]=(double)c/100;
}
}
void solve()
{
for(int i=1;i<=N;i++)
{
for(int j=MAX;j>=a[i];j--)
{
Pro[j]=Pro[j]*(1-p[i])+Pro[j-a[i]]*p[i];
}
for(int k=0;k<a[i];k++)
Pro[k]=Pro[k]*(1-p[i]);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(Pro,0,sizeof(Pro));
Pro[0]=1;
input();
solve();
double ans=0;
for(int i=((sum+1)/2);i<=sum;i++)
{
//printf("%.5f\n",Pro[i]);
ans+=Pro[i];
}
printf("%.7f\n",ans);
sum=0;
}
}