#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 3005; const int M = 1e9 + 7; LL n, m, dp[2][N][2]; int main() { _upgrade cin >> n >> m; dp[0][0][1] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { dp[(i + 1) & 1][j][0] = 0; dp[(i + 1) & 1][j][1] = 0; } for (LL j = 0; j <= i; j++) { dp[(i + 1) & 1][j][1] = (dp[(i + 1) & 1][j][1] + dp[i & 1][j][0] * j) % M; dp[(i + 1) & 1][j][0] = (dp[(i + 1) & 1][j][0] + dp[i & 1][j][0] * (m - j)) % M; dp[(i + 1) & 1][j][1] = (dp[(i + 1) & 1][j][1] + dp[i & 1][j][1] * j) % M; dp[(i + 1) & 1][j + 1][0] = (dp[(i + 1) & 1][j + 1][0] + dp[i & 1][j][1] * (m - j)) % M; } } LL ans = 0; for (int j = 0; j <= n; j++) ans = (ans + dp[n & 1][j][1]) % M; cout << ans << "\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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 3005; const int M = 1e9 + 7; LL n, m, dp[2][N][2]; int main() { _upgrade cin >> n >> m; dp[0][0][1] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { dp[(i + 1) & 1][j][0] = 0; dp[(i + 1) & 1][j][1] = 0; } for (LL j = 0; j <= i; j++) { dp[(i + 1) & 1][j][1] = (dp[(i + 1) & 1][j][1] + dp[i & 1][j][0] * j) % M; dp[(i + 1) & 1][j][0] = (dp[(i + 1) & 1][j][0] + dp[i & 1][j][0] * (m - j)) % M; dp[(i + 1) & 1][j][1] = (dp[(i + 1) & 1][j][1] + dp[i & 1][j][1] * j) % M; dp[(i + 1) & 1][j + 1][0] = (dp[(i + 1) & 1][j + 1][0] + dp[i & 1][j][1] * (m - j)) % M; } } LL ans = 0; for (int j = 0; j <= n; j++) ans = (ans + dp[n & 1][j][1]) % M; cout << ans << "\n"; return 0; } |