CF1740I.Arranging Crystal Balls

普及/提高-

通过率:0%

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 n1n-1 with statue ii being to the left of statue i+1i+1 and statue n1n-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 m1m-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)modn,(p+2)modn,(p+3)modn,,(p+k1)modnp, (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)modm(a_i+1) \bmod m .
  • For all chosen statues, change their values of aia_i into (ai1)modm(a_i-1) \bmod m .

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

输入格式

The first line contains three integers nn , mm , and kk ( 2n,m1062 \leq n,m \leq 10^6 , nm2106nm \leq 2 \cdot 10^6 , 1k<n1 \leq k < 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,,an1a_0,a_1,\ldots,a_{n-1} ( 0ai<m0 \leq a_i < m ) — the integers in the crystal balls.

输出格式

If it is possible to perform zero or more operations so that a0=a1==an1=0a_0=a_1=\ldots=a_{n-1}=0 , output the minimum number of operations required. Otherwise, output 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:=(ai1)modma_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:=(ai1)modma_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)modma_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:=(ai1)modma_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] .
首页