CF1778D.Flexible String Revisit

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two binary strings aa and bb of length nn. In each move, the string aa is modified in the following way.

  • An index ii (1≤i≤n1 \leq i \leq n) is chosen uniformly at random. The character aia_i will be flipped. That is, if aia_i is 00, it becomes 11, and if aia_i is 11, it becomes 00.

What is the expected number of moves required to make both strings equal for the first time?

A binary string is a string, in which the character is either 0\tt{0} or 1\tt{1}.

给你两个长度为 nn 的二进制字符串 aa 和 bb。在每一步操作中,字符串 aa 按如下方式被修改:

  • 均匀随机地选择一个下标 ii(1≤i≤n1 \leq i \leq n),并将字符 aia_i 翻转:即若 aia_i 为 0\tt{0},则变为 1\tt{1};若 aia_i 为 1\tt{1},则变为 0\tt{0}。

问:使两个字符串首次相等所需的期望操作次数是多少?

二进制字符串是指仅由字符 0\tt{0} 或 1\tt{1} 组成的字符串。

输入格式

The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1061 \leq n \leq 10^6) — the length of the strings.

The second line of each test case contains the binary string aa of length nn.

The third line of each test case contains the binary string bb of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5)—— 表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \leq n \leq 10^6)—— 表示字符串的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 aa。

每个测试用例的第三行包含一个长度为 nn 的二进制字符串 bb。

保证所有测试用例的 nn 之和不超过 10610^6。

输出格式

For each test case, output a single line containing the expected number of moves modulo 998 244 353998\,244\,353.

Formally, let M=998 244 353M = 998\,244\,353. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to p⋅q−1 mod Mp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0≤x<M0 \le x \lt M and x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}.

对于每个测试用例,输出一行,包含期望移动次数对 998 244 353998\,244\,353 取模的结果。

形式化地,令 M=998 244 353M = 998\,244\,353。可以证明答案可表示为既约分数 pq\frac{p}{q},其中 pp 和 qq 为整数,且 q≢0(modM)q \not \equiv 0 \pmod{M}。请输出整数 p⋅q−1 mod Mp \cdot q^{-1} \bmod M。换言之,请输出满足 0≤x<M0 \le x \lt M 且 x⋅q≡p(modM)x \cdot q \equiv p \pmod{M} 的整数 xx。

输入输出样例

  • 输入#1

    4
    1
    0
    1
    2
    00
    00
    4
    1000
    1110
    5
    01001
    10111

    输出#1

    1
    0
    665496254
    665496277

说明/提示

In the first test case, index 11 is chosen randomly and a1a_1 is flipped. After the move, the strings aa and bb are equal. The expected number of moves is 11.

The strings aa and bb are already equal in the second test case. So, the expected number of moves is 00.

The expected number of moves for the third and fourth test cases are 563\frac{56}{3} and 1253\frac{125}{3} respectively.

在第一个测试用例中,随机选择索引 11 并翻转 a1a_1。操作后,字符串 aa 和 bb 相等。期望操作次数为 11。

在第二个测试用例中,字符串 aa 和 bb 已经相等,因此期望操作次数为 00。

第三个和第四个测试用例的期望操作次数分别为 563\frac{56}{3} 和 1253\frac{125}{3}。

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

首页