#include<algorithm> #include<iostream> #include<vector> using namespace std; using VI = vector<long long>; using VVI = vector<VI>; constexpr int mod = int(1e9 + 7); int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m; cin >> n >> m; long long c = min((n + 1) / 2, m); VVI loop(n + 1, VI(c + 1, 0)); VVI dp(n + 1, VI(c + 1, 0)); dp[1][1] = m; loop[1][1] = 0; for (int i = 2; i <= n; i++) { for (int j = 1; j <= c; j++) { dp[i][j] += (dp[i - 1][j] - loop[i - 1][j] + mod) * (m - j); dp[i][j] += dp[i - 1][j] * j; dp[i][j] += loop[i - 1][j - 1] * (m - j + 1); dp[i][j] %= mod; loop[i][j] = (dp[i - 1][j] * j) % mod; } } long long ans = 0; for (int j = 1; j <= c; j++) ans += loop[n][j]; ans %= mod; cout << ans << "\n"; }
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 | #include<algorithm> #include<iostream> #include<vector> using namespace std; using VI = vector<long long>; using VVI = vector<VI>; constexpr int mod = int(1e9 + 7); int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m; cin >> n >> m; long long c = min((n + 1) / 2, m); VVI loop(n + 1, VI(c + 1, 0)); VVI dp(n + 1, VI(c + 1, 0)); dp[1][1] = m; loop[1][1] = 0; for (int i = 2; i <= n; i++) { for (int j = 1; j <= c; j++) { dp[i][j] += (dp[i - 1][j] - loop[i - 1][j] + mod) * (m - j); dp[i][j] += dp[i - 1][j] * j; dp[i][j] += loop[i - 1][j - 1] * (m - j + 1); dp[i][j] %= mod; loop[i][j] = (dp[i - 1][j] * j) % mod; } } long long ans = 0; for (int j = 1; j <= c; j++) ans += loop[n][j]; ans %= mod; cout << ans << "\n"; } |