#include<bits/stdc++.h> using namespace std; constexpr int MAX_N = 3000; constexpr int MOD = 1000000007; int n,m,k[2]; long long wyn[2][2][MAX_N+9]; int main() { scanf("%d%d", &n, &m); wyn[1][0][1] = m; k[0] = 1; for(int i = 2; i <= n; ++i) { for(int j = 0; j <= k[i&1]; ++j) { wyn[i&1][0][j] = 0; wyn[i&1][1][j] = 0; } for(int j = 1; j <= k[i&1]; ++j) { wyn[i&1][1][j] += wyn[1^i&1][0][j] * j; wyn[i&1][0][j] += wyn[1^i&1][0][j] * (m-j); wyn[i&1][1][j] += wyn[1^i&1][1][j] * j; wyn[i&1][0][j+1] += wyn[1^i&1][1][j] * (m-j); wyn[i&1][0][j] %= MOD; wyn[i&1][1][j] %= MOD; wyn[i&1][0][j+1] %= MOD; } if(wyn[i&1][0][k[i&1]+1] > 0) { k[1^i&1] = k[i&1]+1; } else { k[1^i&1] = k[i&1]; } } long long ret = 0; for(int i = 0; i <= n; ++i) { ret += wyn[n&1][1][i]; ret %= MOD; } printf("%lld\n", ret); 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 | #include<bits/stdc++.h> using namespace std; constexpr int MAX_N = 3000; constexpr int MOD = 1000000007; int n,m,k[2]; long long wyn[2][2][MAX_N+9]; int main() { scanf("%d%d", &n, &m); wyn[1][0][1] = m; k[0] = 1; for(int i = 2; i <= n; ++i) { for(int j = 0; j <= k[i&1]; ++j) { wyn[i&1][0][j] = 0; wyn[i&1][1][j] = 0; } for(int j = 1; j <= k[i&1]; ++j) { wyn[i&1][1][j] += wyn[1^i&1][0][j] * j; wyn[i&1][0][j] += wyn[1^i&1][0][j] * (m-j); wyn[i&1][1][j] += wyn[1^i&1][1][j] * j; wyn[i&1][0][j+1] += wyn[1^i&1][1][j] * (m-j); wyn[i&1][0][j] %= MOD; wyn[i&1][1][j] %= MOD; wyn[i&1][0][j+1] %= MOD; } if(wyn[i&1][0][k[i&1]+1] > 0) { k[1^i&1] = k[i&1]+1; } else { k[1^i&1] = k[i&1]; } } long long ret = 0; for(int i = 0; i <= n; ++i) { ret += wyn[n&1][1][i]; ret %= MOD; } printf("%lld\n", ret); return 0; } |