#arc170e. E - BDFS

E - BDFS

Score: 800800 points

问题陈述

给定整数 NNPP

存在一个包含 NN 个顶点和 NN 条边的图,其中每个顶点被标记为 11NN。第 ii 条边双向连接顶点 iii+1i+1。在这里,顶点 N+1N+1 指的是顶点 11

执行以下算法以获得长度为 NN 的序列 D=(D1,D2,,DN)D=(D_1,D_2,\ldots,D_N)

  • 将长度为 NN 的整数序列 DD 设置为 D=(D1,,DN)=(1,,1)D=(D_1,\ldots,D_N)=(-1,\ldots,-1)。同时,将数字对的序列 QQ 设置为 Q=((1,0))Q=((1,0))。当 QQ 不为空时,重复以下过程:

    • (v,d)(v,d)QQ 的第一个元素。移除这个元素。

    • 如果 Dv=1D_v = -1,那么设置 Dv:=dD_v := d,并且对于每个与顶点 vv 相邻且 Dx=1D_x=-1 的顶点 xx,执行以下过程。如果有多个这样的 xx 满足条件,按照顶点编号的升序处理它们:

      1. 以概率 P100\frac{P}{100},将 (x,d+1)(x,d+1) 添加到 QQ前面
      2. 如果 (x,d+1)(x,d+1) 没有被添加到 QQ 的前面,就将它添加到 QQ后面

找到最终获得的序列 DD 的元素之和的期望值,对 998244353998244353 取模。

解决给定的每个 TT 个测试用例。

期望值的定义 mod 998244353\text{mod } 998244353 可以证明要找到的期望值总是一个有理数。此外,这个问题的约束条件保证如果该值表示为一个不可约分数 PQ\frac{P}{Q},那么 QQ 不能被 998244353998244353 整除。在这里,存在一个唯一的整数 RR00998244352998244352 之间,包括 00998244352998244352,使得 R×QP(mod998244353)R\times Q \equiv P\pmod{998244353}。提供这个 RR 作为答案。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given integers NN and PP.

There is a graph with NN vertices and NN edges, where each vertex is labeled 11 to NN. The ii-th edge connects vertices ii and i+1i+1 bidirectionally. Here, vertex N+1N+1 refers to vertex 11.

Perform the following algorithm to obtain a sequence D=(D1,D2,,DN)D=(D_1,D_2,\ldots,D_N) of length NN:

  • Set an integer sequence DD of length NN to D=(D1,,DN)=(1,,1)D=(D_1,\ldots,D_N)=(-1,\ldots,-1). Also, set a sequence QQ of number pairs to Q=((1,0))Q=((1,0)). Repeat the following process while QQ is not empty:

    • Let (v,d)(v,d) be the first element of QQ. Remove this element.

    • If Dv=1D_v = -1, then set Dv:=dD_v := d, and for each vertex xx adjacent to vertex vv such that Dx=1D_x=-1, perform the following process. If there are multiple such xx that satisfy the condition, process them in ascending order of vertex number:

      1. With probability P100\frac{P}{100}, add (x,d+1)(x,d+1) to the front of QQ.
      2. If (x,d+1)(x,d+1) was not added to the front of QQ, add it to the end of QQ.

Find the expected value of the sum of the elements of the final sequence DD obtained, modulo 998244353998244353.

Solve each of the TT test cases given.

Definition of expected value mod 998244353\text{mod } 998244353 It can be proved that the expected value to be found is always a rational number. Furthermore, the constraints of this problem guarantee that if that value is expressed as an irreducible fraction PQ\frac{P}{Q}, then QQ is not divisible by 998244353998244353. Here, there is a unique integer RR between 00 and 998244352998244352, inclusive, such that R×QP(mod998244353)R\times Q \equiv P\pmod{998244353}. Provide this RR as the answer.

Constraints

  • 1T1041 \leq T \leq 10^4
  • 3N10183 \leq N \leq 10^{18}
  • 1P991\leq P \leq 99
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

\vdots

caseT\mathrm{case}_T

Each case is given in the following format:

NN PP

Output

Print TT lines. The ii-th line (1iT)(1 \leq i \leq T) should contain the answer for the ii-th test case.

Sample Input 1

3
3 50
4 1
1000000000000000000 70

Sample Output 1

499122179
595552585
760296751

In the first test case, the algorithm may operate as follows:

  • Initially, D=(1,1,1)D=(-1,-1,-1) and Q=((1,0))Q=((1,0)). Remove the first element (1,0)(1,0) from QQ.
  • D1=1D_1 = -1, so set D1:=0D_1 := 0. The vertices xx adjacent to vertex 11 such that Dx=1D_x= -1 are 22 and 33.
  • Add (2,1)(2,1) to the front of QQ. Add (3,1)(3,1) to the end of QQ. Now Q=((2,1),(3,1))Q=((2,1),(3,1)).
  • Remove the first element (2,1)(2,1) from QQ.
  • D2=1D_2 = -1, so set D2:=1D_2 := 1. The vertex xx adjacent to vertex 22 such that Dx=1D_x= -1 is 33.
  • Add (3,2)(3,2) to the front of QQ. Now Q=((3,2),(3,1))Q=((3,2),(3,1)).
  • Remove the first element (3,2)(3,2) from QQ.
  • D3=1D_3 = -1, so set D3:=2D_3 := 2. There are no vertices xx adjacent to vertex 33 such that Dx=1D_x= -1, so do nothing.
  • Remove the first element (3,1)(3,1) from QQ.
  • D3=2D_3 =2, so do nothing.
  • QQ is now empty, so the process ends.

In this case, the final sequence obtained is D=(0,1,2)D=(0,1,2). The probability that the algorithm operates as described above is 18\frac{1}{8}, and the expected sum of the elements of DD is 52\frac{5}{2}.