#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define REP(i,N) FOR(i,0,N) #define FORD(i,b,a) for (int i = (int)(b)-1; i >= a; --i) using namespace std; typedef long long LL; typedef pair<int, int> PII; #define st first #define nd second #define pb push_back const int MOD = 1000000007; LL DP[3005][3005][2]; int main() { int N, M; scanf("%d%d", &N, &M); DP[0][0][1] = 1; REP(i,N) { REP(j,N+1) { (DP[i+1][j][1] += (DP[i][j][0] + DP[i][j][1]) * j) %= MOD; (DP[i+1][j][0] += DP[i][j][0] * (M-j)) %= MOD; (DP[i+1][j+1][0] += DP[i][j][1] * (M-j)) %= MOD; } } LL result = 0; REP(j,N+1) (result += DP[N][j][1]) %= MOD; printf("%lld\n", result); }
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 <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define REP(i,N) FOR(i,0,N) #define FORD(i,b,a) for (int i = (int)(b)-1; i >= a; --i) using namespace std; typedef long long LL; typedef pair<int, int> PII; #define st first #define nd second #define pb push_back const int MOD = 1000000007; LL DP[3005][3005][2]; int main() { int N, M; scanf("%d%d", &N, &M); DP[0][0][1] = 1; REP(i,N) { REP(j,N+1) { (DP[i+1][j][1] += (DP[i][j][0] + DP[i][j][1]) * j) %= MOD; (DP[i+1][j][0] += DP[i][j][0] * (M-j)) %= MOD; (DP[i+1][j+1][0] += DP[i][j][1] * (M-j)) %= MOD; } } LL result = 0; REP(j,N+1) (result += DP[N][j][1]) %= MOD; printf("%lld\n", result); } |