#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef double K; constexpr int INF = 0x3f3f3f3f; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define TRAV(x, a) for(auto &x: (a)) #define SZ(x) ((int)(x).size()) #define PB push_back #define X first #define Y second constexpr int mod = 1e9 + 7; constexpr int N = 3010; int kon[N], zdr[N], nos[N], zak[N]; void solve() { int n, m; cin >> n >> m; kon[2] = 1; FOR(i, 1, n + 1) { for(int j = n; j >= 0; j--) { nos[j + 2] = 1ll * zak[j + 2] * (m - j - 1) % mod; zak[j + 2] = (1ll * zdr[j + 2] + nos[j + 1] + zak[j + 2]) * j % mod; zdr[j + 2] = (1ll * kon[j + 2] + zdr[j + 2] + nos[j + 1]) * (m - j) % mod; kon[j + 2] = (1ll * zdr[j + 1] + nos[j] + zak[j + 1]) % mod; } } int sum = 0; FOR(i, 0, n + 3) sum = (sum + kon[i]) % mod; cout << sum << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) { // // cout << "Case #" << te + 1 << ": "; // solve(); // } 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef double K; constexpr int INF = 0x3f3f3f3f; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define TRAV(x, a) for(auto &x: (a)) #define SZ(x) ((int)(x).size()) #define PB push_back #define X first #define Y second constexpr int mod = 1e9 + 7; constexpr int N = 3010; int kon[N], zdr[N], nos[N], zak[N]; void solve() { int n, m; cin >> n >> m; kon[2] = 1; FOR(i, 1, n + 1) { for(int j = n; j >= 0; j--) { nos[j + 2] = 1ll * zak[j + 2] * (m - j - 1) % mod; zak[j + 2] = (1ll * zdr[j + 2] + nos[j + 1] + zak[j + 2]) * j % mod; zdr[j + 2] = (1ll * kon[j + 2] + zdr[j + 2] + nos[j + 1]) * (m - j) % mod; kon[j + 2] = (1ll * zdr[j + 1] + nos[j] + zak[j + 1]) % mod; } } int sum = 0; FOR(i, 0, n + 3) sum = (sum + kon[i]) % mod; cout << sum << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) { // // cout << "Case #" << te + 1 << ": "; // solve(); // } solve(); return 0; } |