题目大意:从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);
}
}