CF289B.Polo the Penguin and Matrix

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as a__ij.

In one move the penguin can add or subtract number d 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.

小企鹅波洛有一个 n×mn \times m 的整数矩阵。我们将矩阵的行从上到下编号为 11 到 nn,列从左到右编号为 11 到 mm。用 aija_{ij} 表示第 ii 行与第 jj 列交叉处的矩阵元素。

在一次操作中,企鹅可以对某个矩阵元素加上或减去数 dd。求使所有矩阵元素相等所需的最少操作次数。如果无法实现该目标,请说明。

输入格式

The first line contains three integers n, m and d (1 ≤ n, m ≤ 100, 1 ≤ d ≤ 104) — the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element a__ij (1 ≤ a__ij ≤ 104).

第一行包含三个整数 nn、mm 和 dd(1 ≤ n, m ≤ 1001 ≤ n, m ≤ 100,1 ≤ d ≤ 1041 ≤ d ≤ 10^4)—— 分别表示矩阵的行数、列数以及参数 dd。接下来的 nn 行描述该矩阵:第 ii 行中的第 jj 个整数为矩阵元素 aija_{ij}(1 ≤ aij ≤ 1041 ≤ 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(不带引号)。

输入输出样例

  • 输入#1

    2 2 2
    2 4
    6 8

    输出#1

    4
  • 输入#2

    1 2 7
    6 7

    输出#2

    -1

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

首页