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. .Net core 使用SSH.Net上传到SFTP服务器和和下载文件

    今天换了个服务器,文件上传到sftp服务器上了,那么ftp和sftp服务器有什么区别呢,正常来说sftp会更安全一些. 废话不多说,首先.net core 上传到sftp需要引入一个Nuget包,就是 ...

  2. Spring Boot在反序列化过程中:jackson.databind.exc.InvalidDefinitionException cannot deserialize from Object value

    错误场景 用Spring boot写了一个简单的RESTful API,在测试POST请求的时候,request body是一个符合对应实体类要求的json串,post的时候报错. 先贴一段error ...

  3. 【小尝试】Java获取慕课网原有路径课程列表

    作为一个老慕课网(https://www.imooc.com/)粉丝,还记得最开始的慕课网有很多免费的路径课程,练习什么的也特别详细,是入门一门语言的好方法. 现在慕课网发展起来了收费模式,添加了很多 ...

  4. 「PHP」工厂方法模式

    引言   所属:创建型模式,常用设计模式之一 工厂模式分为:简单工厂模式.工厂方法模式.静态工厂模式.抽象工厂模式. 下面为工厂方法模式. 参考资料: <大话设计模式>程杰   模式概述 ...

  5. python学习——模块和包

    在之前常用模块中我们已经初步了解了模块的导入,今天来说学习一下模块和包.我们可以把模块理解成每一个python文件.而包就是多个能解决一类问题的python文件全部放在一起.OK

  6. python--模块之os操作文件模块

    作用:OS又名为:操作系统.所以就是操作系统相关的功能.可以处理文件和目录这些我们日常手动需要做的操作,比如:显示当前目录下所有文件.删除某个文件.获取文件大小...os模块是与操作系统交互的一个接口 ...

  7. Caliburn.Micro 杰的入门教程5,Window Manager 窗口管理器

    Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...

  8. BZOJ1029_建筑抢修_KEY

    题目传送门 这是一道贪心的问题. 总体做法是这样的:先按照报废的快慢从小到大SORT一遍,优先修报废快的.同时开一个大根堆(C++的朋友可以用priority_queue),用来记录已经修了的建筑的耗 ...

  9. 武汉ber优步司机奖励政策(1月4日~1月10日)

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

  10. iOS 库 开发小结

    1.基本用法 定义类,导出头文件,注意头文件,库文件的search path 2.加载资源 - 使用主工程的文件,耦合性太强 - 封装到NSBundle中 NSBundle可以封装xib storyb ...