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

题干:

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 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441176.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 270C】Magical Boxes (思维,进制,有坑)

题干&#xff1a; Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes. From the top view each magical box looks like a square with side leng…

虚拟机安装黑群晖_【群晖系统】HEI群辉DSM 6.2.1 系统安装图文教程 (19年2月)

黑群晖系统其实是指在普通电脑运行Synology DSM系统, 事实上在普通PC电脑上安装黑群晖(Synology DSM)也非常方便, 现在把教程简单写一下。引导系统装哪里&#xff1f;非常关键的问题&#xff0c;DSM采用系统和数据相分离的结构&#xff0c;也就是说引导系统需要独立安装在一个设…

【Effect CodeForces - 270D】Greenhouse (思维,最长非递减子序列(上升),对偶问题,考虑反面)

题干&#xff1a; Emuskald is an avid horticulturist and owns the worlds longest greenhouse — it is effectively infinite in length. Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His …

错误1053服务没有及时_无法启动xx服务 错误1053:服务没有及时响应启动或控制请求,排查方法。...

sc安装服务&#xff0c;启动失败&#xff1a;显示错误1053&#xff1a;服务没有及时响应启动或控制请求网上找了很多方法资料&#xff0c;什么注册表啊&#xff0c;权限啊之类的。你按照这些都做完后&#xff0c;仍然提示这个错误。告诉你&#xff0c;要检查自己的程序是不是有…

【CodeForces - 219D 】Choosing Capital for Treeland (树形dp)

题干&#xff1a; The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we dont take the direction of the roads into consideration, we can get …

数据库分页查询mysql_各种数据库的分页查询SQL语句总结

1.oracle数据库分页select * from (select a.*,rownum rc from 表名 where rownum<endrow) a where a.rc>startrow2.DB2数据库分页Select * from (select rownumber() over() as rc,a.* from (select * from 表名 order by 列名) as a) where rc between startrow and en…

【HihoCoder - 1880】地铁环线 (前缀和,水题,模拟)

题干&#xff1a; H市有一环线地铁&#xff0c;一共包含N站&#xff0c;编号1~N。正向行驶的地铁会按1 -> 2 -> 3 -> ... -> N -> 1的方向行驶&#xff0c;反向会按1 -> N -> N-1 -> ... -> 3 -> 2 -> 1的方向行驶。 给定所有相邻两站之间…

unsigned int mysql_mysql 中int类型字段unsigned和signed的探索

转自&#xff1a;http://www.0791quanquan.com/news_keji/topic_816453/探索一&#xff1a;正负数问题拿tinyint字段来举例&#xff0c;unsigned后&#xff0c;字段的取值范围是0-255&#xff0c;而signed的范围是-128 - 127。 那么如果我们在明确不需要负值存在的情况下&#…

【HihoCoder - 1881】特殊任务 (树形图,遍历)

题干&#xff1a; H公司一共有N名员工&#xff0c;编号1~N&#xff0c;其中CEO是1号员工。除了CEO之外&#xff0c;其他员工都有唯一的直接上司&#xff0c;所以N名员工上下级关系恰好形成了一棵树形结构。 我们知道每一名员工向H公司的代码库贡献了多少行代码。具体来说&a…

mysql 主从 keepalived_MySQL之双向主从加keepalived高可用

最近在做MySQL数据库的双向主从&#xff0c;了解到keepalived能够自动判断并切换到可用数据库&#xff0c;自己试了一下&#xff0c;整理出文档来。先声明一下环境iptables开启3306端口或者关掉&#xff0c;关闭selinuxMySQL-01&#xff1a;192.168.204.138MySQL-02&#xff1a…

关于vector的size()的使用问题

vector<int> vec; for (int i 0; i < vec.size() - 1; i) &#xff5b; vec[i] &#xff5d; 这里会有个隐藏问题&#xff0c;当vec的size为0时&#xff0c;因为size&#xff08;&#xff09;函数的返回值是无符号整型&#xff0c;这时vec.size() - 1是一个正数…

mysql密码命名规则_MySql命名规范

数据库环境dev&#xff1a;开发环境&#xff0c;开发可读写&#xff0c;可修改表结构。开发人员可以修改表结构&#xff0c;可以随意修改其中的数据但是需要保证不影响其他开发同事。qa&#xff1a;测试环境&#xff0c;开发可读写&#xff0c;开发人员可以通过工具修改表结构。…

【CodeVS - 3639】(树的重心模板,裸题)

题干&#xff1a; 题目描述 Description 给出一棵树&#xff0c;求出树的中心。 为了定义树的中心&#xff0c;首先给每个结点进行标号。对于一个结点K&#xff0c;如果把K从树中删除&#xff08;连同与它相连的边一起&#xff09;&#xff0c;剩下的被分成了很多块&#xf…

go-mysql查询单条数据_Golang 从 MySQL 数据库读取一条数据

刚开始在 sqlx 和 gorm 两者间纠结了半天&#xff0c;准备使用 sqlx 了&#xff0c;看了文档&#xff0c;感觉就是灾难般的文档。我按照例子硬是没有从 MySQL 读出数据。又尝试了 gorm&#xff0c;这家伙的文档更加不友好&#xff0c;概念更多。调试了半天&#xff0c;返回的结…

【HYSBZ - 1088 】扫雷Mine (简单dp)

题干&#xff1a; 相信大家都玩过扫雷的游戏。那是在一个n*m的矩阵里面有一些雷&#xff0c;要你根据一些信息找出雷来。万圣节到了 &#xff0c;“余”人国流行起了一种简单的扫雷游戏&#xff0c;这个游戏规则和扫雷一样&#xff0c;如果某个格子没有雷&#xff0c;那么它里…

docker启动mysql容器_Docker容器开机自启动

查看所有容器[vagrantlocalhost ~]$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f142f72d7e8 redis "docker-entr…

【CodeForces - 527C】Glass Carving(线段树或者SBT或者set)

题干&#xff1a; Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm    h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understand…

mysql常驻内存_MySQL的内存和相关问题排查

我们都知道数据库是IO密集型一类应用&#xff0c;为了提高其性能大量使用内存代替文件(交换分区)的IO操作是保证数据库稳定、高效的基本原则。那么数据库是如何使用内存的&#xff0c;我们如何查看数据库内存的占用&#xff0c;如何通过通过数据库内存配置设置提高其性能&#…

【HihoCoder - 1850】字母去重 (字符串,思维)

题干&#xff1a; 给定一个字符串S&#xff0c;每次操作你可以将其中任意一个字符修改成其他任意字符。 请你计算最少需要多少次操作&#xff0c;才能使得S中不存在两个相邻的相同字符。 Input 只包含小写字母的字符串S。 1 ≤ |S| ≤ 100000 Output 一个整数代表答案…

bash mysql count()_【MySQL】性能优化之 count(*) VS count(col)

优化mysql数据库时&#xff0c;经常有开发询问 count(1)和count(primary_key) VS count(*)的性能有何差异&#xff1f;看似简单的问题&#xff0c;估计会有很多人对此存在认知误区&#xff1a;1. 认为count(1) 和 count(primary_key) 比 count(*) 的性能好。2. count(column) 和…