sdut2878——Circle(高斯消元求期望)

来源:互联网 发布:python 基础 编辑:程序博客网 时间:2024/06/16 09:58

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.

示例输入

33 25 410 5

示例输出

2.00004.0000

25.0000

解题思路:

题意为n个节点编号0到n-1,成一个环形,给定一个数x,求从0号节点走到x节点的期望步数是多少。节点向两边走的概率相同,每一步走一个节点。

高斯消元,n个方程,n个未知量, 设E[ p ] 为从p节点走到x节点还需要走的步数的期望数。那么E [ x ] =0;

对于每个节点都有   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。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <algorithm>#include <vector>#include <map>#include <string>#include <stack>using namespace std;typedef long long ll;#define PI 3.1415926535897932#define E 2.718281828459045#define INF 0xffffffff//0x3f3f3f3f#define mod 997const int M=1005;//int n,m;int cnt;int sx,sy,sz;int mp[1000][1000];int pa[M*10],rankk[M];int head[M*6],vis[M*100];int dis[M*100];ll prime[M*1000];bool isprime[M*1000];int lowcost[M],closet[M];char st1[5050],st2[5050];int len[M*6];typedef pair<int ,int> ac;//vector<int> g[M*10];ll dp[50][20][2];int has[10500];int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};void getpri(){    ll i;    int j;    cnt=0;    memset(isprime,false,sizeof(isprime));    for(i=2; i<1000000LL; i++)    {        if(!isprime[i])prime[cnt++]=i;        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)        {            isprime[i*prime[j]]=1;            if(i%prime[j]==0)break;        }    }}struct node{    int v,w;    node(int vv,int ww)    {        v=vv;        w=ww;    }};vector<int> g[M*100];char str[100005];//浮点型高斯消元模板const double eps=1e-12;const int maxm=1000;///m个方程,n个变量const int maxn=1000;int m,n;double a[maxm][maxn+1];///增广矩阵bool free_x[maxn];///判断是否是不确定的变元double x[maxn];///解集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;row<m&&col<n;row++,col++)    {        max_r=row;        for(i=row+1;i<m;i++)///找到当前列所有行中的最大值(做除法时减小误差)        {            if(sign(fabs(a[i][col])-fabs(a[max_r][col]))>0)                max_r=i;        }        if(max_r!=row)        {            for(j=row;j<n+1;j++)                swap(a[max_r][j],a[row][j]);        }        if(sign(a[row][col])==0)///当前列row行以下全为0(包括row行)        {            row--;            continue;        }        for(i=row+1;i<m;i++)        {            if(sign(a[i][col])==0)                continue;            double tmp=a[i][col]/a[row][col];            for(j=col;j<n+1;j++)                a[i][j]-=a[row][j]*tmp;        }    }    for(i=row;i<m;i++)///col=n存在0...0,a的情况,无解    {        if(sign(a[i][col]))            return -1;    }    if(row<n)///存在0...0,0的情况,有多个解,自由变元个数为n-row个    {        for(i=row-1;i>=0;i--)        {            int free_num=0;///自由变元的个数            int free_index;///自由变元的序号            for(j=0;j<n;j++)            {                if(sign(a[i][j])!=0&&free_x[j])                    free_num++,free_index=j;            }            if(free_num>1)                continue;///该行中的不确定的变元的个数超过1个,无法求解,它们仍然为不确定的变元        ///只有一个不确定的变元free_index,可以求解出该变元,且该变元是确定的            double tmp=a[i][n];            for(j=0;j<n;j++)            {                if(sign(a[i][j])!=0&&j!=free_index)                    tmp-=a[i][j]*x[j];            }            x[free_index]=tmp/a[i][free_index];            free_x[free_index]=false;        }        return n-row;    }    ///有且仅有一个解,严格的上三角矩阵(n==m)    for(i=n-1;i>=0;i--)    {        double tmp=a[i][n];        for(j=i+1;j<n;j++)        if(sign(a[i][j])!=0)            tmp-=a[i][j]*x[j];                x[i]=tmp/a[i][i];    }    return 0;}///模板结束int main(){    int i,j,k,t;    scanf("%d",&t);    while(t--){        scanf("%d%d",&n,&k);        memset(a,0,sizeof(a));        for(i=0;i<n;i++){            if(i==k){//目标节点期望是0                a[i][i]=1; //系数矩阵  1表示E[k]的系数                a[i][n]=0;//增广那一列表示初始状态,E[k]=0                continue;            }            a[i][i]=1;            a[i][n]=1;  //除了E[k]=0,其他行都是-0.5*E[p-1]+E[p]-0.5*E[p+1]=1            a[i][(i-1+n)%n]=-0.5;            a[i][(i+1)%n]=-0.5;        }        Gauss();        printf("%.4lf\n",x[0]);    }    return 0;}

与第四届省赛概率期望题对比

上面两道题,首先第一步是列出期望公式,写边界条件。

第一道只要按顺序递推即可;第二道题则需列方程组高斯消元求解

产生这么大的区别的原因是:

当我们把各个状态当成是一个个节点时,概率关系为有向边,我们可看到,可递推的问题其实就是这个关系图是无环的,必须要用方程组解决的问题其实就是存在环的! 而且还要指出的是用高斯消元的时候,要注意误差的问题,最好把式子适当的增大,避免解小数,否则误差太大,估计也会卡题。

1 0
原创粉丝点击