#include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < int(n); ++i) #define FO(i, a, b) for (int i = (a); i < int(b); ++i) #define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((b) < (a) ? (a) : (b)) #define REMIN(a, b) ((a) = min(a, b)) #define REMAX(a, b) ((a) = max(a, b)) #define ALL(c) (c).begin(), (c).end() #define SQR(x) ((x) * (x)) // const int N = 3009; int dp[N][N][2]; int main() { ios_base::sync_with_stdio(0); int n, m; const int mod = 1e9 + 7; cin >> n >> m; dp[0][0][1] = 1; FO(i, 1, n + 1) { FO(k, 0, i + 1) { int r0 = 1LL * dp[i - 1][k][0] * (m - k) % mod; if (k - 1 >= 0) r0 = (r0 + 1LL * dp[i - 1][k - 1][1] * (m - k + 1)) % mod; dp[i][k][0] = r0; dp[i][k][1] = (1LL * dp[i - 1][k][0] + dp[i - 1][k][1]) * k % mod; // cerr << "dp[" << i << "][" << k << "][0] == " << dp[i][k][0] << endl; // cerr << "dp[" << i << "][" << k << "][1] == " << dp[i][k][1] << endl; } } int r = 0; FOR(i, n + 1) r = (r + dp[n][i][1]) % mod; cout << r << endl; return 0; }
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 46 47 48 49 50 51 52 | #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < int(n); ++i) #define FO(i, a, b) for (int i = (a); i < int(b); ++i) #define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((b) < (a) ? (a) : (b)) #define REMIN(a, b) ((a) = min(a, b)) #define REMAX(a, b) ((a) = max(a, b)) #define ALL(c) (c).begin(), (c).end() #define SQR(x) ((x) * (x)) // const int N = 3009; int dp[N][N][2]; int main() { ios_base::sync_with_stdio(0); int n, m; const int mod = 1e9 + 7; cin >> n >> m; dp[0][0][1] = 1; FO(i, 1, n + 1) { FO(k, 0, i + 1) { int r0 = 1LL * dp[i - 1][k][0] * (m - k) % mod; if (k - 1 >= 0) r0 = (r0 + 1LL * dp[i - 1][k - 1][1] * (m - k + 1)) % mod; dp[i][k][0] = r0; dp[i][k][1] = (1LL * dp[i - 1][k][0] + dp[i - 1][k][1]) * k % mod; // cerr << "dp[" << i << "][" << k << "][0] == " << dp[i][k][0] << endl; // cerr << "dp[" << i << "][" << k << "][1] == " << dp[i][k][1] << endl; } } int r = 0; FOR(i, n + 1) r = (r + dp[n][i][1]) % mod; cout << r << endl; return 0; } |