QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#128771 | #5954. Power Swapper | Linshey | 4 | 1ms | 3828kb | C++14 | 1.7kb | 2023-07-21 15:41:59 | 2023-07-21 15:57:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std; const int maxn = 3e3 + 5; typedef long long ll;
int fac[13];
int n, a[13][maxn];
ll ans;
inline bool go(int l)
{
// for (int i = 0; i < 1 << l; i++) cerr << a[l][i] << ' '; cerr << endl;
for (int i = 0; i < 1 << l; i += 2) if (a[l][i] & 1) return 0;
for (int i = 0; i < 1 << l; i += 2) if (a[l][i + 1] != a[l][i] + 1) return 0;
for (int i = 0; i < 1 << l; i += 2) a[l - 1][i >> 1] = a[l][i] >> 1;
return 1;
}
void dfs(int l, int t)
{
// cerr << l << ' ' << t << endl;
if (l == 0)
{
ans += fac[t];
return;
}
vector<int> sk;
for (int i = 0; i < 1 << l; i += 2)
{
if (!(a[l][i] & 1) && a[l][i + 1] == a[l][i] + 1);
else
{
sk.push_back(i);
}
}
if (sk.size() == 0) assert(go(l)), dfs(l - 1, t);
else if (sk.size() == 1)
{
// cerr << "!" << endl;
swap(a[l][sk[0]], a[l][sk[0] + 1]);
if (go(l)) dfs(l - 1, t + 1);
}
else if (sk.size() > 2) return;
else
{
for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++)
{
swap(a[l][sk[0] + x], a[l][sk[1] + y]);
if (go(l)) dfs(l - 1, t + 1);
swap(a[l][sk[0] + x], a[l][sk[1] + y]);
}
}
}
inline void solve()
{
scanf("%d", &n);
for (int i = 0; i < 1 << n; i++) scanf("%d", a[n] + i), a[n][i]--;
ans = 0;
dfs(n, 0);
printf("%lld\n", ans);
}
int main()
{
for (int i = fac[0] = 1; i <= 12; i++) fac[i] = fac[i - 1] * i;
int Tt; scanf("%d", &Tt); for (int i = 1; i <= Tt; i++) printf("Case #%d: ", i), solve();
return 0;
}
详细
Subtask #1:
score: 4
Accepted
Test #1:
score: 4
Accepted
time: 1ms
memory: 3828kb
input:
200 1 2 1 2 1 4 3 2 3 7 8 5 6 1 2 4 3 2 4 3 2 1 2 2 4 1 3 4 3 10 11 12 1 2 9 4 13 14 15 16 5 6 7 8 1 2 1 1 2 1 1 2 1 1 1 2 4 1 2 3 4 13 14 15 16 9 12 11 10 5 6 7 8 4 9 10 11 12 1 2 15 16 13 14 6 4 5 3 7 8 2 1 4 3 2 2 3 2 1 4 4 1 2 3 4 13 14 15 16 9 10 11 8 5 6 7 12 4 13 14 11 12 1 2 5 4 9 10 15 16 3...
output:
Case #1: 1 Case #2: 3 Case #3: 6 Case #4: 0 Case #5: 2 Case #6: 30 Case #7: 1 Case #8: 1 Case #9: 1 Case #10: 1 Case #11: 38 Case #12: 30 Case #13: 3 Case #14: 3 Case #15: 38 Case #16: 24 Case #17: 8 Case #18: 0 Case #19: 3 Case #20: 1 Case #21: 1 Case #22: 24 Case #23: 6 Case #24: 24 Case #25: 6 Ca...
result:
ok 200 lines
Subtask #2:
score: 0
Runtime Error
Test #2:
score: 0
Runtime Error
input:
200 1 2 1 2 1 4 3 2 3 7 8 5 6 1 2 4 3 2 4 3 2 1 6 33 34 35 36 37 38 39 40 41 42 43 44 19 20 51 52 47 10 11 12 13 14 15 16 57 58 59 60 61 62 63 64 1 2 3 4 5 6 7 8 45 46 9 48 53 54 55 56 17 18 49 50 21 22 23 24 25 26 27 28 29 30 31 32 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2...