QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115772 | #6132. Repair the Artwork | Denisov | AC ✓ | 1114ms | 5588kb | C++20 | 6.2kb | 2023-06-26 23:26:28 | 2023-06-26 23:26:29 |
Judging History
answer
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
inline bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
inline bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef LOCAL
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif // LOCAL
const int max_n = 103, inf = 1000111222;
const int MOD = 1e9 + 7;
struct mint {
int val;
mint () : val(0) {}
mint (int x) {
if (-MOD <= x && x < MOD) {
val = x;
}
else {
val = x % MOD;
}
if (val < 0) val += MOD;
}
mint (ll x) {
if (-MOD <= x && x < MOD) {
val = x;
}
else {
val = x % MOD;
}
if (val < 0) val += MOD;
}
mint (const mint& x) : val(x.val) {}
constexpr mint& operator = (const mint& x) {
val = x.val;
return *this;
}
inline mint& operator += (const mint &x) {
val += x.val;
if (val >= MOD) {
val -= MOD;
}
return *this;
}
inline mint& operator -= (const mint &x) {
val -= x.val;
if (val < 0) {
val += MOD;
}
return *this;
}
inline mint operator - () const {
mint tmp(*this);
if (tmp.val) tmp.val = MOD - tmp.val;
return tmp;
}
inline mint operator + (const mint &x) const {
return mint(*this) += x;
}
inline mint operator - (const mint &x) const {
return mint(*this) -= x;
}
inline mint& operator *= (const mint &x) {
val = ((ll)val * x.val) % MOD;
return *this;
}
inline mint operator * (const mint &x) const {
return mint(*this) *= x;
}
inline mint binpow (int n) const {
mint res = 1, tmp = *this;
for (; n; n >>= 1) {
if (n & 1) {
res *= tmp;
}
tmp *= tmp;
}
return res;
}
inline mint inverse () const {
return binpow(MOD - 2);
}
inline mint& operator /= (const mint &x) {
return *this *= x.inverse();
}
inline mint operator / (const mint &x) {
return mint(*this) *= x.inverse();
}
friend ostream& operator << (ostream &os, const mint &x) {
os << x.val;
return os;
}
friend istream& operator >> (istream &is, mint &x) {
is >> x.val;
return is;
}
inline bool operator == (const mint &x) const {
return val == x.val;
}
inline bool operator != (const mint &x) const {
return val != x.val;
}
inline bool operator < (const mint &x) const {
return val < x.val;
}
inline bool operator > (const mint &x) const {
return val > x.val;
}
friend string to_string (const mint &x) {
return to_string(x.val);
}
};
vector <mint> f = {1}, fi = {1};
inline mint fact (int n) {
f.reserve(n + 1);
while (len(f) <= n) {
f.emplace_back(f.back() * len(f));
}
return f[n];
}
inline mint inv_fact (int n) { /// think
if (len(fi) <= n) {
fi.resize(n + 1, 0);
mint val = mint(1) / fact(n);
for (int i = n; fi[i] == 0; i--) {
fi[i] = val;
val *= i;
}
}
return fi[n];
}
inline mint A (int n, int k) {
if (k < 0 || k > n) return 0;
return fact(n) * inv_fact(n - k);
}
inline mint C (int n, int k) {
if (k < 0 || k > n) return 0;
return A(n, k) * inv_fact(k);
}
const int max_k = 100 * 101 / 2 + 3;
mint dp[max_n][max_k] = {};
inline void test_case () {
int n, m;
cin >> n >> m;
vector <int> a(n);
for (auto &i : a) cin >> i;
vector <vector <int> > cnt(n + 2, vector <int> (n + 2));
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (a[j] == 1) break;
++cnt[i][j];
}
}
for (int len = 2; len <= n + 1; len++) {
for (int i = 0; i + len - 1 <= n; i++) {
int j = i + len - 1;
cnt[i][j] += cnt[i][j - 1];
cnt[i][j] += cnt[i + 1][j];
cnt[i][j] -= cnt[i + 1][j - 1];
}
}
/*for (int i = 0; i <= n; i++) {
for (int j = i; j <= n; j++) {
cout << cnt[i][j] << ' ';
}
cout << '\n';
}*/
int mx = cnt[0][n] + 1;
//LOG(mx);
for (int i = 0; i <= n; i++) {
for (int j = 0; j < mx; j++) {
dp[i][j] = 0;
}
}
dp[0][0] = 1;
vector <mint> st(mx + 1);
for (int i = 1; i <= mx; i++) {
st[i] = mint(i).binpow(m);
}
mint ans = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < mx; j++) {
if (dp[i][j] == 0) {
continue;
}
int c = 0;
for (int k = i; k < n; k++) {
if (a[k] == 2) {
dp[k + 1][j + c] -= dp[i][j];
}
c = cnt[i][k];
}
ans += st[j + cnt[i][n]] * dp[i][j];
}
}
cout << ans << '\n';
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for (int test = 1; test <= t; test++) {
test_case();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5424kb
input:
3 2 2 2 0 3 2 2 1 0 3 1 2 1 0
output:
8 3 1
result:
ok 3 number(s): "8 3 1"
Test #2:
score: 0
Accepted
time: 2ms
memory: 5460kb
input:
100 2 1 0 1 2 1 2 1 2 1 1 1 1 6 2 1 14 2 3 12 2 2 2 6 13 2 2 0 2 0 2 7 14 0 0 0 0 2 2 0 5 8 2 2 0 0 0 5 5 2 2 0 0 0 12 3 0 2 0 2 2 0 1 2 2 2 2 0 7 11 2 2 0 1 0 1 0 4 4 2 1 2 2 7 5 1 1 0 0 1 0 0 2 14 2 1 15 17 2 2 1 2 0 0 0 0 2 0 1 0 0 0 0 15 11 1 1 2 0 1 2 0 0 1 0 2 1 1 1 1 15 18 1 0 1 0 2 2 1 2 0 1...
output:
1 1 0 1 1 175715347 833406719 467966815 458805426 650344 2208 537089254 146 7776 1 703335050 123067364 355668256 487954758 53774922 544070885 436748805 369291507 760487845 513270785 501075264 487417783 464534262 979007529 137956839 143317512 648268264 851188473 702545117 946416372 595191705 35054850...
result:
ok 100 numbers
Test #3:
score: 0
Accepted
time: 182ms
memory: 5568kb
input:
1000 20 673037423 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 774964932 2 2 2 17 730319736 2 2 1 1 2 2 2 2 2 2 2 2 2 1 2 2 1 11 893285699 2 2 2 1 2 1 2 2 2 1 2 16 98149251 1 2 1 2 1 2 1 1 2 1 2 2 2 2 1 2 7 556953277 1 2 2 1 2 2 2 3 228111342 1 1 1 11 640995044 2 2 1 1 2 2 1 1 1 1 1 19 741419324 1 1 2 ...
output:
447486147 204414804 452414918 684654914 763978130 805973365 0 922180033 214948715 401017738 0 201368027 752718484 611006275 848004989 391560729 950934074 202096866 443534870 24665646 482580424 321199514 922369975 152629767 5546104 1 194145234 1 1 1 562381239 648246425 497517379 217016206 961507095 4...
result:
ok 1000 numbers
Test #4:
score: 0
Accepted
time: 1114ms
memory: 5588kb
input:
1000 50 416236992 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 50 657728991 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 50 740461763 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...
output:
763259804 476502422 599342821 232859927 793988697 591429049 270188459 585052379 112828376 874793236 511742305 443789116 531138043 829814299 715762187 530976897 659595243 398499036 665696512 377388317 780011237 877457048 769085674 80046792 628967449 305823394 274620920 654337446 807171478 690217437 6...
result:
ok 1000 numbers
Test #5:
score: 0
Accepted
time: 130ms
memory: 5580kb
input:
1000 50 598094879 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 50 370102582 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 50 89148477 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...
output:
799398716 932856936 764416567 57812598 711885564 231337579 355184372 128337468 66039637 243697360 95147120 522827313 427687773 11613749 119992325 840421248 552748897 2153604 854978507 598264350 888588637 168914307 64499881 640494492 442303426 759524304 392240094 936658374 641034548 250860728 8449099...
result:
ok 1000 numbers