0
点赞
收藏
分享

微信扫一扫

CodeForces - 271B (素数打表)

zhoulujun 2023-02-03 阅读 70


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.

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.

给出一个矩阵,只要满足有一行或者一列全是素数就是满足的矩阵,不是只有一行,不过我们要让变换次数最小就尽量变最小的那一行或者那一列。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,m,t;
int i,j,k;
int prime[100010],a[510][510];
int hang[510],lie[510];
int cnt[100010];
int cnt;

void su()
{
memset(cnt,0,sizeof(cnt));
cnt=0;
cnt[0]=cnt[1]=1;
for(int i=2; i<100010; i++)
{
if(!cnt[i])
prime[cnt++]=i;
for(int j=0; j<cnt&&prime[j]*i<100010; j++)
{
cnt[i*prime[j]]=1;
if(i%prime[j]==0)
break;
}
}
}

int main()
{
su();
scanf("%d %d",&n,&m);
memset(a,0,sizeof(a));
memset(hang,0,sizeof(hang));
memset(lie,0,sizeof(lie));
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
scanf("%d",&a[i][j]);
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(cnt[a[i][j]])
{
t=lower_bound(prime,prime+cnt,a[i][j])-prime;///大于当前数最小素数的一个坐标
lie[j]+=(prime[t]-a[i][j]);
hang[i]+=(prime[t]-a[i][j]);
}
}
}
sort(lie,lie+m);
sort(hang,hang+n);
int ans=min(lie[0],hang[0]);
cout<<ans<<endl;
return 0;
}

 

举报

相关推荐

0 条评论