CF1864E.Guess Game

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Carol has a sequence ss of nn non-negative integers. She wants to play the "Guess Game" with Alice and Bob.

To play the game, Carol will randomly select two integer indices iai_a and ibi_b within the range [1,n][1, n], and set a=siaa=s_{i_a}, b=sibb=s_{i_b}. Please note that iai_a and ibi_b may coincide.

Carol will tell:

  • the value of aa to Alice;
  • the value of bb to Bob;
  • the value of a∣ba \mid b to both Alice and Bob, where ∣| denotes the bitwise OR operation.

Please note that Carol will not tell any information about ss to either Alice or Bob.

Then the guessing starts. The two players take turns making guesses, with Alice starting first. The goal of both players is to establish which of the following is true: a<ba \lt b, a>ba \gt b, or a=ba = b.

In their turn, a player can do one of the following two things:

  • say "I don't know", and pass the turn to the other player;
  • say "I know", followed by the answer "a<ba \lt b", "a>ba \gt b", or "a=ba=b"; then the game ends.

Alice and Bob hear what each other says, and can use this information to deduce the answer. Both Alice and Bob are smart enough and only say "I know" when they are completely sure.

You need to calculate the expected value of the number of turns taken by players in the game. Output the answer 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}.

卡罗尔有一个长度为 nn 的非负整数序列 ss。她想和爱丽丝(Alice)与鲍勃(Bob)一起玩“猜数游戏”。

游戏规则如下:卡罗尔在区间 [1,n][1, n] 内随机选取两个整数下标 iai_a 和 ibi_b,并令 a=siaa = s_{i_a}、b=sibb = s_{i_b}。注意:iai_a 与 ibi_b 可以相同。

卡罗尔会告知:

  • 将 aa 的值告诉爱丽丝;
  • 将 bb 的值告诉鲍勃;
  • 将 a∣ba \mid b 的值同时告诉爱丽丝和鲍勃,其中 ∣| 表示按位或运算。

注意:卡罗尔不会向爱丽丝或鲍勃透露关于序列 ss 的任何其他信息。

随后开始猜测环节。两位玩家轮流发言,爱丽丝先手。双方的目标是判断以下三种关系中哪一种成立:a<ba \lt b、a>ba \gt b,还是 a=ba = b。

在自己的回合中,一名玩家可执行以下两种操作之一:

  • 说“我不知道”,并将回合交给另一名玩家;
  • 说“我知道”,并紧接着给出答案:“a<ba \lt b”、“a>ba \gt b” 或 “a=ba = b”;此时游戏立即结束。

爱丽丝和鲍勃能听到对方所说的话,并可据此推理得出答案。两人均足够聪明,且仅当完全确定答案时才会说“我知道”。

你需要计算游戏中玩家所用回合数的期望值,并将结果对 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。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). The description of the test cases follows.

The first line of each testcase contains a single integer nn (1≤n≤2⋅1051 \le n \le 2\cdot 10^5).

The second line of each testcase contains nn integers s1,s2,…,sns_1,s_2,\ldots, s_n (0≤si<2300 \le s_i \lt 2^{30}).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1051 \le t \le 10^5)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2\cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 s1,s2,…,sns_1,s_2,\ldots, s_n(0≤si<2300 \le s_i \lt 2^{30})。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print a single integer — the answer to the problem modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——问题答案对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

    4
    2
    2 3
    3
    0 0 0
    3
    9 9 6
    8
    34124838 0 113193378 8 321939321 113193378 9463828 99

    输出#1

    499122179
    1
    332748120
    77987843

说明/提示

In the first test case, there are only 44 possible situations:

  1. ia=1i_a=1, ib=1i_b=1, a=2a=2, b=2b=2, the number of turns is 22;
  2. ia=1i_a=1, ib=2i_b=2, a=2a=2, b=3b=3, the number of turns is 33;
  3. ia=2i_a=2, ib=1i_b=1, a=3a=3, b=2b=2, the number of turns is 22;
  4. ia=2i_a=2, ib=2i_b=2, a=3a=3, b=3b=3, the number of turns is 33.

The expected number of turns is 2+3+2+34=52=499122179(mod998244353)\frac{2+3+2+3}{4}=\frac{5}{2}=499122179\pmod{998244353}.

Consider the first case, when a=2a=2, b=2b=2. The guessing process is as follows.

In the first turn, Alice thinks, "I know that a=2,a∣b=2a=2, a\mid b=2. I can infer that b=0b=0 or b=2b=2, but I am not sure which one". Thus, she says, "I don't know".

In the second turn, Bob thinks, "I know that b=2,a∣b=2b=2, a\mid b=2. I can infer that a=0a=0 or a=2a=2. But if a=0a=0, then Alice would have already said that a<ba \lt b in the first turn, but she hasn't. So a=2a=2". Thus, he says, "I know, a=ba=b". The game ends.

In the second test case, for a=0a=0, b=0b=0, Alice knows that a=ba=b immediately. The expected number of turns equals 11.

在第一个测试用例中,仅有 44 种可能的情形:

  1. ia=1i_a=1, ib=1i_b=1, a=2a=2, b=2b=2,回合数为 22;
  2. ia=1i_a=1, ib=2i_b=2, a=2a=2, b=3b=3,回合数为 33;
  3. ia=2i_a=2, ib=1i_b=1, a=3a=3, b=2b=2,回合数为 22;
  4. ia=2i_a=2, ib=2i_b=2, a=3a=3, b=3b=3,回合数为 33。

期望回合数为 2+3+2+34=52=499122179(mod998244353)\frac{2+3+2+3}{4}=\frac{5}{2}=499122179\pmod{998244353}。

考虑第一种情形,即 a=2a=2, b=2b=2 时。猜测过程如下:

在第一回合,Alice 思考:“我知道 a=2a=2,且 a∣b=2a\mid b=2。由此可推断 b=0b=0 或 b=2b=2,但我无法确定是哪一个。” 因此,她说:“我不知道。”

在第二回合,Bob 思考:“我知道 b=2b=2,且 a∣b=2a\mid b=2。由此可推断 a=0a=0 或 a=2a=2。但如果 a=0a=0,那么 Alice 在第一回合就已能断言 a<ba \lt b,而她并未如此说。因此 a=2a=2。” 于是,他说:“我知道,a=ba=b。” 游戏结束。

在第二个测试用例中,当 a=0a=0, b=0b=0 时,Alice 立即可知 a=ba=b。期望回合数为 11。

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

首页