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)A=(A_1,A_2,\dots,A_N) and B=(B1,B2,…,BN−1)B=(B_1,B_2,\dots,B_{N-1}) consisting of integers between 00 and M−1M-1, inclusive. The lengths of AA and BB are NN and N−1N-1, respectively.
You can perform the following operation on AA any number of times.

  • Choose an integer ii with 1≤i≤N1 \leq i \leq N, and add 11 to AiA_i.

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−1i=1,2,\dots,N-1, the remainder when Ai+Ai+1A_i+A_{i+1} is divided by MM equals BiB_i.

问题 E 的题面与问题 C 相同,仅红色标出的约束条件不同。

给定两个整数序列 A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N) 和 B=(B1,B2,…,BN−1)B=(B_1,B_2,\dots,B_{N-1}),其中每个元素均为 00 到 M−1M-1(含端点)之间的整数。序列 AA 和 BB 的长度分别为 NN 和 N−1N-1。
你可以对 AA 执行以下操作任意多次:

  • 选择一个整数 ii,满足 1≤i≤N1 \leq i \leq N,并将 AiA_i 增加 11。

求满足以下条件所需的最少操作次数。
在本题约束下,可以证明该条件总能被满足。

  • 对于 i=1,2,…,N−1i=1,2,\dots,N-1,(Ai+Ai+1) mod M=Bi(A_i+A_{i+1}) \bmod M = B_i。

输入格式

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

NN MM
A1A_1 A2A_2 …\ldots ANA_N
B1B_1 B2B_2 …\ldots BN−1B_{N-1}

输入从标准输入中按以下格式给出:

NN MM
A1A_1 A2A_2 …\ldots ANA_N
B1B_1 B2B_2 …\ldots BN−1B_{N-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=2i=2 for the first operation, we get A=(4,7,7)A=(4,7,7).
If we choose i=1i=1 for the second operation, we get A=(5,7,7)A=(5,7,7).
If we choose i=1i=1 for the third operation, we get A=(6,7,7)A=(6,7,7).
If we choose i=2i=2 for the fourth operation, we get A=(6,8,7)A=(6,8,7).
If we choose i=1i=1 for the fifth operation, we get A=(7,8,7)A=(7,8,7).
Since A1+A2=7+8=15A_1+A_2=7+8=15 and A2+A3=8+7=15A_2+A_3=8+7=15, the condition is satisfied.
The condition cannot be satisfied with four or fewer operations, so the answer is 55.

Constraints

  • 2≤N≤2×1052 \leq N \leq 2 \times 10^5
  • 3≤M≤1093 \leq M \leq 10^9
  • 0≤Ai≤M−10 \leq A_i \leq M-1
  • 0≤Bi≤M−10 \leq B_i \leq M-1
  • All input values are integers.

样例 1 解释:
若第一次操作选择 i=2i=2,则得到 A=(4,7,7)A=(4,7,7)。
若第二次操作选择 i=1i=1,则得到 A=(5,7,7)A=(5,7,7)。
若第三次操作选择 i=1i=1,则得到 A=(6,7,7)A=(6,7,7)。
若第四次操作选择 i=2i=2,则得到 A=(6,8,7)A=(6,8,7)。
若第五次操作选择 i=1i=1,则得到 A=(7,8,7)A=(7,8,7)。
由于 A1+A2=7+8=15A_1+A_2=7+8=15 且 A2+A3=8+7=15A_2+A_3=8+7=15,条件得以满足。
无法在四次或更少的操作次数内满足该条件,因此答案为 55。

约束条件

  • 2≤N≤2×1052 \leq N \leq 2 \times 10^5
  • 3≤M≤1093 \leq M \leq 10^9
  • 0≤Ai≤M−10 \leq A_i \leq M-1
  • 0≤Bi≤M−10 \leq B_i \leq M-1
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页