相邻和(困难版)AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)The problem statement of Problem E is the same as Problem C. Only the constraint shown in red differ.
题目 E 的题面与题目 C 相同。只有以红色显示的约束条件不同。AI (CI翻译)
You are given integer sequences $A=(A_1,A_2,\dots,A_N)$ and $B=(B_1,B_2,\dots,B_{N-1})$ consisting of integers between $0$ and $M-1$, inclusive. The lengths of $A$ and $B$ are $N$ and $N-1$, respectively.
You can perform the following operation on $A$ any number of times.
给定由 $0$ 到 $M-1$(含)之间的整数构成的整数序列 $A=(A_1,A_2,\dots,A_N)$ 和 $B=(B_1,B_2,\dots,B_{N-1})$。序列 $A$ 和 $B$ 的长度分别为 $N$ 和 $N-1$。
你可以对 $A$ 执行以下操作任意次。AI (CI翻译)
- Choose an integer
$i$with$1 \leq i \leq N$, and add$1$to$A_i$.
- 选择一个满足
$1 \leq i \leq N$的整数$i$,使$A_i$加$1$。
Find the minimum number of operations required to satisfy the following condition. It can be proved that the condition can always be satisfied under the constraints of this problem.
求满足以下条件所需的最少操作次数。 在本题约束条件下,可以证明该条件总是能够满足。AI (CI翻译)
- For
$i=1,2,\dots,N-1$, the remainder when$A_i+A_{i+1}$is divided by$M$equals$B_i$.
- 对于
$i=1,2,\dots,N-1$,$A_i+A_{i+1}$除以$M$所得的余数等于$B_i$。
Constraints
约束条件
AI (CI翻译)$2 \leq N \leq 2 \times 10^5$$3 \leq M \leq 10^9$$0 \leq A_i \leq M-1$$0 \leq B_i \leq M-1$- All input values are integers.
$2 \leq N \leq 2 \times 10^5$$3 \leq M \leq 10^9$$0 \leq A_i \leq M-1$$0 \leq B_i \leq M-1$- 所有输入值均为整数。
Input
输入格式
AI (CI翻译)The input is given from Standard Input in the following format:
输入从标准输入中给出,格式如下:AI (CI翻译)
N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_{N-1}
N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_{N-1}AI (CI翻译)
Output
输出格式
AI (CI翻译)Output the answer on one line.
在一行中输出答案。AI (CI翻译)
样例
3 10
4 6 7
5 5
5
2 3
1 2
2
2
10 10
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
40
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。