【Codeforces Round #166 (Div. 2) B】Prime Matrix

Codeforces Round #166 (Div. 2) B. Prime Matrix
Codeforces Round #166 (Div. 2) A. Beautiful Year 题解
Codeforces Round #166 (Div. 2) D. Good Substrings 题解


题目

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.

Input
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.

Output
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.

Examples
input

3 3
1 2 3
5 6 1
4 4 1

output

1

input

2 3
4 8 8
9 2 9

output

3

input

2 2
1 3
4 2

output

0

Note
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.


题目大意

现在给你一个n*m的矩阵,矩阵上每一格都有一个数
你可以施无数次魔法(bushi,让一个格子上的数 +1
目标是得到一列或者一行的素数,请问最少操作多少次达到目的


解题思路

和A题有异曲同工之妙

预处理出素数,然后标记每个数,标记出大于当前数的最小质数s[]
把矩阵中每个数都转换成,变成素数需要操作多少次
第 i 行第 j 行的次数即 s[a[i][j]] - a[i][j]

题目变成,给出一个矩阵,找到矩阵中的一行或一列的和最少
这个直接暴力就好


Code

#include <iostream>
#include <cstdio>
#define N 200000
#define M 600

using namespace std;

int n, m, num, ans = 1e10, maxn;
int ljh[M], ljl[M], v[N + 200], s[N + 200], zs[N], a[M][M];

void work() {
	v[1] = 1;
	for(int i = 2; i <= N; i ++) {
		if(v[i]) continue;
		for(int j = 1; i * j <= N; j ++)
			v[i * j] = 1;
		zs[++ num] = i;  //记录下质数
	}
	for(int i = maxn; i >= 1; i --) {
	  //把每个数都标记,比一个一个找矩阵内的标记数要方便很多,也更快(吧
		while(zs[num - 1] >= i)
			num --;
		if(zs[num] >= i) s[i] = zs[num];  //标记大于当前数的最小质数
	}
}

int main() {
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= m; j ++) {
			scanf("%d", &a[i][j]);
			maxn = max(maxn, a[i][j]);
		}
	work();
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= m; j ++) {
			a[i][j] = s[a[i][j]] - a[i][j];  //标记每个数
			ljh[i] += a[i][j], ljl[j] += a[i][j];  //一个累(l)计(j)行(h),一个累(l)计(j)列(l)
		}
	for(int i = 1; i <= n; i ++) ans = min(ans, ljh[i]);
	for(int i = 1; i <= m; i ++) ans = min(ans, ljl[i]);
	printf("%d", ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值