#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l) cout << *l << " \n"[l + 1 == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.X >> a.Y;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.X << ", " << a.Y << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
if (a.empty()) return o << "{}";
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
template <typename T> struct vv : vector <vector <T>> {
vv(int n, int m, T v) : vector <vector <T>> (n, vector <T>(m, v)) {}
vv() {}
};
template <typename T> struct vvv : vector <vv <T>> {
vvv(int n, int m, int k, T v) : vector <vv <T>> (n, vv <T>(m, k, v)) {}
vvv() {}
};
#ifdef Doludu
#define test(args...) abc("[" + string(#args) + "]", args)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#else
#define test(args...) void(0)
#define owo ios::sync_with_stdio(false); cin.tie(0)
#endif
const int mod = 1e9 + 7, N = 100000, logN = 20, K = 48763;
int main () {
owo;
int n;
cin >> n;
vector <bool> zero(n * 2, false);
for (int i = 0, x; i < n; ++i)
cin >> x, --x, zero[x] = true;
vector <int> pw(n + 1, 1);
for (int i = 0; i < n; ++i)
pw[i + 1] = pw[i] * 3;
int bound = pw[n];
auto gen = [&](int s) {
vector <int> cnt(n, 0);
for (int i = 0; i < n; ++i) {
cnt[i] = s % 3, s /= 3;
}
return cnt;
};
vector <int> cnt(bound, 0);
vector <vector <int>> num;
for (int s = 0; s < bound; ++s) {
vector <int> c = gen(s);
num.pb(c);
int res = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < c[i]; ++j) {
int now = i + 1;
while (now > 0 && res >> now & 1)
now--;
if (now > 0)
res |= 1 << now;
}
}
for (int i = 1; i <= n; ++i) {
if (res >> i & 1)
cnt[s]++;
else
break;
}
// > cnt[s] -> ok
}
vector <int> pre(bound, 0), dp;
pre[0] = 1; // used
for (int pos = 2 * n - 1; ~pos; --pos) {
dp.assign(bound, 0);
for (int s = 0; s < bound; ++s) if (pre[s]) {
int c = cnt[s];
for (int nxt = 1; nxt <= n; ++nxt) if (num[s][nxt - 1] < 2) {
if (nxt <= c && zero[pos])
continue;
if (nxt > c && !zero[pos])
continue;
dp[s + pw[nxt - 1]] += pre[s];
if (dp[s + pw[nxt - 1]] >= mod)
dp[s + pw[nxt - 1]] -= mod;
}
}
pre = dp;
}
cout << pre.back() << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
423 ms |
156400 KB |
Output is correct |
3 |
Correct |
434 ms |
156300 KB |
Output is correct |
4 |
Correct |
5 ms |
1936 KB |
Output is correct |
5 |
Correct |
427 ms |
156264 KB |
Output is correct |
6 |
Correct |
417 ms |
156288 KB |
Output is correct |
7 |
Correct |
416 ms |
156280 KB |
Output is correct |
8 |
Correct |
519 ms |
156192 KB |
Output is correct |
9 |
Correct |
440 ms |
156268 KB |
Output is correct |
10 |
Correct |
459 ms |
156436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
423 ms |
156400 KB |
Output is correct |
3 |
Correct |
434 ms |
156300 KB |
Output is correct |
4 |
Correct |
5 ms |
1936 KB |
Output is correct |
5 |
Correct |
427 ms |
156264 KB |
Output is correct |
6 |
Correct |
417 ms |
156288 KB |
Output is correct |
7 |
Correct |
416 ms |
156280 KB |
Output is correct |
8 |
Correct |
519 ms |
156192 KB |
Output is correct |
9 |
Correct |
440 ms |
156268 KB |
Output is correct |
10 |
Correct |
459 ms |
156436 KB |
Output is correct |
11 |
Runtime error |
2 ms |
596 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
423 ms |
156400 KB |
Output is correct |
3 |
Correct |
434 ms |
156300 KB |
Output is correct |
4 |
Correct |
5 ms |
1936 KB |
Output is correct |
5 |
Correct |
427 ms |
156264 KB |
Output is correct |
6 |
Correct |
417 ms |
156288 KB |
Output is correct |
7 |
Correct |
416 ms |
156280 KB |
Output is correct |
8 |
Correct |
519 ms |
156192 KB |
Output is correct |
9 |
Correct |
440 ms |
156268 KB |
Output is correct |
10 |
Correct |
459 ms |
156436 KB |
Output is correct |
11 |
Runtime error |
2 ms |
596 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |