CF 601C(Kleofáš and the n-thlon-期望dp)

m人参加n场比赛,已知某一人各比赛的名次,求它的期望总名次(各比赛名次相加的和的排名)

显然期望dp
对于其它m-1人,令 Ei,sis

Ei,s=jEi1,jm1=jEi1,jm1

然后前缀和乱搞

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (100+10)
#define MAXM (1000+10)
double E[MAXN][MAXN*MAXM]; 
int n,m,a[MAXN];
int main()
{
    freopen("CF601C.in","r",stdin);
// freopen(".out","w",stdout);
    n=read(),m=read();
    For(i,n) a[i]=read();
    if (m==1) {
        cout<<"1.0000000000000000\n";
        return 0;
    }
    MEM(E)
    Rep(j,m+1) E[0][j]=1.0;
    For(i,n) {
        Fork(s,i,i*m) {
            E[i][s]=E[i-1][s-1] - ((s-m-1>=0)?E[i-1][s-m-1]:0) ;
            if (s>=a[i]) E[i][s]-=E[i-1][s-a[i]]-((s-a[i]-1>=0)?E[i-1][s-a[i]-1]:0); 
            E[i][s]/=m-1;
        }
        For(s,i*m+m) { 
            E[i][s]+=E[i][s-1];
        }
    }
    int s1=0;
    For(i,n) s1+=a[i];
    cout<<setprecision(10)<<setiosflags(ios::fixed)<<(m-1)*E[n][s1-1]+1<<endl; 
    return 0;
}

你可能感兴趣的:(dp)