AT_abc467_e.Adjacent Sums (hard)
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The problem statement of Problem E is the same as Problem C. Only the constraint shown in red differ.
You are given integer sequences A=(A1,A2,…,AN) and B=(B1,B2,…,BN−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.
- Choose an integer i with 1≤i≤N, and add 1 to Ai.
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.
- For i=1,2,…,N−1, the remainder when Ai+Ai+1 is divided by M equals Bi.
问题 E 的题面与问题 C 相同,仅红色标出的约束条件不同。
给定两个整数序列 A=(A1,A2,…,AN) 和 B=(B1,B2,…,BN−1),其中每个元素均为 0 到 M−1(含端点)之间的整数。序列 A 和 B 的长度分别为 N 和 N−1。
你可以对 A 执行以下操作任意多次:
- 选择一个整数 i,满足 1≤i≤N,并将 Ai 增加 1。
求满足以下条件所需的最少操作次数。
在本题约束下,可以证明该条件总能被满足。
- 对于 i=1,2,…,N−1,(Ai+Ai+1)modM=Bi。
输入格式
The input is given from Standard Input in the following format:
N M
A1 A2 … AN
B1 B2 … BN−1
输入从标准输入中按以下格式给出:
N M
A1 A2 … AN
B1 B2 … BN−1
输出格式
Output the answer on one line.
在一行中输出答案。
输入输出样例
输入#1
3 10 4 6 7 5 5
输出#1
5
输入#2
2 3 1 2 2
输出#2
2
输入#3
10 10 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1
输出#3
40
说明/提示
Sample 1 Explanation:
If we choose i=2 for the first operation, we get A=(4,7,7).
If we choose i=1 for the second operation, we get A=(5,7,7).
If we choose i=1 for the third operation, we get A=(6,7,7).
If we choose i=2 for the fourth operation, we get A=(6,8,7).
If we choose i=1 for the fifth operation, we get A=(7,8,7).
Since A1+A2=7+8=15 and A2+A3=8+7=15, the condition is satisfied.
The condition cannot be satisfied with four or fewer operations, so the answer is 5.
Constraints
- 2≤N≤2×105
- 3≤M≤109
- 0≤Ai≤M−1
- 0≤Bi≤M−1
- All input values are integers.
样例 1 解释:
若第一次操作选择 i=2,则得到 A=(4,7,7)。
若第二次操作选择 i=1,则得到 A=(5,7,7)。
若第三次操作选择 i=1,则得到 A=(6,7,7)。
若第四次操作选择 i=2,则得到 A=(6,8,7)。
若第五次操作选择 i=1,则得到 A=(7,8,7)。
由于 A1+A2=7+8=15 且 A2+A3=8+7=15,条件得以满足。
无法在四次或更少的操作次数内满足该条件,因此答案为 5。
约束条件
- 2≤N≤2×105
- 3≤M≤109
- 0≤Ai≤M−1
- 0≤Bi≤M−1
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?