题干:

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.

题目大意:

给出定义:素数矩阵是指一个矩阵中存在至少一行和一列全是素数的矩阵。现在给你一个矩阵,你可以选择矩阵中任意一个元素加1,问最少需要多少次这样的操作才能把这个矩阵变成一个素数矩阵。

解题报告:

这题一眼就是一个n^2logn的写法,,,但是写这篇题解的时候发现其实是可以n^2的,,但是因为给了个2s所以时间还算宽裕就直接n^2logn了。。

言归正传,其实就是个素数打表然后二分预处理出每个数对应的可以变成的素数的值,同时维护一个求个差的最小值就好了。。说到n^2其实也不难想,,就是把二分的过程给优化掉,,因为预处理的时候就是单调的所以不需要二分查找了做了一些无用的操作。。直接数组递推过去就可以了、。(这一招很常用啊,虽然在这一题中不明显但是有的时候就需要正着递推一遍反着递推一遍,线性就可以得到我们想要的东西,,比如还是那个经典题Fountain)

另外啊这题因为数据量1e5所以打表就用了简单的nlogn,,懒得写线性筛了、、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e6 + 5;
int su[MAX],cnt;
bool isprime[MAX];
int a[550][550];
int qq[550][550];
void prime() {memset(isprime,1,sizeof isprime);isprime[1]=isprime[0]=0;for(int i = 2; i<=MAX; i++) {if(isprime[i]) {su[++cnt] = i;for(int j = 2*i; j<=MAX; j+=i) isprime[j] = 0;}}
}
int main()
{prime();int n,m;cin>>n>>m;for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {scanf("%d",&a[i][j]);}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {int pos = lower_bound(su+1,su+cnt+1,a[i][j]) - su;qq[i][j] = su[pos];}}ll minn = 0x3f3f3f3f3f;for(int i = 1; i<=n; i++) {ll tmp = 0;for(int j = 1; j<=m; j++) tmp += qq[i][j] - a[i][j];minn = min(minn,tmp);}for(int j = 1; j<=m; j++) {ll tmp = 0;for(int i = 1; i<=n; i++) tmp += qq[i][j] - a[i][j];minn = min(minn,tmp);}printf("%lld\n",minn);return 0 ;}

【CodeForces - 271B 】Prime Matrix (素数,预处理打表,思维)相关推荐

  1. P1217 [USACO1.5]回文质数 Prime Palindromes(素数筛法/打表)

    P1217 [USACO1.5]回文质数 Prime Palindromes(素数筛法/打表) 一:埃氏筛(时间复杂度--nloglogn) 重点:一个数x是合数,则它的倍数也是合数 //用埃氏筛生成 ...

  2. HDU 4548 美素数(打表)

    HDU  4548  美素数(打表)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/H 题目 ...

  3. 数据结构-线性表-思维导图+小结

    数据结构-线性表思维导图+小结 1 数据结构-第二章-线性表-思维导图 2 数据结构-第二章-线性表-习题小结 2.1 概念性习题小结 2.2 操作性习题小结 1 数据结构-第二章-线性表-思维导图 ...

  4. 【CodeForces - 1047C】Enlarge GCD(数学,枚举,预处理打表,思维)

    题干: F先生有n个正整数,a1,a2,...,an 他认为这些整数的最大公约数太小了,所以他想通过删除一些整数来扩大它 您的任务是计算需要删除的最小整数数,以便剩余整数的最大公约数大于所有整数的公约 ...

  5. POJ - 2689 Prime Distance(素数区间筛模板)

    题目链接:点击查看 题目大意:给出一段闭区间[l,r],求区间内相邻距离最大的素数对和相邻距离最小的素数对,题目保证r-l<=1e6,1<=l<=r<= 题目分析:因为我们要求 ...

  6. Gym 102055L Ultra Weak Goldbach's Conjecture (素数密度+打表/哥德巴赫猜想)

    题意 给定一个 n (1<=n<=1e12),试将其分解成 6 个质数之和的形式,如果可以分解,输出任意一种,否则输出IMPOSSIBLE. 思路 模拟赛的时候想的做法好像有点- 考虑到素 ...

  7. HDU 5750 快速筛素数法打表

    题目大意:x能整除n,x就是n的positive proper divisor,但n本身不算,给你两个数n和d,找出所有小于n的数中,最大positive proper divisor是d个数,T组测 ...

  8. hdoj5317【素数预处理】

    //这个很好了...虽然是一般.. int isp[1000100]; int p[1000100]; void init() {int sum=0;int i,j;fill(isp,isp+1000 ...

  9. c语言判断素数squ,poj1811——Prime Test//素数判断+整数分解因子

    题意:给定N,如果N为素数,输出"Prime",否则输出其最小因子. 思路:用miller_rabin判断素数,pollardRho用于整数因子的分解.整数因子分解还有一个更快的算 ...

最新文章

  1. C#和F#默认接口方法更新
  2. 第1关:学习-用循环和数组实现输入某年某月某日,判断这一天一年的第几天
  3. Android开发之dp转像素,像素转换为dp工具类,详细代码,带有源文件下载地址。...
  4. Python-OpenCV 处理视频(三)(四)(五): 标记运动轨迹 运动检测 运动方向判断
  5. spring cloud云服务架构 - particle云架构代码结构讲解
  6. Emscripten-mac安装与升级
  7. (线性基) bzoj 2460
  8. mysql新增后默认返回值_mybatis insert、update 、delete默认返回值解释与如何设置返回表主键...
  9. ORACLE数据库DDL审计触发器与隐藏参数_system_trig_enabled
  10. 粘性定位(HTML、CSS)
  11. Linux 基本命令(七)--cat,less,more,head,tail,nl 常用命令
  12. JavaFX游戏制作:瓦片地图绘制
  13. 传感器研究NO1.陀螺仪
  14. kettle 邮件服务器,kettle 实用功能之三 ---- 使用 kettle 群发动态内容的邮件。
  15. 独家!天才少年 Vitalik:“中国开发者应多关注以太坊!”
  16. Hive  if null 的用法
  17. Java面试总结(2021优化版)发布1024程序员节
  18. Redis 7.0 正式发布,新增近 50 个新命令,这次真的学不动了。。
  19. 腾讯手游助手修改共享目录/缓存目录/Temp文件夹路径
  20. 如何做一个简单的开放接口(1)-功能设计

热门文章

  1. [Leetcode][第392题][JAVA][判断子序列][动态规划][双指针]
  2. ios php rsa,RSA 加密 iOS
  3. java多线程创建runnable_Java线程池和runnables创建runnables
  4. javame学习_从零基础自学Java教程:648集全网最新Java学习教程,一学就会
  5. node 更新_被创造者嫌弃,Node.js 如何应对来自 Deno 的挑战
  6. c语言编程流水灯与交通灯实验,C51单片机实验报告_流水灯_交通灯_定时器_双机交互_时钟.doc...
  7. 家用电脑配置_游戏搬砖必看教程,游戏工作室电脑如何配置
  8. 百度推送java_关于百度推送,请教一下大家
  9. ajax status php,解决laravel 出现ajax请求419(unknown status)的问题
  10. x210烧写流程(inand)