GCJ 2008 APAC local onsites C Millionaire
时间复杂度很大。dp[i][j]表示第i轮 j这种状态的概率。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; double dp[20][50000]; int T; int M; int X; double P; void work() { memset(dp,0,sizeof dp); dp[0][1<<M]=1.00; for(int r=1; r<=M; r++) { for(int j=0; j<=(1<<M); j++) { double ans=0; for(int k=0;; k++) { if(j+k>(1<<M)||j-k<0) break; ans=max(ans,P*dp[r-1][j+k]+(1-P)*dp[r-1][j-k]); } dp[r][j]=ans; } } } int main() { //freopen("C-large-practice.in","r",stdin); // freopen("C-large-practice.out","w",stdout); scanf("%d",&T); for(int Case=1; Case<=T; Case++) { scanf("%d%lf%d",&M,&P,&X); work(); long long XX=(long long)X; int d=(1<<M); long long dd=(long long)d; long long RR=XX*dd/(long long)1000000; int R=int(RR); printf("Case #%d: %6lf\n",Case,dp[M][R]); } return 0; }