310645: CF1864E. Guess Game

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Guess Gametime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Carol has a sequence $s$ of $n$ 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 $i_a$ and $i_b$ within the range $[1, n]$, and set $a=s_{i_a}$, $b=s_{i_b}$. Please note that $i_a$ and $i_b$ may coincide.

Carol will tell:

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

Please note that Carol will not tell any information about $s$ 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 < b$, $a > b$, or $a = 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<b$", "$a>b$", or "$a=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\,353$.

Formally, let $M = 998\,244\,353$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.

Input

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

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

The second line of each testcase contains $n$ integers $s_1,s_2,\ldots, s_n$ ($0 \le s_i < 2^{30}$).

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

Output

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

ExampleInput
4
2
2 3
3
0 0 0
3
9 9 6
8
34124838 0 113193378 8 321939321 113193378 9463828 99
Output
499122179
1
332748120
77987843
Note

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

  1. $i_a=1$, $i_b=1$, $a=2$, $b=2$, the number of turns is $2$;
  2. $i_a=1$, $i_b=2$, $a=2$, $b=3$, the number of turns is $3$;
  3. $i_a=2$, $i_b=1$, $a=3$, $b=2$, the number of turns is $2$;
  4. $i_a=2$, $i_b=2$, $a=3$, $b=3$, the number of turns is $3$.

The expected number of turns is $\frac{2+3+2+3}{4}=\frac{5}{2}=499122179\pmod{998244353}$.

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

In the first turn, Alice thinks, "I know that $a=2, a\mid b=2$. I can infer that $b=0$ or $b=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\mid b=2$. I can infer that $a=0$ or $a=2$. But if $a=0$, then Alice would have already said that $a<b$ in the first turn, but she hasn't. So $a=2$". Thus, he says, "I know, $a=b$". The game ends.

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

Output

题目大意:
Carol有一个由n个非负整数组成的序列s。她想和Alice和Bob玩“猜谜游戏”。

为了玩游戏,Carol将随机选择两个整数索引i_a和i_b在[1,n]范围内,并设置a = s_ia,b = s_ib。请注意,i_a和i_b可能重合。

Carol将告诉:
- Alice的值为a;
- Bob的值为b;
- Alice和Bob的值为a | b,其中|表示按位或操作。

请注意,Carol不会告诉Alice或Bob有关s的任何信息。

然后开始猜测。两名玩家轮流猜测,Alice先开始。两个玩家的目标都是确定以下哪一个是正确的:a < b,a > b,还是a = b。

轮到他们时,玩家可以执行以下两件事之一:
- 说“我不知道”,并将轮次交给另一位玩家;
- 说“我知道”,然后回答“a < b”,“a > b”或“a = b”;然后游戏结束。

Alice和Bob会听到对方说的话,并可以利用这些信息来推断答案。Alice和Bob都很聪明,只有在完全确定的情况下才会说“我知道”。

您需要计算玩家在游戏中采取的轮数期望值。答案模998244353输出。

输入输出数据格式:
每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤10^5)。接下来是每个测试用例的描述。

每个测试用例的第一行包含一个整数n(1≤n≤2 * 10^5)。

每个测试用例的第二行包含n个整数s1,s2,...,sn(0≤si < 2^30)。

保证所有测试用例的n之和不超过2 * 10^5。

对于每个测试用例,输出一个整数——问题的答案模998244353。题目大意: Carol有一个由n个非负整数组成的序列s。她想和Alice和Bob玩“猜谜游戏”。 为了玩游戏,Carol将随机选择两个整数索引i_a和i_b在[1,n]范围内,并设置a = s_ia,b = s_ib。请注意,i_a和i_b可能重合。 Carol将告诉: - Alice的值为a; - Bob的值为b; - Alice和Bob的值为a | b,其中|表示按位或操作。 请注意,Carol不会告诉Alice或Bob有关s的任何信息。 然后开始猜测。两名玩家轮流猜测,Alice先开始。两个玩家的目标都是确定以下哪一个是正确的:a < b,a > b,还是a = b。 轮到他们时,玩家可以执行以下两件事之一: - 说“我不知道”,并将轮次交给另一位玩家; - 说“我知道”,然后回答“a < b”,“a > b”或“a = b”;然后游戏结束。 Alice和Bob会听到对方说的话,并可以利用这些信息来推断答案。Alice和Bob都很聪明,只有在完全确定的情况下才会说“我知道”。 您需要计算玩家在游戏中采取的轮数期望值。答案模998244353输出。 输入输出数据格式: 每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤10^5)。接下来是每个测试用例的描述。 每个测试用例的第一行包含一个整数n(1≤n≤2 * 10^5)。 每个测试用例的第二行包含n个整数s1,s2,...,sn(0≤si < 2^30)。 保证所有测试用例的n之和不超过2 * 10^5。 对于每个测试用例,输出一个整数——问题的答案模998244353。

加入题单

上一题 下一题 算法标签: