#P9590. 「PFLOI R1」PFL 团主的 PFL 操作

    ID: 10818 远端评测题 500ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP洛谷原创O2优化矩阵加速期望

「PFLOI R1」PFL 团主的 PFL 操作

Background

After the contest ended, Zhili, Yangmai, Huamao invited Duanyao, and the four became friends from then on.


In fact, not only Duanyao, but also Zhili, Yangmai, and Huamao were once the strongest in the OI community. After getting AK again and again in one Trash Round after another, they got tired, disappeared from the scene, and quit the world of OI.

Today, seeing that Duanyao is still as talented as before, they again think of those times spent with OI... and excitement rises in their hearts.

So they found Junjun, the leader of PFLOI, and asked Junjun to give them a chance to shine again—by holding a contest of their own.

After hearing their stories, Junjun was deeply moved and gladly agreed. The five of them gathered in PFLOI.

But after Yangmai joined PFLOI, making problems randomly being too naughty, Junjun was not happy, so:

Problem Description

There are nn operations. In each operation, one of the following events happens with equal probability:

  1. Add aia_i into the team. After the operation, aia_i becomes a member.
  2. Kick aia_i out of the team.
  3. Set aia_i as an administrator.
  4. Set aia_i as a member.

Note:

  • Initially, nobody is in the team.
  • If aia_i is not in the team, then operations 2, 3, 4 have no effect.
  • If aia_i is a member, then operations 1 and 4 have no effect.
  • If aia_i is an administrator, then operations 1, 2, 3 have no effect.

Finally, output the expected number of administrators in the team, modulo 998244353998244353.

Input Format

The first line contains an integer type\text{type}.
If type\text{type} is 11, use the first input method; otherwise, use the second input method.

First input method:

The first line contains a positive integer nn.
The second line contains nn integers representing the array aa.

Second input method:

A single line contains four integers n,a0,p,qn,a_0,p,q.

Note: In the second input method, you need to compute the array aa, which satisfies ai=(ai1×p+p)modq+1a_i=(a_{i-1}\times p+p)\bmod q+ 1 (i1) (i \geq 1).

Output Format

Output one integer representing the expected number of administrators in the team, modulo 998244353998244353.

1
6
1 1 2 1 2 1

760381441
2
11 4 5 14
686292993

Hint

This problem uses bundled tests.

Subtask ID type=\text{type}= nn aia_i Score
11 11 n100n\le 100 1ai101\le a_i\le10 2525
22 n5×105n\le 5\times 10^5 1ai10181\le a_i\le 10^{18} 3535
Subtask ID type=\text{type}= nn a0,p,qa_0,p,q Score
33 22 n106n\le 10^6 1a0,p<q201\le a_0,p<q\le 20 1010
44 n1018n\le 10^{18} 1a0,p<q3×1051\le a_0,p<q\le 3\times 10^5 3030

For all testdata, 1n10181\le n\le 10^{18}.

Translated by ChatGPT 5