现在的位置: 首页 > 综合 > 正文

HDU 4576(2013杭州邀请赛) 滚动数组+模拟过程

2013年10月19日 ⁄ 综合 ⁄ 共 489字 ⁄ 字号 评论关闭

一个求概率的题目,模拟过程

用个类似滚动数组的

思路如代码:

#include<string.h>
#include<algorithm>
#include <stdio.h>
using namespace std;
#define N 201
double sum[2][N];//滚动数组
int main()
{
    int i,j,n,m,l,r,w,dis;
    while(scanf("%d %d %d %d",&n,&m,&l,&r),n){
        int ro=0;
        sum[0][0]=1;
        while(m--){
            scanf("%d",&w);	   w%=n;//取个模防止机器人走好几圈
            ro=!ro;
            memset(sum[ro],0,sizeof(sum[ro]));
            for(i=0;i<n;i++)
            {
                if(i-w>=0)dis=i-w;
                else dis=i+n-w;
                    sum[ro][i]=sum[!ro][(i+w)%n]*0.5+sum[!ro][dis]*0.5;
            }
        }
        double ans=0;
        for(i=l-1;i<r;i++)
			ans+=sum[ro][i];
        printf("%.4lf\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.