#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long using namespace std; int n, m; const int mod = 1e9 + 7; const int nax = 3005; ll dp[nax][nax][2]; void solve(){ cin >> n >> m; dp[1][1][0] = m; for(int i=1;i<n;i++){ for(int j=0;j<nax;j++){ if(j > m) break; int endWays = j; int randomWays = m - j; dp[i + 1][j][1] += (dp[i][j][0] * endWays) % mod; if(dp[i + 1][j][1] >= mod) dp[i + 1][j][1] -= mod; dp[i + 1][j][0] += (dp[i][j][0] * randomWays) % mod; if(dp[i + 1][j][0] >= mod) dp[i + 1][j][0] -= mod; dp[i + 1][j][1] += (dp[i][j][1] * endWays) % mod; if(dp[i + 1][j][1] >= mod) dp[i + 1][j][1] -= mod; dp[i + 1][j + 1][0] += (dp[i][j][1] * randomWays) % mod; if(dp[i + 1][j + 1][0] >= mod) dp[i + 1][j + 1][0] -= mod; } } ll sum = 0; for(int i=0;i<nax;i++){ sum += dp[n][i][1]; sum %= mod; } cout << sum << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); 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 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long using namespace std; int n, m; const int mod = 1e9 + 7; const int nax = 3005; ll dp[nax][nax][2]; void solve(){ cin >> n >> m; dp[1][1][0] = m; for(int i=1;i<n;i++){ for(int j=0;j<nax;j++){ if(j > m) break; int endWays = j; int randomWays = m - j; dp[i + 1][j][1] += (dp[i][j][0] * endWays) % mod; if(dp[i + 1][j][1] >= mod) dp[i + 1][j][1] -= mod; dp[i + 1][j][0] += (dp[i][j][0] * randomWays) % mod; if(dp[i + 1][j][0] >= mod) dp[i + 1][j][0] -= mod; dp[i + 1][j][1] += (dp[i][j][1] * endWays) % mod; if(dp[i + 1][j][1] >= mod) dp[i + 1][j][1] -= mod; dp[i + 1][j + 1][0] += (dp[i][j][1] * randomWays) % mod; if(dp[i + 1][j + 1][0] >= mod) dp[i + 1][j + 1][0] -= mod; } } ll sum = 0; for(int i=0;i<nax;i++){ sum += dp[n][i][1]; sum %= mod; } cout << sum << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; } |