Description

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.

Sample Input

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

Hint

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,问最少需要多少次这样的操作才能把这个矩阵变成一个素数矩阵。

解题思路:

先通过素数打表,对矩阵中的每一个数求其到大于这个数的最小素数的差值,保存到一个数组中,这个差值就是需要的操作,暴力每一行每一列得到最小操作次数。

上代码:

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 1000010
long long s[MAX],isprime[MAX];
int prime()
{
int i,j,k=;
memset(isprime,,sizeof(isprime));
isprime[]=;
isprime[]=;
for(i=;i<=MAX;i++)
{
if(isprime[i])
{
s[k++]=i;
}
for(j=i*;j<=MAX;j+=i)
{
isprime[j]=;
}
}
return k;
}
int main()
{
int i,j,x,n,m,num,sum,min1,min2,mins;
int a[][];
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
scanf("%d",&num);
for(x=;x<prime();x++)
{
if(s[x]>=num)
{
break;
}
}
a[i][j]=s[x]-num;
}
}
min1=;
min2=;
for(i=;i<n;i++)///行最小
{
sum=;
for(j=;j<m;j++)
{
sum=sum+a[i][j];
}
min1=min(min1,sum);
}
for(j=;j<m;j++)
{
sum=;
for(i=;i<n;i++)
{
sum=sum+a[i][j];
}
min2=min(min2,sum);
}
mins=min(min1,min2);
printf("%d\n",mins);
return ;
}

Prime Matrix(暴力出奇迹)的更多相关文章

  1. SID1190471 / 烦人的幻灯片 暴力出奇迹 !!!!!!!!!!!!!!!!!!

    PID221 / 烦人的幻灯片 ☆ 提交你的代码 查看讨论和题解 你还木有做过哦 我的状态         查看最后一次评测记录 质量还不能统计出来哦~ 题目评价 质量 无 ★★★★★ ★★★★☆ ★ ...

  2. 紫书 习题 8-2 UVa 1610 (暴力出奇迹)

    这道题我真的想的非常的复杂, 拿草稿纸一直在找规律,推公式, 然后总有一些特殊的情况. 然后就WA了N次.无奈之下看了别人的博客, 然后就惊了.直接暴力枚举两个相邻字符串 里面的所有可能就可以了--真 ...

  3. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  4. Codeforces - 570D 离散DFS序 特殊的子树统计 (暴力出奇迹)

    题意:给定一棵树,树上每个节点有对应的字符,多次询问在\(u\)子树的深度为\(d\)的所有节点上的字符任意组合能否凑成一个回文串 把dfs序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度 ...

  5. 51nod——1285 山峰和分段(暴力出奇迹)

    要求每段的点数都一样,因此分的段数cnt肯定是n的因子,要求每段都有山峰,因此cnt肯定小于等于山峰数量.分段的宽度d=n/cnt,对山峰数量做一个前缀和,检查一下每一段的山峰数量是否没有增加即可. ...

  6. 【杂】暴力出奇迹,lz水数据

    做了个填涂颜色的题qwq 洛谷上的qwq,然后我就把这道题水过去了qwq(显然这是不对的,我们不能水数据qwq)当然我本身是80分的qwq end-

  7. Post Lamps CodeForces - 990E(暴力出奇迹?)

    题意: 在一个从0开始的连续区间上  放置几个小区间,使得这些小区间覆盖整个大区间,不同长度的小区间有不同的花费,其中有m个点,小区间的左端点不能放在这些点上 解析: 显然如果0是这m点中的一个 则无 ...

  8. HDU4587--TWO NODES(无向图割点,暴力出奇迹)这是我见过的时间最长的题。。。

    TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  9. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

随机推荐

  1. TinyMCE插件:Filemanager [4.x-6.x] 文件名统一格式化

    上传图片程序(filemanager/upload.php) 在if (!empty($_FILES) && $upload_files)中上传图片时,在文件正式上传至服务器前,有一次 ...

  2. HTML5之特效

    2D转换 在二维的平面上做一些变化,使用transform属性 1. 2D转换之移动(translate) 案例: div{ width: 200px; height: 200px; backgrou ...

  3. PHP7.1中使用openssl替换mcrypt

    PHP7.1中使用openssl替换mcrypt 在php开发中,使用mcrypt相关函数可以很方便地进行AES加.解密操作,但是PHP7.1中废弃了mcrypt扩展,所以必需寻找另一种实现.在迁移手 ...

  4. hadoop生态搭建(3节点)-11.storm配置

    # http://archive.apache.org/dist/storm/apache-storm-1.1.0/ # ======================================= ...

  5. day2-exercise

    # Author: 刘佳赐-Isabelle October 22,2018 """ 1.有变量name = "aleX leNb" 完成如下操作: ...

  6. GoLand软件免激活的使用方法

    由于官方的Goland软件,免费使用期限是30天.如果你不购买产品的话,就需要不断的卸载和重装软件才能使用.不过要是您的资金允许的话,可以去http://www.jetbrains.com/go/bu ...

  7. SAP Odata実行命令(2)

    前言 $ skiptokenは.アプリケーションに送信されるエントリ数を制限するために使用されます. 膨大な数のエントリが要求された場合.これはパフォーマンスの向上にも役立ちます.次のリンクがアプリケ ...

  8. [WebService] 使用httpWebrequest 调用并调试WebService

    使用httpWebrequest 调用并调试WebService. 首先  使用httpWebrequest 调用WebService 代码: using System.Net;            ...

  9. 滑雪_KEY

    滑雪 ( skiing.pas/c/cpp) [题目描述] MM 参加一个滑雪比赛,滑雪场是一个 N×M 的矩形, MM 要从起点( 1, 1)滑到( N,M).矩形中每个单位格子有一个海拔高度值 h ...

  10. 北京Uber优步司机奖励政策(2月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...