[51NOD1670] 打怪兽

传送门

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1670

题目大意

0<=n!

题解

dp[i]:i
sum[i]:<=i
dp[i]=dp[i1]sum[i1]i+1ni+1 逆原处理分母
ans=ni=0(dp[i]dp[i+1])i

const
    maxn=100005;
    mmod=1000000007;
var
    dp,sum,x:array[0..maxn]of int64;
    i,j,k:longint;
    n,a:longint;
    ans:int64;
begin
    readln(n);
    x[1]:=1;
    for i:=2 to n do
        x[i]:=(((-int64(mmod div i)+mmod)mod mmod)*x[mmod mod i]+mmod)mod mmod;
    fillchar(sum,sizeof(sum),0);
    for i:=1 to n do
        begin read(a); inc(sum[a]); end;
    for i:=1 to n do
        sum[i]:=sum[i-1]+sum[i];
    dp[0]:=1;
    for i:=1 to n do
        dp[0]:=(dp[0]*i)mod mmod;   
    for i:=1 to n do
        dp[i]:=(((dp[i-1]*(sum[i-1]-i+1))mod mmod)*x[n-i+1])mod mmod;
    dp[n+1]:=0;
    ans:=0;
    for i:=0 to n do
        ans:=(ans+(dp[i]-dp[i+1])*i)mod mmod;
    writeln(ans);
end.

你可能感兴趣的:([51NOD1670] 打怪兽)