#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; constexpr int MOD = 1000000007; int dp[3005][3005][2]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, m; std::cin >> n >> m; FOR(i, n+1) dp[0][i][1] = 1; REP(i, 1, n+1){ int upto = std::min(n, m); FOR(j, upto+1){ dp[i][j][0] = ( 1ll * dp[i-1][j][1] * j + 1ll * dp[i-1][j][0] * (m-j) ) % MOD; dp[i][j][1] = ( 1ll * dp[i-1][j][1] * j + 1ll * dp[i-1][j+1][0] * (m-j) ) % MOD; } } std::cout << dp[n][0][1] << "\n"; 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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; constexpr int MOD = 1000000007; int dp[3005][3005][2]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, m; std::cin >> n >> m; FOR(i, n+1) dp[0][i][1] = 1; REP(i, 1, n+1){ int upto = std::min(n, m); FOR(j, upto+1){ dp[i][j][0] = ( 1ll * dp[i-1][j][1] * j + 1ll * dp[i-1][j][0] * (m-j) ) % MOD; dp[i][j][1] = ( 1ll * dp[i-1][j][1] * j + 1ll * dp[i-1][j+1][0] * (m-j) ) % MOD; } } std::cout << dp[n][0][1] << "\n"; return 0; } |