QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#862156 | #9980. Boolean Function Reconstruction | ucup-team2796# | WA | 22ms | 3712kb | C++17 | 5.0kb | 2025-01-18 22:22:05 | 2025-01-18 22:22:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())
using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
assert(y != 0);
if (y < 0)
x = -x, y = -y;
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
assert(y != 0);
if (y < 0)
x = -x, y = -y;
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
return (x == 0 ? -1 : __builtin_ctzll(x));
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "P(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
os << "{";
for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
os << "(" << itr->first << ", " << itr->second << ")";
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
os << "{";
for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
for (; a[i] != ',' && a[i] != '\0'; i++)
cerr << a[i];
cerr << ":" << b << " ";
_show(i + 1, a, c...);
}
/**
* @brief template
*/
void Solve() {
int N;
cin >> N;
vector<int> B(1<<N);
{
string S;
cin >> S;
rep(i,0,1<<N) B[i] = S[i]-'0';
}
auto A = B;
{
rep(i,0,N) {
rep(j,0,1<<N) {
if (j&(1<<i)) continue;
A[j|(1<<i)] += A[j];
}
}
rep(i,0,1<<N) {
if (B[i] == 0 && A[i] > 0) {
cout << "No" << endl;
return;
}
}
}
if (A.back() == 0) {
cout << "Yes" << endl;
cout << 'F' << endl;
return;
}
if (A.back() == (1<<N)) {
cout << "Yes" << endl;
cout << 'T' << endl;
return;
}
auto DFS = [&](auto self, int Dep, int L) -> string {
char C = 'a' + (Dep-1);
int R = L + (1<<Dep), M = (L+R)/2;
if (A[R-1] == 0) return "F";
if (A[R-1] == (1<<Dep)) return "T";
rep(i,L,M) {
A[i|(1<<(Dep-1))] -= A[i];
}
string SL = self(self, Dep-1, L);
string SR = self(self, Dep-1, M);
if (SL == "F") {
if (SR == "F") {
return "F";
}
else if (SR == "T") {
return string(1,C);
}
else {
return '(' + SR + '&' + C + ')';
}
}
else if (SL == "T") {
return "T";
}
else {
if (SR == "F") {
return SL;
}
else if (SR == "T") {
return '(' + SL + '|' + C + ')';
}
else {
return '(' + SL + '|' + '(' + SR + '&' + C + ')' + ')';
}
}
};
cout << "Yes" << endl;
cout << DFS(DFS,N,0) << endl;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int _;
cin >> _;
while(_--) {
Solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
input:
7 2 0001 2 0111 2 1111 3 00010111 1 10 2 0101 5 00000000000000000000000000000001
output:
Yes (a&b) Yes (a|b) Yes T Yes ((a&b)|((a|b)&c)) No Yes (a|(a&b)) Yes ((((a&b)&c)&d)&e)
result:
ok 7 lines, tightest: 4 out of 14 (7 test cases)
Test #2:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
4 1 00 1 10 1 01 1 11
output:
Yes F No Yes a Yes T
result:
ok 4 lines, tightest: 0 out of 11 (4 test cases)
Test #3:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
16 2 0000 2 1000 2 0100 2 1100 2 0010 2 1010 2 0110 2 1110 2 0001 2 1001 2 0101 2 1101 2 0011 2 1011 2 0111 2 1111
output:
Yes F No No No No No No No Yes (a&b) No Yes (a|(a&b)) No Yes b No Yes (a|b) Yes T
result:
ok 16 lines, tightest: 2 out of 12 (16 test cases)
Test #4:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
256 3 00000000 3 10000000 3 01000000 3 11000000 3 00100000 3 10100000 3 01100000 3 11100000 3 00010000 3 10010000 3 01010000 3 11010000 3 00110000 3 10110000 3 01110000 3 11110000 3 00001000 3 10001000 3 01001000 3 11001000 3 00101000 3 10101000 3 01101000 3 11101000 3 00011000 3 10011000 3 01011000...
output:
Yes F No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No ...
result:
ok 256 lines, tightest: 6 out of 14 (256 test cases)
Test #5:
score: 0
Accepted
time: 22ms
memory: 3584kb
input:
65536 4 0000000000000000 4 1000000000000000 4 0100000000000000 4 1100000000000000 4 0010000000000000 4 1010000000000000 4 0110000000000000 4 1110000000000000 4 0001000000000000 4 1001000000000000 4 0101000000000000 4 1101000000000000 4 0011000000000000 4 1011000000000000 4 0111000000000000 4 1111000...
output:
Yes F No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No ...
result:
ok 65536 lines, tightest: 14 out of 18 (65536 test cases)
Test #6:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
168 4 0000000000000000 4 0000000000000001 4 0000000000000011 4 0000000000000101 4 0000000000000111 4 0000000000001111 4 0000000000010001 4 0000000000010011 4 0000000000010101 4 0000000000010111 4 0000000000011111 4 0000000000110011 4 0000000000110111 4 0000000000111111 4 0000000001010101 4 000000000...
output:
Yes F Yes (((a&b)&c)&d) Yes ((b&c)&d) Yes (((a|(a&b))&c)&d) Yes (((a|b)&c)&d) Yes (c&d) Yes (((a&b)|((a&b)&c))&d) Yes (((a&b)|(b&c))&d) Yes (((a&b)|((a|(a&b))&c))&d) Yes (((a&b)|((a|b)&c))&d) Yes (((a&b)|c)&d) Yes ((b|(b&c))&d) Yes ((b|((a|b)&c))&d) Yes ((b|c)&d) Yes (((a|(a&b))|((a|(a&b))&c))&d) Ye...
result:
ok 168 lines, tightest: 14 out of 18 (168 test cases)
Test #7:
score: -100
Wrong Answer
time: 11ms
memory: 3584kb
input:
7581 5 00000000000000000000000000000000 5 00000000000000000000000000000001 5 00000000000000000000000000000011 5 00000000000000000000000000000101 5 00000000000000000000000000000111 5 00000000000000000000000000001111 5 00000000000000000000000000010001 5 00000000000000000000000000010011 5 0000000000000...
output:
Yes F Yes ((((a&b)&c)&d)&e) Yes (((b&c)&d)&e) Yes ((((a|(a&b))&c)&d)&e) Yes ((((a|b)&c)&d)&e) Yes ((c&d)&e) Yes ((((a&b)|((a&b)&c))&d)&e) Yes ((((a&b)|(b&c))&d)&e) Yes ((((a&b)|((a|(a&b))&c))&d)&e) Yes ((((a&b)|((a|b)&c))&d)&e) Yes ((((a&b)|c)&d)&e) Yes (((b|(b&c))&d)&e) Yes (((b|((a|b)&c))&d)&e) Ye...
result:
wrong answer 27 operations, you can't use more than 26 operations (test case 4947)