#include<bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;
/*
pair<int,int> operator +(pair<int,int> l, pair<int,int> r){
int l1=l.first, l2=l.second;
int r1=r.first, r2=r.second;
return
}
*/
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n,k;cin>>n>>k;
vector<pair<int,int>> Dp(n+5,{0,0});Dp[1]={k,0};
int M=1000000007;
for(int i=2;i<=n;i++) {
for(int j=i+1; j>0;j--){
Dp[j]={((k-j)*Dp[j].first%M + (k-j+1)*Dp[j-1].second%M)%M,( j*Dp[j].first%M + j*Dp[j].second%M)%M};
}
/* cout << i<<": ";
for(int i2=0; i2 < i+1;i2++){
cout <<i2<<":"<< Dp[i2].first<< ","<< Dp[i2].second<<" ";
}
cout << endl;*/
}
int res=0;
for(int i=0;i<n;i++)res=(res+Dp[i].second)%M;
cout << res;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include<bits/stdc++.h> #define int long long #define ll long long using namespace std; /* pair<int,int> operator +(pair<int,int> l, pair<int,int> r){ int l1=l.first, l2=l.second; int r1=r.first, r2=r.second; return } */ int32_t main(){ ios::sync_with_stdio(false); cin.tie(NULL); int n,k;cin>>n>>k; vector<pair<int,int>> Dp(n+5,{0,0});Dp[1]={k,0}; int M=1000000007; for(int i=2;i<=n;i++) { for(int j=i+1; j>0;j--){ Dp[j]={((k-j)*Dp[j].first%M + (k-j+1)*Dp[j-1].second%M)%M,( j*Dp[j].first%M + j*Dp[j].second%M)%M}; } /* cout << i<<": "; for(int i2=0; i2 < i+1;i2++){ cout <<i2<<":"<< Dp[i2].first<< ","<< Dp[i2].second<<" "; } cout << endl;*/ } int res=0; for(int i=0;i<n;i++)res=(res+Dp[i].second)%M; cout << res; } |
English