#P11617. [PumpkinOI Round 1] 递推

    ID: 12309 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学递推2025Special JudgeO2优化生成函数Ad-hoc洛谷比赛

[PumpkinOI Round 1] 递推

Background

A simple question: what is a recurrence?

Problem Description

Define a recurrence of a sequence {a0an1}\{a_0 \dots a_{n - 1}\} as a sequence {r0rm}\{r_0\dots r_m\} that satisfies:

$$\sum_{j = 0} ^ m r_j a_{i - j} = 0, \forall i \ge m$$

mm is called the order of this recurrence. In particular, r00r_0\neq 0.

You are given the first nn terms of an infinite sequence {ai}\{a_i\} and a recurrence {bi}\{b_i\} of order nn for the sequence {ai}\{a_i\}.

Find the sum of all terms of the sequence {ai}\{a_i\}. Output the answer modulo 998244353998244353.

It can be proven that, for any input under modulo 998244353998244353, there exists a corresponding sequence in the real-number sense whose sum is convergent.

Input Format

The first line contains a positive integer nn.

The second line contains nn integers, representing the first nn terms of the sequence {ai}\{a_i\}, i.e. {a0an1}\{a_0 \dots a_{n-1}\}, under modulo 998244353998244353.

The third line contains n+1n+1 integers, representing the recurrence {b0bn}\{b_0\dots b_n\} under modulo 998244353998244353.

Output Format

Output one integer on one line, representing the sum of all terms of the sequence {ai}\{a_i\} modulo 998244353998244353.

It is guaranteed that the answer can be represented under modulo 998244353998244353. That is, if the final answer is a fraction qp\frac qp (it can be proven that the answer must be a rational number), then p≢0(mod998244353)p\not\equiv0\pmod {998244353} is guaranteed.

1
1
1 499122176

2

2
1 1
1 199648870 99824435

14
1
1
1 499122177

665496236

Hint

Sample Explanation #1

49912217612(mod998244353)499122176\equiv -\frac12\pmod {998244353}.

in,ai12×ai1=0\forall i\ge n,a_i-\frac12\times a_{i-1}=0, i.e. ai=12×ai1a_i=\frac12\times a_{i-1}. So the sequence {ai}\{a_i\} is a geometric sequence 1,12,14,1,\frac12,\frac14,\dots, and its sum converges to 22.

Sample Explanation #2

$199648870\equiv -0.6\pmod {998244353},99824435\equiv -0.3\pmod {998244353}$.

$\forall i\ge n,a_i-0.6\times a_{i-1}-0.3\times a_{i-2}=0$, i.e. ai=0.6×ai1+0.3×ai2a_i=0.6\times a_{i-1}+0.3\times a_{i-2}. After calculation, its sum converges to 1414.

This problem uses bundled/dependent subtasks.

For all subtasks, 1n5×1031\le n\le5\times 10^3, 0ai,bi<9982443530\le a_i,b_i< 998244353. In particular, b00b_0\neq 0.

Subtask ID Score nn\le Depends On
11 3030 11 None
22 22 11
33 4040 5×1035\times 10^3 1,21,2

Translated by ChatGPT 5