#include <bits/stdc++.h> #define dbg(x) cerr << #x << " = " << x << "\n" using namespace std; int n, m, res; int tree[55]; int check(int a, int b) { //cout << a << " " << b << endl; if (a == b) return 0; int lele = 0; for (int i = a + 1; i < b - 1; i++) { if (tree[i] == tree[a] && check(i + 1, b)) { return 1; } } if (tree[a] == tree[b]) { return 1; } return 0; } int cnt, cnt2; void b(int pos) { if (pos == n) { res += check(0, n-1); if (res == 1'000'000'007) res = 0; return; } for (int i = 1; i <= m; i++) { tree[pos] = i; b(pos + 1); } } using ll = long long; const ll mod = 1000000007; // faster if const ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } #define rep(i, a, b) for(int i = a; i < (b); ++i) vector<ll> berlekampMassey(vector<ll> s) { int n = s.size(), L = 0, m = 0; vector<ll> C(n), B(n), T; C[0] = B[0] = 1; ll b = 1; rep(i,0,n) { ++m; ll d = s[i] % mod; rep(j,1,L+1) d = (d + C[j] * s[i - j]) % mod; if (!d) continue; T = C; ll coef = d * modpow(b, mod-2) % mod; rep(j,m,n) C[j] = (C[j] - coef * B[j - m]) % mod; if (2 * L > i) continue; L = i + 1 - L; B = T; b = d; m = 0; } C.resize(L + 1); C.erase(C.begin()); for (ll& x : C) x = (mod - x) % mod; return C; } ll a[3005]; int32_t main() { ios_base::sync_with_stdio(0); cin >> n >> m; if (n == 1) { cout << "0\n"; return 0; } if (n == 2) { cout << m << "\n"; return 0; } if (m == 1) { cout << 1 << "\n"; return 0; } if (m == 2) { cout << ((2 + modpow(2, n) - 2 * n) % mod + mod) % mod << "\n"; return 0; } if (n == 3) { cout << (m * m) % mod; } if (n == 4) { a[0] = 1; a[1] = 10; a[2] = 33; a[3] = 76; for (int i = 4; i < m; i++) { a[i] = ((4 * a[i-1] -6*a[i-2] +4*a[i-3] - a[i-4]) % mod + mod) % mod; } cout << a[m-1] << "\n"; return 0; } b(0); cout << res << "\n"; }
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include <bits/stdc++.h> #define dbg(x) cerr << #x << " = " << x << "\n" using namespace std; int n, m, res; int tree[55]; int check(int a, int b) { //cout << a << " " << b << endl; if (a == b) return 0; int lele = 0; for (int i = a + 1; i < b - 1; i++) { if (tree[i] == tree[a] && check(i + 1, b)) { return 1; } } if (tree[a] == tree[b]) { return 1; } return 0; } int cnt, cnt2; void b(int pos) { if (pos == n) { res += check(0, n-1); if (res == 1'000'000'007) res = 0; return; } for (int i = 1; i <= m; i++) { tree[pos] = i; b(pos + 1); } } using ll = long long; const ll mod = 1000000007; // faster if const ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } #define rep(i, a, b) for(int i = a; i < (b); ++i) vector<ll> berlekampMassey(vector<ll> s) { int n = s.size(), L = 0, m = 0; vector<ll> C(n), B(n), T; C[0] = B[0] = 1; ll b = 1; rep(i,0,n) { ++m; ll d = s[i] % mod; rep(j,1,L+1) d = (d + C[j] * s[i - j]) % mod; if (!d) continue; T = C; ll coef = d * modpow(b, mod-2) % mod; rep(j,m,n) C[j] = (C[j] - coef * B[j - m]) % mod; if (2 * L > i) continue; L = i + 1 - L; B = T; b = d; m = 0; } C.resize(L + 1); C.erase(C.begin()); for (ll& x : C) x = (mod - x) % mod; return C; } ll a[3005]; int32_t main() { ios_base::sync_with_stdio(0); cin >> n >> m; if (n == 1) { cout << "0\n"; return 0; } if (n == 2) { cout << m << "\n"; return 0; } if (m == 1) { cout << 1 << "\n"; return 0; } if (m == 2) { cout << ((2 + modpow(2, n) - 2 * n) % mod + mod) % mod << "\n"; return 0; } if (n == 3) { cout << (m * m) % mod; } if (n == 4) { a[0] = 1; a[1] = 10; a[2] = 33; a[3] = 76; for (int i = 4; i < m; i++) { a[i] = ((4 * a[i-1] -6*a[i-2] +4*a[i-3] - a[i-4]) % mod + mod) % mod; } cout << a[m-1] << "\n"; return 0; } b(0); cout << res << "\n"; } |