QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#181314 | #7227. The Magic Square | ckiseki# | RE | 0ms | 0kb | C++17 | 2.7kb | 2023-09-16 17:41:10 | 2023-09-16 17:41:11 |
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef CKISEKI
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int cnt = sizeof...(T);
(..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
cerr << (f++ ? ", " : "") << *L;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
const int mod = 1000000007;
int modpow(int64_t e, int p) {
int64_t r = 1;
while (p) {
if (p & 1) r = r * e % mod;
e = e * e % mod;
p >>= 1;
}
return r;
}
const int maxn = 1005;
int g[maxn];
int gao(int i, int l, int r) {
int64_t coef = 1, ans = 0;
const int inv2 = (mod + 1) / 2;
for (int j = l; j < r; j++) {
ans = (ans + g[j - i + 1] * coef) % mod;
coef = coef * inv2 % mod;
}
return ans;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
for (int len = 0; len < maxn; len++) {
g[len] = (2 * len * len * len + 3 * len * len + 3 * len + 3) % mod;
}
// for (int len = maxn - 1; len >= 0; len--) {
for (int len = 1; len < maxn; len++) {
for (int i = 1; i < len; i++) {
g[len] = (g[len] - 1LL * (len - i + 1) * g[i] % mod + mod) % mod;
}
}
int n, m;
cin >> n >> m;
vector<int> c(n);
vector<int> cnt(m);
for (int i = 0; i < n; i++) {
cin >> c[i];
--c[i];
cnt[c[i]] += 1;
}
for (int j = 0; j < m; j++)
if (cnt[j] == 0) {
cout << 0 << '\n';
return 0;
}
vector<int> p21(n + 1), p2(n + 1), invp21(n + 1);
p2[0] = 1;
p21[0] = 0;
for (int i = 1; i <= n; i++) {
p2[i] = (p2[i-1] + p2[i-1]) % mod;
p21[i] = (p21[i-1] + p21[i-1] + 1) % mod;
}
for (int i = 1; i <= n; i++) {
invp21[i] = modpow(p21[i], mod - 2);
}
int64_t ans = 0;
vector<int> pos(m, -1);
for (int i = n - 1; i >= 0; i--) {
pos[c[i]] = i;
vector<int> evt;
int64_t prod = 1;
for (int j = 0; j < m; j++) {
if (pos[j] != -1) {
evt.push_back(pos[j]);
}
prod = prod * p21[cnt[c[i]]] % mod;
}
sort(evt.begin(), evt.end());
int f = i;
for (int p: evt) {
// [f, p)
if (f != p) {
debug(i, f, p, prod * gao(i, f, p) % mod);
ans = (ans + prod * gao(i, f, p)) % mod;
f = p;
}
prod = prod * invp21[cnt[c[i]]] % mod;
prod = prod * p2[cnt[c[i]]] % mod;
}
debug(i, f, n, prod * gao(i, f, n) % mod);
ans = (ans + prod * gao(i, f, n)) % mod;
}
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2