# CF886E Maximum Element

$=f_{j-1} \times(i-1) ! \times \frac{1}{(j-1) !}$

$\begin{array}{l}{f_{i}=\sum_{j=i-k+1}^{i} \frac{f_{j-1}}{(j-1) !} \times(i-1) !} \\ {=(i-1) ! \sum_{j=i-k}^{i-1} \frac{f_{j}}{j !}}\end{array}$

$a n s=n !-\sum_{i=1}^{n} f_{i-1} \times\left(\begin{array}{c}{n-1} \\ {n-i}\end{array}\right) \times(n-i) !$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxn (int)(1e6+100)
#define mod (int)(1e9+7)
#define ll long long
using namespace std;
int n,k;
ll inv[maxn],fact[maxn],pre[maxn],dp[maxn];
ll ksm(ll x,ll times) {
ll cur=x,ans=1;
while(times) {
if(times&1)ans*=cur,ans%=mod;
cur*=cur;cur%=mod;times>>=1;
}
return ans;
}
ll c(ll tot,ll chose) {
return (fact[tot]*(ksm((fact[chose]*fact[tot-chose])%mod,mod-2)%mod))%mod;
}

int main() {
scanf("%d%d",&n,&k);
pre[0]=fact[1]=fact[0]=1;
for(int i=2;i<maxn;i++){fact[i]=fact[i-1]*i;fact[i]%=mod;}
for(int i=1;i<maxn;i++){inv[i]=ksm(i,mod-2);}
//cout<<">>"<<(inv[3]*9)%mod<<endl;
dp[1]=1;pre[1]=1;dp[0]=1;
for(int i=1;i<=k;i++)dp[i]=fact[i],pre[i]=pre[i-1]+dp[i]*ksm(fact[i],mod-2),pre[i]%=mod;
for(int i=k+1;i<=n;i++) {
dp[i]=(fact[i-1]*(pre[i-1]-pre[i-k-1]+mod)%mod)%mod;
pre[i]=pre[i-1]+dp[i]*ksm(fact[i],mod-2) % mod;pre[i]%=mod;
}
ll ans=0;//=fact[n]-;
for(int i=1;i<=n;i++) {
ans-=(((dp[i-1]*c(n-1,n-i))%mod)*fact[n-i])%mod,ans += mod;ans%=mod;
}
ans+=fact[n];ans%=mod;ans+=mod;ans%=mod;
printf("%lld",ans);
return 0;
}


posted @ 2019-08-04 08:19  GavinZheng  阅读(231)  评论(0编辑  收藏  举报