bzoj 2510: 弱题

2510: 弱题

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 498  Solved: 271
[Submit][Status][Discuss]

Description

M个球,一开始每个球均有一个初始标号,标号范围为1~N且为整数,标号为i的球有ai个,并保证Σai = M
每次操作等概率取出一个球(即取出每个球的概率均为1/M),若这个球标号为kk < N),则将它重新标号为k + 1;若这个球标号为N,则将其重标号为1。(取出球后并不将其丢弃)
现在你需要求出,经过K次这样的操作后,每个标号的球的期望个数。
 
 

Input

第1行包含三个正整数NMK,表示了标号与球的个数以及操作次数。
第2行包含N非负整数ai,表示初始标号为i的球有ai个。
 
 

Output

应包含N行,第i行为标号为i的球的期望个数,四舍五入保留3位小数。
 
 

Sample Input

2 3 2
3 0

Sample Output

1.667
1.333

HINT

 

【样例说明】

第1次操作后,由于标号为2球个数为0,所以必然是一个标号为1的球变为标号为2的球。所以有2个标号为1的球,有1个标号为2的球。

第2次操作后,有1/3的概率标号为2的球变为标号为1的球(此时标号为1的球有3个),有2/3的概率标号为1的球变为标号为2的球(此时标号为1的球有1个),所以标号为1的球的期望个数为1/3*3+2/3*1 = 5/3。同理可求出标号为2的球期望个数为4/3。

 

【数据规模与约定】

对于10%的数据,N ≤ 5, M ≤ 5, K ≤ 10;

对于20%的数据,N ≤ 20, M ≤ 50, K ≤ 20;

对于30%的数据,N ≤ 100, M ≤ 100, K ≤ 100;

对于40%的数据,M ≤ 1000, K ≤ 1000;

对于100%的数据,N ≤ 1000, M ≤ 100,000,000, K ≤ 2,147,483,647。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1010 
using namespace std;
int n,m,k,w[maxn];
struct Matrix{
    double a[maxn];
    Matrix(){memset(a,0,sizeof(a));}
    Matrix operator * (const Matrix &b)const{
        Matrix res;
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            res.a[i]+=a[j]*b.a[(i+n-j)%n];
        return res;
    }
}a;
Matrix Pow(Matrix x,int y){
    Matrix res;
    res.a[0]=1;
    while(y){
        if(y&1)res=res*x;
        x=x*x;
        y>>=1;
    }
    return res;
}
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++)scanf("%d",&w[i]);
    a.a[0]=(double)(m-1)/m;
    a.a[1]=(double)1/m;
    a=Pow(a,k);
    for(int i=0;i<n;i++){
        double ans=0;
        for(int j=0;j<n;j++)
            ans+=(double)w[j]*a.a[(i+n-j)%n];
        printf("%.3lf\n",ans);
    }
    return 0;
}

 

posted @ 2018-05-05 09:59  Echo宝贝儿  阅读(238)  评论(0编辑  收藏  举报