SDUT 2878 Circle(概率dp)

16 篇文章 0 订阅

题目大意:从0点出发到达x点的期望是多少,每个点可以向左向右走概率相同均为0.5。

省赛的时候水过去了,又拿迭代系数做了一遍,初始化可能有点措,边界有点问题,不过特判了一下。

Circle

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

You have been given a circle from 0 to n - 1. If you are currently at x, you will move to (x - 1) mod n or (x + 1) mod n with equal probability. Now we want to know the expected number of steps you need to reach x from 0.

输入

The first line contains one integer T — the number of test cases.
 
Each of the next T lines contains two integers n, x (0  ≤ x < n ≤ 1000) as we mention above.

输出

For each test case. Print a single float number — the expected number of steps you need to reach x from 0. The figure is accurate to 4 decimal places.

示例输入

3
3 2
5 4
10 5

示例输出

2.0000
4.0000
25.0000

提示

 

来源

2014年山东省第五届ACM大学生程序设计竞赛
#include <algorithm>  
#include <iostream>  
#include <stdlib.h>  
#include <string.h>  
#include <iomanip>  
#include <stdio.h>  
#include <string>  
#include <queue>  
#include <cmath>  
#include <stack>  
#include <map>  
#include <set>  
#define eps 1e-8  
#define M 1000100  
//#define LL __int64  
#define LL long long  
#define INF 0x7ffffff  
#define PI 3.1415926535898  
#define Mod 100000000  
#define p (1.0/2)  
  
  
const int maxn = 10100;  
  
using namespace std;  
  
int main()  
{  
    int T;  
    cin >>T;  
    while(T--)  
    {  
        int n, x;  
        cin >>n>>x;  
        double c1 = p;  
        double c2 = 1.0;  
        double p1 = p;  
        double p2 = 1.0;  
        if(x == 0 || n == 0 || x == n)  
        {  
            cout<<"0.0000"<<endl;  
            continue;  
        }  
        for(int i = x-2; i > 0; i--)  
        {  
            double t = c1;  
            double tt = c2;  
            c1 = p/(1-t*p);  
            c2 = (1.0+tt*p)/(1-t*p);  
        }  
        for(int i = x+2; i < n; i++)  
        {  
            double t = p1;  
            double tt = p2;  
            p1 = p/(1-(t*1.0)/2);  
            p2 = (1.0+tt*p)/(1-t*p);  
        }  
        ///printf("%.4lf %.4lf %.4lf %.4lf\n",c1, c2, p1, p2);  
        double s = ((c2+p2)*p+1)/(1-(p1+c1)*p);  
        if(n-x == 1 || x == 1)  
            s/= 2;  
        printf("%.4lf\n",s);  
    }  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值