#include <algorithm> #include <cstdio> #define ll long long using namespace std; const ll B = 1000000007; ll dp[3005][2][1505]; ll solve(int n, int m) { dp[1][0][1] = 1 % B; for(int i=1;i<n;i++) { int end = min(m, (i+1)/2); for(int j=1;j<=end;j++) { dp[i+1][1][j] += (j * dp[i][0][j]) % B; dp[i+1][1][j] %= B; dp[i+1][0][j] += (((m+B-j)%B) * dp[i][0][j]) % B; dp[i+1][0][j] %= B; dp[i+1][1][j] += (j * dp[i][1][j]) % B; dp[i+1][1][j] %= B; dp[i+1][0][j+1] += dp[i][1][j]; dp[i+1][0][j+1] %= B; } } ll res = 0; ll d = m; int end = min(m, n/2); for(int i=1;i<=end;i++) { res += (dp[n][1][i] * d) % B; res %= B; d *= (m+B-i)%B; d %= B; } return res; } int main(int argc, char** argv) { int n, m; scanf("%d%d", &n, &m); ll res = solve(n, m); printf("%lld\n", res); 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 | #include <algorithm> #include <cstdio> #define ll long long using namespace std; const ll B = 1000000007; ll dp[3005][2][1505]; ll solve(int n, int m) { dp[1][0][1] = 1 % B; for(int i=1;i<n;i++) { int end = min(m, (i+1)/2); for(int j=1;j<=end;j++) { dp[i+1][1][j] += (j * dp[i][0][j]) % B; dp[i+1][1][j] %= B; dp[i+1][0][j] += (((m+B-j)%B) * dp[i][0][j]) % B; dp[i+1][0][j] %= B; dp[i+1][1][j] += (j * dp[i][1][j]) % B; dp[i+1][1][j] %= B; dp[i+1][0][j+1] += dp[i][1][j]; dp[i+1][0][j+1] %= B; } } ll res = 0; ll d = m; int end = min(m, n/2); for(int i=1;i<=end;i++) { res += (dp[n][1][i] * d) % B; res %= B; d *= (m+B-i)%B; d %= B; } return res; } int main(int argc, char** argv) { int n, m; scanf("%d%d", &n, &m); ll res = solve(n, m); printf("%lld\n", res); return 0; } |