#include <algorithm> #include <iostream> using namespace std; const long long MOD = 1000000007; const int MAXN = 3000; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long np[MAXN+1][MAXN+1]; long long p[MAXN+1][MAXN+1]; int main() { ios_base::sync_with_stdio(0); long long n, m; cin >> n >> m; p[0][0] = 1; for (long long i = 1; i<=n; i++) { for (long long k = 1; k * 2-1<= n; k++) { p[i][k] = ((p[i-1][k] + np[i-1][k]) * k) % MOD; np[i][k] = (np[i-1][k] * (m-k) + p[i-1][k-1]) % MOD; } } // for (int i = 0; i<=n; i++) { // cout << "p(" << i << "):"; // for (int k = 0; k<=n; k++) { // cout << "\t" << p[i][k]; // } // cout << "\tnp(" << i << "):"; // for (int k = 0; k*2-1<=n; k++) { // cout << "\t" << np[i][k]; // } // cout << endl; // } long long sum = 0; long long mult = 1; for (long long k = 1; k * 2 <= n; k++) { mult = (mult * (m - k + 1))%MOD; sum += mult * p[n][k]; sum %= MOD; } cout << sum << endl; }
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 43 44 45 | #include <algorithm> #include <iostream> using namespace std; const long long MOD = 1000000007; const int MAXN = 3000; #define FOR(i, n) for(int i = 0, __n = (n); i < __n; i++) long long np[MAXN+1][MAXN+1]; long long p[MAXN+1][MAXN+1]; int main() { ios_base::sync_with_stdio(0); long long n, m; cin >> n >> m; p[0][0] = 1; for (long long i = 1; i<=n; i++) { for (long long k = 1; k * 2-1<= n; k++) { p[i][k] = ((p[i-1][k] + np[i-1][k]) * k) % MOD; np[i][k] = (np[i-1][k] * (m-k) + p[i-1][k-1]) % MOD; } } // for (int i = 0; i<=n; i++) { // cout << "p(" << i << "):"; // for (int k = 0; k<=n; k++) { // cout << "\t" << p[i][k]; // } // cout << "\tnp(" << i << "):"; // for (int k = 0; k*2-1<=n; k++) { // cout << "\t" << np[i][k]; // } // cout << endl; // } long long sum = 0; long long mult = 1; for (long long k = 1; k * 2 <= n; k++) { mult = (mult * (m - k + 1))%MOD; sum += mult * p[n][k]; sum %= MOD; } cout << sum << endl; } |