CF1896H2.Cyclic Hamming (Hard Version)

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The only difference between the two versions is the constraint on kk. You can make hacks only if all versions of the problem are solved.

In this statement, all strings are 00-indexed.

For two strings aa, bb of the same length pp, we define the following definitions:

  • The hamming distance between aa and bb, denoted as h(a,b)h(a, b), is defined as the number of positions ii such that 0≤i<p0 \le i \lt p and ai≠bia_i \ne b_i.
  • bb is a cyclic shift of aa if there exists some 0≤k<p0 \leq k \lt p such that b(i+k) mod p=aib_{(i+k) \bmod p} = a_i for all 0≤i<p0 \le i \lt p. Here x mod yx \bmod y denotes the remainder from dividing xx by yy.

You are given two binary strings ss and tt of length 2k+12^{k+1} each. Both strings may contain missing characters (denoted by the character '?'). Your task is to count the number of ways to replace the missing characters in both strings with the characters '0' or '1' such that:

  • Each string ss and tt contains exactly 2k2^k occurrences of each character '0' and '1'
  • h(s,c)≥2kh(s, c) \ge 2^k for all strings cc that is a cyclic shift of tt.

As the result can be very large, you should print the value modulo 998 244 353998\,244\,353.

这是该问题的困难版本。两个版本之间的唯一区别在于对 kk 的约束条件。仅当该问题的所有版本均被解决时,你才可以进行 hack。

在本题陈述中,所有字符串均采用 00-索引方式。

对于两个长度均为 pp 的字符串 aa 和 bb,我们定义如下概念:

  • aa 与 bb 的汉明距离(Hamming distance),记作 h(a,b)h(a, b),定义为满足 0≤i<p0 \le i \lt p 且 ai≠bia_i \ne b_i 的下标 ii 的个数。
  • 若存在某个 0≤k<p0 \leq k \lt p,使得对所有 0≤i<p0 \le i \lt p 均有 b(i+k) mod p=aib_{(i+k) \bmod p} = a_i,则称 bb 是 aa 的一个循环移位(cyclic shift)。其中 x mod yx \bmod y 表示 xx 除以 yy 所得的余数。

给定两个长度均为 2k+12^{k+1} 的二进制字符串 ss 和 tt。这两个字符串中均可能包含缺失字符(用字符 ? 表示)。你的任务是计算将两个字符串中所有缺失字符替换为 '0' 或 '1' 的方案数,使得满足以下条件:

  • 字符串 ss 和 tt 中各自恰好包含 2k2^k 个字符 '0' 和 2k2^k 个字符 '1';
  • 对 tt 的任意一个循环移位字符串 cc,均有 h(s,c)≥2kh(s, c) \ge 2^k。

由于结果可能非常大,请输出答案对 998 244 353998\,244\,353 取模后的值。

输入格式

The first line of the input contains a single integer kk (1≤k≤121 \le k \le 12).

The second line of the input contains string ss of size 2k+12^{k+1}, consisting of the characters '0', '1' and '?'.

The third line of the input contains string tt of size 2k+12^{k+1}, consisting of the characters '0', '1' and '?'.

It is guaranteed that both strings ss and tt contains no more than 2k2^k character '0' or '1'.

输入的第一行包含一个整数 kk(1≤k≤121 \le k \le 12)。

输入的第二行包含一个长度为 2k+12^{k+1} 的字符串 ss,由字符 '0'、'1' 和 '?' 组成。

输入的第三行包含一个长度为 2k+12^{k+1} 的字符串 tt,由字符 '0'、'1' 和 '?' 组成。

保证字符串 ss 和 tt 中字符 '0' 与 '1' 的总数均不超过 2k2^k。

输出格式

Print a single integer — the answer to the problem modulo 998 244 353998\,244\,353.

输出一个整数——该问题答案对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

    1
    0011
    0101

    输出#1

    1
  • 输入#2

    1
    0011
    0110

    输出#2

    0
  • 输入#3

    1
    0??1
    01??

    输出#3

    2
  • 输入#4

    2
    000?????
    01010101

    输出#4

    3
  • 输入#5

    2
    0???????
    1???????

    输出#5

    68
  • 输入#6

    5
    0101010101010101010101010101010101010101010101010101010101010101
    ????????????????????????????????????????????????????????????????

    输出#6

    935297567

说明/提示

In the first example, we can check that the condition h(s,c)≥2kh(s, c) \ge 2^k for all cyclic shift cc of tt is satisfied. In particular:

  • for c=0101c = \mathtt{0101}, h(s,c)=h(0110,0101)=2≥21h(s, c) = h(\mathtt{0110}, \mathtt{0101}) = 2 \ge 2^1;
  • for c=1010c = \mathtt{1010}, h(s,c)=h(0110,1010)=2≥21h(s, c) = h(\mathtt{0110}, \mathtt{1010}) = 2 \ge 2^1.

In the second example, there exists a cycle shift cc of tt such that h(s,c)<2kh(s, c) \lt 2^k (in particular, c=0011c = \mathtt{0011}, and h(s,c)=h(0011,0011)=0h(s, c) = h(\mathtt{0011}, \mathtt{0011}) = 0).

In the third example, there are 22 possible ways to recover the missing characters:

  • s=0101s = \mathtt{0101}, t=0110t = \mathtt{0110};
  • s=0011s = \mathtt{0011}, t=0101t = \mathtt{0101}.

In the fourth example, there are 33 possible ways to recover the missing characters:

  • s=00011110s = \mathtt{00011110}, t=01010101t = \mathtt{01010101};
  • s=00011011s = \mathtt{00011011}, t=01010101t = \mathtt{01010101};
  • s=00001111s = \mathtt{00001111}, t=01010101t = \mathtt{01010101}.

在第一个例子中,我们可以验证:对 tt 的所有循环移位 cc,条件 h(s,c)≥2kh(s, c) \ge 2^k 均成立。具体而言:

  • 当 c=0101c = \mathtt{0101} 时,h(s,c)=h(0110,0101)=2≥21h(s, c) = h(\mathtt{0110}, \mathtt{0101}) = 2 \ge 2^1;
  • 当 c=1010c = \mathtt{1010} 时,h(s,c)=h(0110,1010)=2≥21h(s, c) = h(\mathtt{0110}, \mathtt{1010}) = 2 \ge 2^1。

在第二个例子中,存在 tt 的某个循环移位 cc,使得 h(s,c)<2kh(s, c) \lt 2^k(特别地,取 c=0011c = \mathtt{0011},则 h(s,c)=h(0011,0011)=0h(s, c) = h(\mathtt{0011}, \mathtt{0011}) = 0)。

在第三个例子中,有 22 种可能的方式恢复缺失的字符:

  • s=0101s = \mathtt{0101},t=0110t = \mathtt{0110};
  • s=0011s = \mathtt{0011},t=0101t = \mathtt{0101}。

在第四个例子中,有 33 种可能的方式恢复缺失的字符:

  • s=00011110s = \mathtt{00011110},t=01010101t = \mathtt{01010101};
  • s=00011011s = \mathtt{00011011},t=01010101t = \mathtt{01010101};
  • s=00001111s = \mathtt{00001111},t=01010101t = \mathtt{01010101}。

输入解题思路,AI测评打分。不知道怎么写?

首页