CF271B.Prime Matrix

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You've got an n × m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.

You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.

A matrix is prime if at least one of the two following conditions fulfills:

  • the matrix has a row with prime numbers only;
  • the matrix has a column with prime numbers only;

Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.

你有一个 n×mn \times m 的矩阵,矩阵中元素均为整数。每次操作,你可以对矩阵中任意一个元素执行一次变换:将其值加 11。每个元素可以被增加任意多次。

你对质数非常感兴趣。我们来回顾一下:质数是指恰好有两个不同的正整数约数(即它本身和 11)的正整数。例如,22、33、55 是质数,而 11、44、66 不是质数。

若一个矩阵满足以下两个条件之一,则称其为质数矩阵:

  • 矩阵中存在某一行,其所有元素均为质数;
  • 矩阵中存在某一列,其所有元素均为质数;

你的任务是:计算将给定矩阵变为质数矩阵所需的最少操作次数。

输入格式

The first line contains two integers n, m (1 ≤ n, m ≤ 500) — the number of rows and columns in the matrix, correspondingly.

Each of the following n lines contains m integers — the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.

The numbers in the lines are separated by single spaces.

第一行包含两个整数 nn 和 mm(1≤n,m≤5001 \leq n, m \leq 500),分别表示矩阵的行数和列数。

接下来的 nn 行中,每行包含 mm 个整数,表示初始矩阵。矩阵中所有元素均为正整数,且初始矩阵中的所有数值均不超过 10510^5。

每行中的数字以单个空格分隔。

输出格式

Print a single integer — the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.

输出一个整数——从当前矩阵得到素数矩阵所需的最少移动次数。如果当前矩阵已是素数矩阵,则输出 0。

输入输出样例

  • 输入#1

    3 3
    1 2 3
    5 6 1
    4 4 1

    输出#1

    1
  • 输入#2

    2 3
    4 8 8
    9 2 9

    输出#2

    3
  • 输入#3

    2 2
    1 3
    4 2

    输出#3

    0

说明/提示

In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.

In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.

In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.

在第一个样例中,你需要将单元格 (1, 1) 中的数字 1 增加 1。这样,第一行将由素数组成:2, 2, 3。

在第二个样例中,你需要将单元格 (1, 2) 中的数字 8 增加三次。这样,第二列将由素数组成:11, 2。

在第三个样例中,你无需进行任何操作,因为第二列已经由素数组成:3, 2。

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

首页