CF289B.Polo the Penguin and Matrix

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Little penguin Polo has an n×mn×m matrix, consisting of integers. Let's index the matrix rows from 1 to nn from top to bottom and let's index the columns from 1 to mm from left to right. Let's represent the matrix element on the intersection of row ii and column jj as aija_{ij} .

In one move the penguin can add or subtract number dd from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.

输入格式

The first line contains three integers nn , mm and dd (1<=n,m<=100,1<=d<=104)(1<=n,m<=100,1<=d<=10^{4}) — the matrix sizes and the dd parameter. Next nn lines contain the matrix: the jj -th integer in the ii -th row is the matrix element aija_{ij} (1<=aij<=104)(1<=a_{ij}<=10^{4}) .

输出格式

In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes).

输入输出样例

  • 输入#1

    2 2 2
    2 4
    6 8
    

    输出#1

    4
    
  • 输入#2

    1 2 7
    6 7
    

    输出#2

    -1
    
首页