CF1740I.Arranging Crystal Balls

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

In the world of Compfestnesia, Pak Chanek discovers a secret underground dungeon. Inside it, there is a treasure chest that is surrounded by nn statues that are arranged in a circular manner. The statues are numbered from 00 to n−1n-1 with statue ii being to the left of statue i+1i+1 and statue n−1n-1 being to the left of statue 00.

Pak Chanek observes that each statue is holding a crystal ball with an integer between 00 and m−1m-1 inclusive. Let's say the integer in the crystal ball of statue ii is aia_i.

The dungeon provides instructions that every integer in the crystal balls must be 00 in order to open the treasure chest. To achieve that, Pak Chanek is given an integer kk, and he can do zero or more operations. In a single operation, Pak Chanek does the following:

  1. Choose exactly kk consecutive statues. In other words, choose the statues p,(p+1) mod n,(p+2) mod n,(p+3) mod n,…,(p+k−1) mod np, (p+1) \bmod n, (p+2) \bmod n, (p+3) \bmod n, \ldots, (p+k-1) \bmod n for some chosen index pp.
  2. Do one of the following:
    • For all chosen statues, change their values of aia_i into (ai+1) mod m(a_i+1) \bmod m.
    • For all chosen statues, change their values of aia_i into (ai−1) mod m(a_i-1) \bmod m.

Help Pak Chanek find the minimum possible number of operations to open the treasure chest.

在 Compfestnesia 世界中,Pak Chanek 发现了一座秘密的地下迷宫。迷宫内部有一只宝箱,被 nn 座呈环形排列的雕像所环绕。这些雕像编号为 00 到 n−1n-1,其中雕像 ii 位于雕像 i+1i+1 的左侧,而雕像 n−1n-1 位于雕像 00 的左侧。

Pak Chanek 观察到,每座雕像都手持一个水晶球,水晶球中包含一个介于 00 到 m−1m-1(含端点)之间的整数。设雕像 ii 手持水晶球中的整数为 aia_i。

迷宫提供了指示:只有当所有水晶球中的整数均为 00 时,宝箱才能开启。为达成这一目标,Pak Chanek 获得了一个整数 kk,并可执行零次或多次操作。每次操作中,Pak Chanek 执行以下步骤:

  1. 恰好选择 kk 座连续的雕像。换言之,对某个选定的下标 pp,选择雕像 p, (p+1) mod n, (p+2) mod n, (p+3) mod n, …, (p+k−1) mod np,\ (p+1) \bmod n,\ (p+2) \bmod n,\ (p+3) \bmod n,\ \ldots,\ (p+k-1) \bmod n。
  2. 执行以下两种操作之一:
    • 对所有被选中的雕像,将其 aia_i 的值更新为 (ai+1) mod m(a_i+1) \bmod m;
    • 对所有被选中的雕像,将其 aia_i 的值更新为 (ai−1) mod m(a_i-1) \bmod m。

请帮助 Pak Chanek 找出开启宝箱所需的最少操作次数。

输入格式

The first line contains three integers nn, mm, and kk (2≤n,m≤1062 \leq n,m \leq 10^6, nm≤2⋅106nm \leq 2 \cdot 10^6, 1≤k<n1 \leq k \lt n) — the number of statues, the bound of the integers in the crystal balls, and the number of statues that can be operated in a single operation.

The second line contains nn integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} (0≤ai<m0 \leq a_i \lt m) — the integers in the crystal balls.

第一行包含三个整数 nn、mm 和 kk(2≤n,m≤1062 \leq n,m \leq 10^6,nm≤2⋅106nm \leq 2 \cdot 10^6,1≤k<n1 \leq k \lt n)—— 分别表示雕像的数量、水晶球中整数的上界,以及单次操作可处理的雕像数量。

第二行包含 nn 个整数 a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}(0≤ai<m0 \leq a_i \lt m)—— 表示各水晶球中的整数。

输出格式

If it is possible to perform zero or more operations so that a0=a1=…=an−1=0a_0=a_1=\ldots=a_{n-1}=0, output the minimum number of operations required. Otherwise, output −1-1.

如果可以通过执行零次或多次操作使得 a0=a1=…=an−1=0a_0=a_1=\ldots=a_{n-1}=0,则输出所需的最少操作次数;否则,输出 −1-1。

输入输出样例

  • 输入#1

    5 9 3
    8 1 4 5 0

    输出#1

    7
  • 输入#2

    4 4 2
    1 0 0 0

    输出#2

    -1
  • 输入#3

    5 5 2
    1 0 0 0 0

    输出#3

    10

说明/提示

In the first example, Pak Chanek can do the following operations:

  1. Do the ai:=(ai−1) mod ma_i := (a_i-1) \bmod m operation 33 times for statues 11, 22, and 33. Now a=[8,7,1,2,0]a=[8,7,1,2,0].
  2. Do the ai:=(ai−1) mod ma_i := (a_i-1) \bmod m operation 11 time for statues 33, 44, and 00. Now a=[7,7,1,1,8]a=[7,7,1,1,8].
  3. Do the ai:=(ai+1) mod ma_i := (a_i+1) \bmod m operation 22 times for statues 44, 00, and 11. Now a=[0,0,1,1,1]a=[0,0,1,1,1].
  4. Do the ai:=(ai−1) mod ma_i := (a_i-1) \bmod m operation 11 time for statues 22, 33, and 44. Now a=[0,0,0,0,0]a=[0,0,0,0,0].

在第一个例子中,Pak Chanek 可执行以下操作:

  1. 对雕像 11、22 和 33 各执行 33 次 ai:=(ai−1) mod ma_i := (a_i-1) \bmod m 操作。此时 a=[8,7,1,2,0]a=[8,7,1,2,0]。
  2. 对雕像 33、44 和 00 各执行 11 次 ai:=(ai−1) mod ma_i := (a_i-1) \bmod m 操作。此时 a=[7,7,1,1,8]a=[7,7,1,1,8]。
  3. 对雕像 44、00 和 11 各执行 22 次 ai:=(ai+1) mod ma_i := (a_i+1) \bmod m 操作。此时 a=[0,0,1,1,1]a=[0,0,1,1,1]。
  4. 对雕像 22、33 和 44 各执行 11 次 ai:=(ai−1) mod ma_i := (a_i-1) \bmod m 操作。此时 a=[0,0,0,0,0]a=[0,0,0,0,0]。

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

首页