#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int mod = 1e9 + 7; long long ans = 0; int n,m; vector<int> v; void check() { vector<vector<int>> dp(n + 1,vector<int>(n + 1)); for(int i = n; i >= 1; i--) { for(int j = i + 1; j <= n; j++) { if(v[i - 1] == v[j - 1]) { dp[i][j] = 1; } else { for(int k = i + 1; k < j; k++) { if(dp[i][k - 1] && dp[k][j]) { dp[i][j] = 1; break; } } } } } if(dp[1][n]) { ans++; if(ans == mod) { ans = 0; } } } void rec(int id) { if(id == n) { check(); return; } for(int i = 1; i <= m; i++) { v[id] = i; rec(id + 1); } } void solve() { cin >> n >> m; v.resize(n); v[0] = 1; rec(1); cout << (ans * m) % mod << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } const int mod = 1e9 + 7; long long ans = 0; int n,m; vector<int> v; void check() { vector<vector<int>> dp(n + 1,vector<int>(n + 1)); for(int i = n; i >= 1; i--) { for(int j = i + 1; j <= n; j++) { if(v[i - 1] == v[j - 1]) { dp[i][j] = 1; } else { for(int k = i + 1; k < j; k++) { if(dp[i][k - 1] && dp[k][j]) { dp[i][j] = 1; break; } } } } } if(dp[1][n]) { ans++; if(ans == mod) { ans = 0; } } } void rec(int id) { if(id == n) { check(); return; } for(int i = 1; i <= m; i++) { v[id] = i; rec(id + 1); } } void solve() { cin >> n >> m; v.resize(n); v[0] = 1; rec(1); cout << (ans * m) % mod << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { solve(); } return 0; } |