HDU4576 Robot

       原题:http://acm.hdu.edu.cn/showproblem.php?pid=4576

       模拟其过程,暴力解。注意对围成圆环的数据的通用处理方法(i+w)%n和(i-w+n)%n。效率方面,一开始我用s=!s,提交两次均用时3828ms,换成异域运算符^,提交两次用时分别为,3687ms,3703ms,看来“^”的效率应该会比“!”高一点。

#include <stdio.h>
#include <string.h>
int main()
{
	int i,n,m,l,r,w;
	double a[2][201],ans;
	bool s;	// sign

	while(scanf("%d%d%d%d",&n,&m,&l,&r))
	{
		if(!n) break;
		memset(&a[0],0,sizeof(double)*200);
		s = 0; a[0][0]=1.0;
		while(m--)
		{
			scanf("%d",&w);
			for(i=0;i<n;i++)
				a[s^1][i]=0.5*a[s][(i+w)%n]+0.5*a[s][(i-w+n)%n];
			s ^= 1;
		}
		for(ans=0,i=l-1;i<r;i++) ans+=a[s][i];
		printf("%.4Lf\n",ans);
	}
	return 0;
}
       解题方法是完全模仿该篇文章的: http://www.cnblogs.com/kuangbin/p/3250367.html

       另有矩阵快速幂解法,我没有祥看:http://blog.csdn.net/huangshenno1/article/details/9891499

你可能感兴趣的:(ACM,围圈题,概率题)