155 lines
5.4 KiB
C++
155 lines
5.4 KiB
C++
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
|
|
// ----- TYPES -----
|
|
typedef long long ll;
|
|
typedef pair<int, int> pii;
|
|
typedef vector<int> vi;
|
|
|
|
// ----- FILE IO -----
|
|
#define STDIN(filename) freopen(filename, "r", stdin)
|
|
#define STDOUT(filename) freopen(filename, "w", stdout)
|
|
#define FASTIO cin.tie(0)->sync_with_stdio(0)
|
|
#define STDIO(filename) \
|
|
FASTIO; \
|
|
string filenamee(filename); \
|
|
STDIN((filenamee + ".in").c_str()); \
|
|
STDOUT((filenamee + ".out").c_str())
|
|
#define NL '\n'
|
|
#define CM ", "
|
|
#define SP " "
|
|
|
|
// ----- FOR LOOPS -----
|
|
#define F(i, L, R) for (int i = L; i < R; i++)
|
|
#define FE(i, L, R) for (int i = L; i <= R; i++)
|
|
#define FD(i, L, R) for (int i = L; i > R; i--)
|
|
#define FDE(i, L, R) for (int i = L; i >= R; i--)
|
|
|
|
// ----- WEZ -----
|
|
#define takeI(n) \
|
|
int n; \
|
|
cin >> n
|
|
#define takeII(n, m) \
|
|
int n, m; \
|
|
cin >> n >> m
|
|
#define takeIII(n, m, k) \
|
|
int n, m, k; \
|
|
cin >> n >> m >> k
|
|
#define takeL(n) \
|
|
ll n; \
|
|
cin >> n
|
|
#define takeS(s) \
|
|
string s; \
|
|
cin >> s
|
|
#define takeV(v, n) \
|
|
vi v(n); \
|
|
for (auto &x : v) \
|
|
cin >> x
|
|
#define takeVL(v, n) \
|
|
vector<ll> v(n); \
|
|
for (auto &x : v) \
|
|
cin >> x
|
|
#define whileZ \
|
|
takeI(T); \
|
|
while (T--)
|
|
//
|
|
// ----- MAX MIN -----
|
|
#define REMAX(a, b) (a) = max((a), (b))
|
|
#define REMIN(a, b) (a) = min((a), (b))
|
|
|
|
// ----- PAIRS -----
|
|
#define fi first
|
|
#define se second
|
|
|
|
// ----- OUTPUT -----
|
|
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
|
|
|
|
// ----- DEBUG -----
|
|
#define DBG(vari) cerr << #vari << " = " << (vari) << endl
|
|
#define DBG2(a, b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
|
|
#define printA(a, L, R) FE(i, L, R) cerr << a[i] << (i == R ? '\n' : ' ')
|
|
#define printV(a) printA(a, 0, SZ(a) - 1)
|
|
|
|
// ----- STL DS
|
|
#define ALL(c) (c).begin(), (c).end()
|
|
#define SZ(c) ((int)(c).size())
|
|
#define SORT(c) sort(ALL(c))
|
|
#define RSORT(c) sort(ALL(c), greater<>())
|
|
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
|
|
#define pb push_back
|
|
|
|
int main() {
|
|
FASTIO;
|
|
int MOD = 998244353;
|
|
|
|
takeI(t);
|
|
while (t--) {
|
|
takeI(n);
|
|
takeS(s);
|
|
int res = 0;
|
|
|
|
// The current row is represented by a string s
|
|
// of length n. Each character of s is 0, 1, or ?.
|
|
// Replace every ? with either 0 or 1.
|
|
|
|
// After replacement, for every 1≤i<n
|
|
// the two neighboring tiles si and si+1 form a domino of weight (s_i + s_i+1)
|
|
|
|
// Note that two consecutive dominoes share exactly one tile.
|
|
// The completed row is valid if every two consecutive dominoes have different weights.
|
|
|
|
// Determine the number of different
|
|
// ways to replace all ? characters so that the completed row is valid.
|
|
// Output the answer modulo 998244353.
|
|
|
|
/**
|
|
|
|
Example of valid n=2 tiles
|
|
Since n = 2, we only have one domino, therefor all consecutive dominoes aren't equal
|
|
|
|
Example of n=3 tiles
|
|
We have 2 dominos, we should be more cafeful
|
|
Proceeding:
|
|
11 -> [2, ] -> Must be 0, otherwise next domino will have score 2
|
|
01 -> [1, ] -> Must be 1, otherwise next domino has score 1
|
|
10 -> [1, ] -> Can be anything
|
|
00 -> [0, ] -> Must be 1
|
|
|
|
The logic follows for the next step
|
|
Consider 0?1
|
|
? = 1 -> 011 (OK)
|
|
? = 0 -> 001 (OK)
|
|
Consider 0?0
|
|
? = 0 -> 000 (NOT OK)
|
|
? = 1 -? 010 (NOT OK)
|
|
|
|
// We want s_i != s_(i+2)
|
|
// Therefor s_(i+2) = 1 - s_i
|
|
|
|
??? will be all possible tiles of length 3
|
|
2 * 2
|
|
How can we find all poss
|
|
|
|
* */
|
|
// First digit can only be 0 or 1
|
|
for (int s0 = 0; s0 < 2; s0++) {
|
|
// Second digit can only be 0 or 1
|
|
for (int s1 = 0; s1 < 2; s1++) {
|
|
bool ok = true;
|
|
for (int i = 0; i < n; i++) {
|
|
// Expeced value follows the formula
|
|
// If i is even, we deal with s0, else we check against s1
|
|
// To find if we deal with s0 or !s1, we need to flip bit
|
|
// With XOR, if (a,1) then we get !a, (a,0) we get a
|
|
int v = ((i%2 == 0) ? s0 : s1) ^ ((i/2) & 1);
|
|
if (s[i] != '?' && s[i] - '0' != v) {ok = false; break;}
|
|
}
|
|
res += ok;
|
|
}
|
|
}
|
|
|
|
cout << res << NL;
|
|
}
|
|
}
|