답안 #142630

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
142630 2019-08-10T06:54:56 Z DrumpfTheGodEmperor Snake Escaping (JOI18_snake_escaping) C++14
0 / 100
3 ms 380 KB
#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int L = (1 << 20) + 5;
int l, q, cost[L], defMask = 0, sumSubset[L], sumSubsetOf[L];
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> l >> q;
	fw (i, 0, (1 << l)) {
		char c;
		cin >> c;
		cost[i] = c - '0';
	}
	//sumSubset[i] stores sum of costs of j that j & i = j.
	fw (i, 0, (1 << l)) sumSubset[i] = cost[i];
	fw (i, 0, l) fw (mask, 0, (1 << l)) {
		if (mask & (1 << i)) sumSubset[mask] += sumSubset[mask ^ (1 << i)];
	}
//	fw (mask, 0, (1 << l)) cout << "sumSubset[" << mask << "] = " << sumSubset[mask] << "\n";
	//sumSubsetOf[i] stores sum of costs of j that j & i = i
	fw (i, 0, (1 << l)) sumSubsetOf[i] = cost[i];
	fw (i, 0, l) bw (mask, (1 << l), 0) {
		if (!(mask & (1 << i))) sumSubsetOf[mask] += sumSubsetOf[mask | (1 << i)];
	}
	while (q--) {
		string t;
		cin >> t;
		reverse(t.begin(), t.end());
		int cnt0 = 0, cnt1 = 0, cntQ = 0;
		fw (i, 0, t.length()) {
			if (t[i] == '0') cnt0++;
			else if (t[i] == '1') cnt1++;
			else cntQ++;
		}
		//At least one of these is <= 6. Iterate over them.
		int ans = 0;
		defMask = 0;
		int alternateMask = 0;
		if (cntQ <= -1) {
			fw (i, 0, t.length()) {
				if (t[i] == '?') alternateMask |= (1 << i);
				else defMask |= ((t[i] - '0') << i);
			}
			//Iterate through all submasks of alternateMask and OR it into defMask
			ans += cost[defMask];
			for (int tmp = alternateMask; tmp > 0; tmp = (tmp - 1) & alternateMask) {
				int mask = (defMask | tmp);
				ans += cost[mask];
			}
		} else if (cnt0 <= -1) {
			fw (i, 0, t.length()) {
				if (t[i] == '0') alternateMask |= (1 << i);
				else {
					if (t[i] == '1') defMask |= (1 << i);
				}
			}
			ans += sumSubsetOf[defMask];
			for (int tmp = alternateMask; tmp > 0; tmp = (tmp - 1) & alternateMask) {
				int mask = (defMask | tmp);
				if (bp(tmp) % 2 == 0) ans += sumSubsetOf[mask];
				else ans -= sumSubsetOf[mask];
			}
		} else if (cnt1 <= 6) {
//			cout << "case3\n";
//			cout << "cnt1 = " << cnt1 << "\n";
			fw (i, 0, t.length()) {
				if (t[i] == '1') alternateMask |= (1 << i);
				else {
					if (t[i] == '?') defMask |= (1 << i);
				}
			}
			if (cnt1 % 2 == 0) ans += sumSubset[defMask];
			else ans -= sumSubset[defMask];
			for (int tmp = alternateMask; tmp > 0; tmp = (tmp - 1) & alternateMask) {
				int mask = (defMask | tmp);
				if ((cnt1 - bp(tmp)) % 2 == 0) ans += sumSubset[mask];
				else ans -= sumSubset[mask];
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:45:7:
   fw (i, 0, t.length()) {
       ~~~~~~~~~~~~~~~~                 
snake_escaping.cpp:45:3: note: in expansion of macro 'fw'
   fw (i, 0, t.length()) {
   ^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:55:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:55:4: note: in expansion of macro 'fw'
    fw (i, 0, t.length()) {
    ^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:66:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:66:4: note: in expansion of macro 'fw'
    fw (i, 0, t.length()) {
    ^~
snake_escaping.cpp:11:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define fw(i, l, r) for (int i = l; i < r; i++)
snake_escaping.cpp:81:8:
    fw (i, 0, t.length()) {
        ~~~~~~~~~~~~~~~~                
snake_escaping.cpp:81:4: note: in expansion of macro 'fw'
    fw (i, 0, t.length()) {
    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Incorrect 2 ms 380 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Incorrect 2 ms 380 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Incorrect 2 ms 380 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Incorrect 2 ms 380 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Incorrect 2 ms 380 KB Output isn't correct
7 Halted 0 ms 0 KB -