Codeforces Round #166 (Div. 2) BPrime Matrix
Posted SSL_ZZL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #166 (Div. 2) BPrime Matrix相关的知识,希望对你有一定的参考价值。
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);
以上是关于Codeforces Round #166 (Div. 2) BPrime Matrix的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #166 (Div. 2) BPrime Matrix
Codeforces Round #166 (Div. 2) DhashGood Substrings
Codeforces Round #166 (Div. 2)暴力A. Beautiful Year
Codeforces Round #166 (Div. 2)暴力A. Beautiful Year