// EXPLOSION!
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include<chrono>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define mp make_pair
#define rsz resize
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define cont continue
#define endl '\n'
//#define ednl '\n'
#define test int testc;cin>>testc;while(testc--)
#define pr(a, b) trav(x,a)cerr << x << b; cerr << endl;
#define message cout << "Hello World" << endl;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4000000000000000000LL;
const ll inf = 1000000007;//998244353
void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) {
F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; }
}void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; }
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef arwaeystoamneg
if (sz(s))
{
freopen((s + ".in").c_str(), "r", stdin);
if (s != "test1")
freopen((s + ".out").c_str(), "w", stdout);
}
#endif
}
int popcount(ll a)
{
int count = 0;
while (a)
{
count += (a & 1);
a >>= 1;
}
return count;
}
const int SZ = 20;
int n, q;
string a;
ll super[1 << SZ], sub[1 << SZ];
int main()
{
setIO("test1");
cin >> n >> q >> a;
F0R(mask, 1 << n)
{
sub[mask] = super[mask] = a[mask] - '0';
}
F0R(i, n)
{
F0R(mask, 1 << n)
{
if (mask & (1 << i))continue;
sub[mask ^ (1 << i)] += sub[mask];
}
}
F0R(i, n)
{
R0F(mask, 1 << n)
{
if (mask & (1 << i))
{
super[mask ^ (1 << i)] += super[mask];
}
}
}
F0R(l, q)
{
string b;
cin >> b;
reverse(all(b));
vi zero, one, quest;
F0R(i, n)if (b[i] == '0')zero.pb(i); else if (b[i] == '1')one.pb(i); else quest.pb(i);
ll ans = 0;
if (sz(zero) <= n / 3)
{
int mask1 = 0;
trav(x, one)mask1 ^= (1 << x);
int m = sz(zero);
int cur = mask1;
F0R(mask, 1 << m)
{
if (popcount(mask) & 1)ans -= super[cur];
else ans += super[cur];
int t = 0;
while (t < m)
{
if (cur & (1 << zero[t]))
{
cur ^= (1 << zero[t]);
t++;
}
else
{
cur ^= (1 << zero[t]);
break;
}
}
}
}
else if (sz(one) <= n / 3)
{
int mask1 = 0;
trav(x, quest)mask1 ^= (1 << x);
int m = sz(one);
int cur = mask1;
F0R(mask, 1 << m)
{
if (popcount(mask) & 1)ans -= sub[cur];
else ans += sub[cur];
int t = 0;
while (t < m)
{
if (cur & (1 << one[t]))
{
cur ^= (1 << one[t]);
t++;
}
else
{
cur ^= (1 << one[t]);
break;
}
}
}
}
else
{
int mask1 = 0;
trav(x, one)mask1 ^= (1 << x);
int m = sz(quest);
int cur = mask1;
F0R(mask, 1 << m)
{
int t = 0;
while (t < m)
{
if (cur & (1 << quest[t]))
{
cur ^= (1 << quest[t]);
t++;
}
else
{
cur ^= (1 << quest[t]);
break;
}
}
ans += a[cur] - '0';
}
}
cout << abs(ans) << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
488 ms |
4352 KB |
Output is correct |
12 |
Correct |
453 ms |
3940 KB |
Output is correct |
13 |
Correct |
521 ms |
3140 KB |
Output is correct |
14 |
Correct |
568 ms |
3380 KB |
Output is correct |
15 |
Correct |
466 ms |
4280 KB |
Output is correct |
16 |
Correct |
573 ms |
3412 KB |
Output is correct |
17 |
Correct |
577 ms |
3492 KB |
Output is correct |
18 |
Correct |
300 ms |
5332 KB |
Output is correct |
19 |
Correct |
461 ms |
2320 KB |
Output is correct |
20 |
Correct |
473 ms |
3952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
488 ms |
4352 KB |
Output is correct |
12 |
Correct |
453 ms |
3940 KB |
Output is correct |
13 |
Correct |
521 ms |
3140 KB |
Output is correct |
14 |
Correct |
568 ms |
3380 KB |
Output is correct |
15 |
Correct |
466 ms |
4280 KB |
Output is correct |
16 |
Correct |
573 ms |
3412 KB |
Output is correct |
17 |
Correct |
577 ms |
3492 KB |
Output is correct |
18 |
Correct |
300 ms |
5332 KB |
Output is correct |
19 |
Correct |
461 ms |
2320 KB |
Output is correct |
20 |
Correct |
473 ms |
3952 KB |
Output is correct |
21 |
Correct |
476 ms |
4368 KB |
Output is correct |
22 |
Correct |
544 ms |
4552 KB |
Output is correct |
23 |
Correct |
618 ms |
3728 KB |
Output is correct |
24 |
Correct |
678 ms |
3540 KB |
Output is correct |
25 |
Correct |
525 ms |
5452 KB |
Output is correct |
26 |
Correct |
639 ms |
3952 KB |
Output is correct |
27 |
Correct |
665 ms |
3936 KB |
Output is correct |
28 |
Correct |
337 ms |
6368 KB |
Output is correct |
29 |
Correct |
519 ms |
2408 KB |
Output is correct |
30 |
Correct |
552 ms |
4688 KB |
Output is correct |
31 |
Correct |
613 ms |
4492 KB |
Output is correct |
32 |
Correct |
643 ms |
4428 KB |
Output is correct |
33 |
Correct |
519 ms |
3364 KB |
Output is correct |
34 |
Correct |
673 ms |
3620 KB |
Output is correct |
35 |
Correct |
699 ms |
3932 KB |
Output is correct |
36 |
Correct |
304 ms |
2476 KB |
Output is correct |
37 |
Correct |
506 ms |
4512 KB |
Output is correct |
38 |
Correct |
511 ms |
2432 KB |
Output is correct |
39 |
Correct |
622 ms |
3696 KB |
Output is correct |
40 |
Correct |
628 ms |
3544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
81 ms |
18072 KB |
Output is correct |
12 |
Correct |
87 ms |
18200 KB |
Output is correct |
13 |
Correct |
106 ms |
18124 KB |
Output is correct |
14 |
Correct |
146 ms |
18136 KB |
Output is correct |
15 |
Correct |
89 ms |
18236 KB |
Output is correct |
16 |
Correct |
135 ms |
18076 KB |
Output is correct |
17 |
Correct |
122 ms |
18080 KB |
Output is correct |
18 |
Correct |
66 ms |
18220 KB |
Output is correct |
19 |
Correct |
83 ms |
17944 KB |
Output is correct |
20 |
Correct |
104 ms |
18252 KB |
Output is correct |
21 |
Correct |
122 ms |
18080 KB |
Output is correct |
22 |
Correct |
138 ms |
18184 KB |
Output is correct |
23 |
Correct |
98 ms |
18028 KB |
Output is correct |
24 |
Correct |
135 ms |
18116 KB |
Output is correct |
25 |
Correct |
126 ms |
18076 KB |
Output is correct |
26 |
Correct |
66 ms |
17956 KB |
Output is correct |
27 |
Correct |
90 ms |
18108 KB |
Output is correct |
28 |
Correct |
98 ms |
17944 KB |
Output is correct |
29 |
Correct |
102 ms |
18088 KB |
Output is correct |
30 |
Correct |
114 ms |
18072 KB |
Output is correct |
31 |
Correct |
86 ms |
18020 KB |
Output is correct |
32 |
Correct |
150 ms |
18208 KB |
Output is correct |
33 |
Correct |
130 ms |
18100 KB |
Output is correct |
34 |
Correct |
68 ms |
17944 KB |
Output is correct |
35 |
Correct |
114 ms |
18096 KB |
Output is correct |
36 |
Correct |
114 ms |
18088 KB |
Output is correct |
37 |
Correct |
114 ms |
18084 KB |
Output is correct |
38 |
Correct |
113 ms |
18068 KB |
Output is correct |
39 |
Correct |
112 ms |
18120 KB |
Output is correct |
40 |
Correct |
107 ms |
18076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
284 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
488 ms |
4352 KB |
Output is correct |
12 |
Correct |
453 ms |
3940 KB |
Output is correct |
13 |
Correct |
521 ms |
3140 KB |
Output is correct |
14 |
Correct |
568 ms |
3380 KB |
Output is correct |
15 |
Correct |
466 ms |
4280 KB |
Output is correct |
16 |
Correct |
573 ms |
3412 KB |
Output is correct |
17 |
Correct |
577 ms |
3492 KB |
Output is correct |
18 |
Correct |
300 ms |
5332 KB |
Output is correct |
19 |
Correct |
461 ms |
2320 KB |
Output is correct |
20 |
Correct |
473 ms |
3952 KB |
Output is correct |
21 |
Correct |
476 ms |
4368 KB |
Output is correct |
22 |
Correct |
544 ms |
4552 KB |
Output is correct |
23 |
Correct |
618 ms |
3728 KB |
Output is correct |
24 |
Correct |
678 ms |
3540 KB |
Output is correct |
25 |
Correct |
525 ms |
5452 KB |
Output is correct |
26 |
Correct |
639 ms |
3952 KB |
Output is correct |
27 |
Correct |
665 ms |
3936 KB |
Output is correct |
28 |
Correct |
337 ms |
6368 KB |
Output is correct |
29 |
Correct |
519 ms |
2408 KB |
Output is correct |
30 |
Correct |
552 ms |
4688 KB |
Output is correct |
31 |
Correct |
613 ms |
4492 KB |
Output is correct |
32 |
Correct |
643 ms |
4428 KB |
Output is correct |
33 |
Correct |
519 ms |
3364 KB |
Output is correct |
34 |
Correct |
673 ms |
3620 KB |
Output is correct |
35 |
Correct |
699 ms |
3932 KB |
Output is correct |
36 |
Correct |
304 ms |
2476 KB |
Output is correct |
37 |
Correct |
506 ms |
4512 KB |
Output is correct |
38 |
Correct |
511 ms |
2432 KB |
Output is correct |
39 |
Correct |
622 ms |
3696 KB |
Output is correct |
40 |
Correct |
628 ms |
3544 KB |
Output is correct |
41 |
Correct |
81 ms |
18072 KB |
Output is correct |
42 |
Correct |
87 ms |
18200 KB |
Output is correct |
43 |
Correct |
106 ms |
18124 KB |
Output is correct |
44 |
Correct |
146 ms |
18136 KB |
Output is correct |
45 |
Correct |
89 ms |
18236 KB |
Output is correct |
46 |
Correct |
135 ms |
18076 KB |
Output is correct |
47 |
Correct |
122 ms |
18080 KB |
Output is correct |
48 |
Correct |
66 ms |
18220 KB |
Output is correct |
49 |
Correct |
83 ms |
17944 KB |
Output is correct |
50 |
Correct |
104 ms |
18252 KB |
Output is correct |
51 |
Correct |
122 ms |
18080 KB |
Output is correct |
52 |
Correct |
138 ms |
18184 KB |
Output is correct |
53 |
Correct |
98 ms |
18028 KB |
Output is correct |
54 |
Correct |
135 ms |
18116 KB |
Output is correct |
55 |
Correct |
126 ms |
18076 KB |
Output is correct |
56 |
Correct |
66 ms |
17956 KB |
Output is correct |
57 |
Correct |
90 ms |
18108 KB |
Output is correct |
58 |
Correct |
98 ms |
17944 KB |
Output is correct |
59 |
Correct |
102 ms |
18088 KB |
Output is correct |
60 |
Correct |
114 ms |
18072 KB |
Output is correct |
61 |
Correct |
86 ms |
18020 KB |
Output is correct |
62 |
Correct |
150 ms |
18208 KB |
Output is correct |
63 |
Correct |
130 ms |
18100 KB |
Output is correct |
64 |
Correct |
68 ms |
17944 KB |
Output is correct |
65 |
Correct |
114 ms |
18096 KB |
Output is correct |
66 |
Correct |
114 ms |
18088 KB |
Output is correct |
67 |
Correct |
114 ms |
18084 KB |
Output is correct |
68 |
Correct |
113 ms |
18068 KB |
Output is correct |
69 |
Correct |
112 ms |
18120 KB |
Output is correct |
70 |
Correct |
107 ms |
18076 KB |
Output is correct |
71 |
Correct |
782 ms |
22888 KB |
Output is correct |
72 |
Correct |
857 ms |
23144 KB |
Output is correct |
73 |
Correct |
1144 ms |
23732 KB |
Output is correct |
74 |
Correct |
1691 ms |
43544 KB |
Output is correct |
75 |
Correct |
840 ms |
45496 KB |
Output is correct |
76 |
Correct |
1815 ms |
43736 KB |
Output is correct |
77 |
Correct |
1596 ms |
43552 KB |
Output is correct |
78 |
Correct |
453 ms |
47492 KB |
Output is correct |
79 |
Correct |
691 ms |
41556 KB |
Output is correct |
80 |
Correct |
782 ms |
44668 KB |
Output is correct |
81 |
Correct |
1137 ms |
44528 KB |
Output is correct |
82 |
Correct |
1788 ms |
43472 KB |
Output is correct |
83 |
Correct |
817 ms |
42676 KB |
Output is correct |
84 |
Correct |
1645 ms |
43404 KB |
Output is correct |
85 |
Correct |
1562 ms |
43632 KB |
Output is correct |
86 |
Correct |
441 ms |
41408 KB |
Output is correct |
87 |
Correct |
753 ms |
44460 KB |
Output is correct |
88 |
Correct |
758 ms |
41420 KB |
Output is correct |
89 |
Correct |
1117 ms |
43204 KB |
Output is correct |
90 |
Correct |
1033 ms |
43472 KB |
Output is correct |
91 |
Correct |
855 ms |
42652 KB |
Output is correct |
92 |
Correct |
1913 ms |
43712 KB |
Output is correct |
93 |
Correct |
1558 ms |
43680 KB |
Output is correct |
94 |
Correct |
456 ms |
41360 KB |
Output is correct |
95 |
Correct |
1297 ms |
43332 KB |
Output is correct |
96 |
Correct |
1401 ms |
43492 KB |
Output is correct |
97 |
Correct |
1327 ms |
43488 KB |
Output is correct |
98 |
Correct |
1320 ms |
43432 KB |
Output is correct |
99 |
Correct |
1354 ms |
43376 KB |
Output is correct |
100 |
Correct |
1384 ms |
43384 KB |
Output is correct |