#include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; ll safe_mod(ll x, ll m) { ll t = x%m; return t >= 0 ? t : t + m; } struct mint { static const ll mod = 1000000007; // change to something else ll x; ll val() { x %= mod; return x; } mint() : x(0) {} mint(ll xx) : x(safe_mod(xx, mod)) {} mint operator+(mint b) { return mint((x + b.x) % mod); } void operator+=(mint b) { x = (x + b.x) % mod; } mint operator-(mint b) { return mint((x - b.x + mod) % mod); } mint operator*(mint b) { return mint((x * b.x) % mod); } mint operator^(ll e) { ll ans = 1, b = x; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } }; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif //Did you REAALLY consider sample tests? int cuttable(vi config) { int n = sz(config); vi dp(n + 1, 0); config.pb(-1); reverse(all(config)); dp[0] = 1; FOR(i, 1, n) { if (dp[i-1]) { FOR(j, i+1, n) { if (config[i] == config[j]) dp[j] = 1; } } } return dp[n]; } mint brut(int n, int m) { // there are m^n configurations mint em = m; mint all = em^n; int vall = all.val(); mint res = 0; for (int i=0; i<vall; ++i) { vi config(n, 0); int mask = i; rep(j, 0, n) { config[j] = (mask % m); mask /= m; } int bit = cuttable(config); res += bit; } return res; } const int maxn = 3010; mint dp[maxn][maxn][2]; mint wzo(int n, int M) { rep(i, 0, maxn) rep(j, 0, maxn) rep(bit, 0, 2) dp[i][j][bit] = 0; dp[1][1][0] = M; for (int N = 1; N < n; ++N) { for (int K = 1; K <= min(M, N); ++K) { dp[N+1][K][0] += dp[N][K][0] * (M - K); dp[N+1][K][1] += dp[N][K][0] * K; dp[N+1][K+1][0] += dp[N][K][1] * (M - K); dp[N+1][K][1] += dp[N][K][1] * K; } } mint res = 0; for (int i = 1; i <= n; ++i) res += dp[n][i][1]; return res; } bool TEST = 0; int main() { int n, k; cin >> n >> k; if (!TEST) { cout << wzo(n, k).val() << "\n"; } else { for (int i=1; i<=n; ++i) { for (int j=1; j<=k; ++j) { printf("%6d ", brut(i, j).val()); } cout << "\n"; } for (int i=1; i<=n; ++i) { for (int j=1; j<=k; ++j) { printf("%6d ", wzo(i, j).val()); } cout << "\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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; ll safe_mod(ll x, ll m) { ll t = x%m; return t >= 0 ? t : t + m; } struct mint { static const ll mod = 1000000007; // change to something else ll x; ll val() { x %= mod; return x; } mint() : x(0) {} mint(ll xx) : x(safe_mod(xx, mod)) {} mint operator+(mint b) { return mint((x + b.x) % mod); } void operator+=(mint b) { x = (x + b.x) % mod; } mint operator-(mint b) { return mint((x - b.x + mod) % mod); } mint operator*(mint b) { return mint((x * b.x) % mod); } mint operator^(ll e) { ll ans = 1, b = x; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } }; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif //Did you REAALLY consider sample tests? int cuttable(vi config) { int n = sz(config); vi dp(n + 1, 0); config.pb(-1); reverse(all(config)); dp[0] = 1; FOR(i, 1, n) { if (dp[i-1]) { FOR(j, i+1, n) { if (config[i] == config[j]) dp[j] = 1; } } } return dp[n]; } mint brut(int n, int m) { // there are m^n configurations mint em = m; mint all = em^n; int vall = all.val(); mint res = 0; for (int i=0; i<vall; ++i) { vi config(n, 0); int mask = i; rep(j, 0, n) { config[j] = (mask % m); mask /= m; } int bit = cuttable(config); res += bit; } return res; } const int maxn = 3010; mint dp[maxn][maxn][2]; mint wzo(int n, int M) { rep(i, 0, maxn) rep(j, 0, maxn) rep(bit, 0, 2) dp[i][j][bit] = 0; dp[1][1][0] = M; for (int N = 1; N < n; ++N) { for (int K = 1; K <= min(M, N); ++K) { dp[N+1][K][0] += dp[N][K][0] * (M - K); dp[N+1][K][1] += dp[N][K][0] * K; dp[N+1][K+1][0] += dp[N][K][1] * (M - K); dp[N+1][K][1] += dp[N][K][1] * K; } } mint res = 0; for (int i = 1; i <= n; ++i) res += dp[n][i][1]; return res; } bool TEST = 0; int main() { int n, k; cin >> n >> k; if (!TEST) { cout << wzo(n, k).val() << "\n"; } else { for (int i=1; i<=n; ++i) { for (int j=1; j<=k; ++j) { printf("%6d ", brut(i, j).val()); } cout << "\n"; } for (int i=1; i<=n; ++i) { for (int j=1; j<=k; ++j) { printf("%6d ", wzo(i, j).val()); } cout << "\n"; } } } |