#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { int n, m; cin >> n >> m; vector<int> dp(2); dp[2 * 0 + 0] = 0; dp[2 * 0 + 1] = 1; for (int l = 1; l <= n; ++l) { vector<int> next(dp.size() + 2); for (int i = 0; i < dp.size() / 2; ++i) { next[2 * i + 1] = ((dp[2 * i + 0] + dp[2 * i + 1]) * (int64_t)i) % MOD; next[2 * i + 0] = (next[2 * i + 0] + dp[2 * i + 0] * (int64_t)(m - i)) % MOD; next[2 * (i + 1) + 0] = (dp[2 * i + 1] * (int64_t)(m - i)) % MOD; } dp = next; } int64_t result = 0; for (int i = 0; i < dp.size() / 2; ++i) result += dp[2 * i + 1]; result %= MOD; cout << result << 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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { int n, m; cin >> n >> m; vector<int> dp(2); dp[2 * 0 + 0] = 0; dp[2 * 0 + 1] = 1; for (int l = 1; l <= n; ++l) { vector<int> next(dp.size() + 2); for (int i = 0; i < dp.size() / 2; ++i) { next[2 * i + 1] = ((dp[2 * i + 0] + dp[2 * i + 1]) * (int64_t)i) % MOD; next[2 * i + 0] = (next[2 * i + 0] + dp[2 * i + 0] * (int64_t)(m - i)) % MOD; next[2 * (i + 1) + 0] = (dp[2 * i + 1] * (int64_t)(m - i)) % MOD; } dp = next; } int64_t result = 0; for (int i = 0; i < dp.size() / 2; ++i) result += dp[2 * i + 1]; result %= MOD; cout << result << endl; } |