This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll MOD=1e9+7;
const int LIM=3e3+7;
ll dp[LIM][LIM], sil[LIM], osil[LIM];
ll pot(ll a, ll b) {
ll ans=1;
while(b) {
if(b&1) ans=(ans*a)%MOD;
a=(a*a)%MOD;
b/=2;
}
return ans;
}
ll C(ll n, ll k) {
if(k>n || n<0) return 0;
return (((sil[n]*osil[k])%MOD)*osil[n-k])%MOD;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll n, k;
cin >> n >> k;
sil[0]=1;
for(ll i=1; i<=n; ++i) sil[i]=(sil[i-1]*i)%MOD;
rep(i, n+1) osil[i]=pot(sil[i], MOD-2);
dp[1][1]=1;
for(int i=2; i<=n; ++i) for(ll j=1; j<=i; ++j) dp[i][j]=(dp[i-1][j]*j+dp[i-1][j-1])%MOD;
ll ans=0;
rep(i, k) {
ll x=(dp[n][k-i]*sil[k-i])%MOD;
x=(x*C(n-(k-i), i))%MOD;
if(i%2==1) x=(MOD-x)%MOD;
ans=(ans+x)%MOD;
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |