HDU 4649 Professor Tian (2013多校联合5 1007)
2013-08-06 19:53
471 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4649
Total Submission(s): 76 Accepted Submission(s): 48
[align=left]Problem Description[/align]
Timer took the Probability and Mathematical Statistics course in the 2012, But his bad attendance angered Professor Tian who is in charge of the course. Therefore, Professor Tian decided to let Timer face a hard probability
problem, and announced that if he fail to slove the problem there would be no way for Timer to pass the final exam.
As a result , Timer passed.
Now, you, the bad guy, also angered the Professor Tian when September Ends. You have to faced the problem too. The problem comes that there is an expression and you should calculate the excepted value of it. And the operators it may contains are '&' (and),'|'(or)
and '^'(xor) which are all bit operators. For example: 7&3=3, 5&2=0, 2|5=7, 4|10=14, 6^5=3, 3^4=7.
Professor Tian declares that each operator Oi with its coming number Ai+1 may disappear, and the probability that it happens is Pi (0<i<=n).
[align=left]Input[/align]
The input contains several test cases. For each test case, there is a integer n (0<n<=200) in the first line.In the second line, there are n+1 integers, stand for {Ai}. The next line contains n operators ,stand
for {Oi}. The forth line contains {Pi}.
Ai will be less than 220, 0<=Pi<=1.
[align=left]Output[/align]
For each text case, you should print the number of text case in the first line.Then output the excepted value of the expression, round to 6 decimal places.
比较简单的一道DP问题,我们设dp[i][j]表示前i个数计算完之后第j位为1的概率(二进制),那最后的答案就是
dp
[0]*2^0+dp
[1]*2^1+dp
[2]*2^2+......dp
[20]*2^20.(这里的^为乘方运算)
对于dp[i][j],设第i个符号消失的概率为pi,第(i+1)个数的第j位为di,则分三种情况考虑:
1.第i个符号为&,这时若di为1,则dp[i][j]=dp[i-1][j],否则,dp[i][j]=pi*dp[i-1][j];
2.第i个符号为|,这时若di为1,则 dp[i][j]=pi*dp[i-1][j]+1-pi,否则,dp[i][j]=dp[i-1][j];
3.第i个符号位^,这时若di为1,则dp[i][j]=dp[i-1][j]*pi+(1-pi)*(1-dp[i-1][j]);,否则,dp[i][j]=dp[i-1][j];
最后统计答案即可。注意初始化,dp[0][j]=(a[0]>>j)&1(0<=j<=20)。代码如下:
Professor Tian
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 76 Accepted Submission(s): 48
[align=left]Problem Description[/align]
Timer took the Probability and Mathematical Statistics course in the 2012, But his bad attendance angered Professor Tian who is in charge of the course. Therefore, Professor Tian decided to let Timer face a hard probability
problem, and announced that if he fail to slove the problem there would be no way for Timer to pass the final exam.
As a result , Timer passed.
Now, you, the bad guy, also angered the Professor Tian when September Ends. You have to faced the problem too. The problem comes that there is an expression and you should calculate the excepted value of it. And the operators it may contains are '&' (and),'|'(or)
and '^'(xor) which are all bit operators. For example: 7&3=3, 5&2=0, 2|5=7, 4|10=14, 6^5=3, 3^4=7.
Professor Tian declares that each operator Oi with its coming number Ai+1 may disappear, and the probability that it happens is Pi (0<i<=n).
[align=left]Input[/align]
The input contains several test cases. For each test case, there is a integer n (0<n<=200) in the first line.In the second line, there are n+1 integers, stand for {Ai}. The next line contains n operators ,stand
for {Oi}. The forth line contains {Pi}.
Ai will be less than 220, 0<=Pi<=1.
[align=left]Output[/align]
For each text case, you should print the number of text case in the first line.Then output the excepted value of the expression, round to 6 decimal places.
比较简单的一道DP问题,我们设dp[i][j]表示前i个数计算完之后第j位为1的概率(二进制),那最后的答案就是
dp
[0]*2^0+dp
[1]*2^1+dp
[2]*2^2+......dp
[20]*2^20.(这里的^为乘方运算)
对于dp[i][j],设第i个符号消失的概率为pi,第(i+1)个数的第j位为di,则分三种情况考虑:
1.第i个符号为&,这时若di为1,则dp[i][j]=dp[i-1][j],否则,dp[i][j]=pi*dp[i-1][j];
2.第i个符号为|,这时若di为1,则 dp[i][j]=pi*dp[i-1][j]+1-pi,否则,dp[i][j]=dp[i-1][j];
3.第i个符号位^,这时若di为1,则dp[i][j]=dp[i-1][j]*pi+(1-pi)*(1-dp[i-1][j]);,否则,dp[i][j]=dp[i-1][j];
最后统计答案即可。注意初始化,dp[0][j]=(a[0]>>j)&1(0<=j<=20)。代码如下:
#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; double dp[210][25]; char op[210]; double pi[210]; int a[210]; int main() { //freopen("dd.txt","r",stdin); int n,i,j,T=0; while(scanf("%d",&n)!=EOF) { printf("Case %d:\n",++T); memset(dp,0,sizeof(dp)); for(i=0;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) cin>>op[i]; for(i=1;i<=n;i++) scanf("%lf",&pi[i]); for(i=0;i<=20;i++) { dp[0][i]=(double)((a[0]>>i)&1); } for(i=1;i<=n;i++) { for(j=0;j<=20;j++) { int tmp=(1&(a[i]>>j)); if(op[i]=='&') { if(tmp) { dp[i][j]=dp[i-1][j]; } else { dp[i][j]=pi[i]*dp[i-1][j]; } } else if(op[i]=='|') { if(tmp) { dp[i][j]=pi[i]*dp[i-1][j]+1-pi[i]; } else { dp[i][j]=dp[i-1][j]; } } else if(op[i]=='^') { if(tmp) { dp[i][j]=dp[i-1][j]*pi[i]+(1-pi[i])*(1-dp[i-1][j]); } else { dp[i][j]=dp[i-1][j]; } } } } double ans=0; for(i=0;i<=20;i++) { ans+=(double)(1<<i)*dp [i]; } printf("%.6lf\n",ans); } return 0; }
相关文章推荐
- HDU 4649 - Professor Tian(2013MUTC5-1007)(概率)
- hdu 4649 Professor Tian 多校联合训练的题
- HDU 4649 - Professor Tian(2013MUTC5-1007)(概率)
- 2013 多校联合4 1007 Group(hdu 4638)
- hdu 4649 Professor Tian 多校联合训练的题
- 2013 多校第五场 hdu 4649 Professor Tian
- hdu 4649 Professor Tian 多校第五场
- hdu 4679 Terrorist’s destroy 树形dp水题 (2013多校联合)
- HDU 4691 Front compression (2013 多校联合9 1006)
- HDU 4671 Backup Plan (2013多校联合7 1006)
- 2013多校联合8 1004 Terrorist’s destroy(hdu 4679)
- hdu 4674 边双连通缩点+倍增lca+麻烦的讨论 (2013多校联合)
- HDU 4649 多校第五场1007
- HDU 4643 GSM 简单计算几何 (2013多校联合)
- hdu 4677 并查集+分块算法 好题 (2013多校联合)
- HDU 4651 2013多校联合第5场 Partition 数论
- hdu 4661 Message Passing 树形dp (2013多校联合)
- hdu 4607 park visit 2013多校联合训练第一场
- 2013 多校联合 H Park Visit (hdu 4607)
- 2013 多校联合 F Magic Ball Game (hdu 4605)