【JZOJ5616】沧海尘记

Description
Description

Input
Input

Output
Output

Sample

Input:
2 2 3 5 
Output:
0.545454545454545454 0.454545454545454545

Data Constraint
Data


Analysis

大佬们一眼切
裸的MCMC算法
它是什么呢?

对于一个集合 A { x } ,定义一种变换 t r a n s ( x )
x A t r a n s ( x ) A
t r a n s ( x ) = y , y x t r a n s ( t r a n s ( t r a n s ( y ) ) ) x
x A t r a n s ( x ) = x

这是可以使用MCMC算法的充分条件
因此,该题目符合此条件

又因为MCMC算法,得出 t r a n s ( x ) = x 的层数较少(话说这一点我也不知道怎么判断),这道题最多只有12层,因此,我们可以随便弄一个起点,比如 { 1 , 0 , 0 , 0 , 0 } 之类的,不停的做变换,直到符合条件即可。

code

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 2510
#define ld long double

using namespace std;

ld A[2][N],p[N][N];
int n,a,b,c,q[N][N],s[N],t;

int main(){
    scanf("%d %d %d %d",&n,&a,&b,&c);
    for(int l=0,i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            l=q[i][j]=(l*a+b)%c;s[i]+=q[i][j]+1;
        }
        for(int j=1;j<=n;j++)p[i][j]=(ld)(q[i][j]+1)/(ld)s[i];
    }
    int can=1,las=0,now=1;A[0][1]=1.0;
    for(;can;las^=1,now^=1){
        can=0;for(int i=1;i<=n;i++)A[now][i]=0.0;
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)A[now][i]+=A[las][j]*p[j][i];
        for(int i=1;i<=n;i++)if(fabs(A[now][i]-A[las][i])>=4e-16){can=1;break;}
    }
    for(int i=1;i<=n;i++)printf("%.18Lf ",A[now][i]);
}

猜你喜欢

转载自blog.csdn.net/white_elephant/article/details/80321150