SDUT-2878-Circle(概率DP/高斯消元)

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大学生程序设计竞赛

示例程序


 E[p]=0.5*E[p-1]+0.5*E[p+1]+1

 即  -0.5*E[p-1]+E[p]-0.5*E[p+1]=1。

可以用高斯消元来做,

另外根据规律可以得到d[i]=(n-1)+(n-3)+(n-5)。。。


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define eps 1e-9
const int MAXN = 1005;
int p,n,m;
double a[MAXN][MAXN],x[MAXN];
bool free_x[MAXN];//标记是否是不确定的变元
//int equ,var;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
/**返回值:
-1 无解
0 有且仅有一个解
>=1 有多个解,根据free_x判断哪些是不确定的解
*/
int Gauss()
{
    int i,j;
    int row,col,max_r;
    m=n;///n个方程,n个变量的那种情况
    for(row=0,col=0; row0)
                max_r=i;
        }
        if(max_r!=row)
        {
            for(j=row; j=0; i--)
        {
            int free_num=0;///自由变元的个数
            int free_index;///自由变元的序号
            for(j=0; j1)
                continue;///该行中的不确定的变元的个数超过1个,无法求解,它们仍然为不确定的变元
            ///只有一个不确定的变元free_index,可以求解出该变元,且该变元是确定的
            double tmp=a[i][n];
            for(j=0; j=0; i--)
    {
        double tmp=a[i][n];
        for(j=i+1; jn/2)
            x=n-x;
        for(int i=1;i<=n/2;++i)
            d[i]=d[i-1]+(n-(2*i-1));
        printf("%.4lf\n",d[x]);
    }
    return 0;
}


SDUT-2878-Circle(概率DP/高斯消元)

你可能感兴趣的:(DP,模板,数论,c++,ACM)