Prime Matrix

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.

 

 

Title meaning:

A prime number matrix refers to a matrix in which at least one row and one column are all prime numbers. Now give you a matrix, you can choose any element in the matrix and add 1, and ask how many operations are required to turn this matrix into a matrix. Matrix of prime numbers.

Problem solving ideas:

First, through the prime number table, find the difference between each number in the matrix and the smallest prime number greater than this number, and save it in an array. 

 

Above code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 #define MAX  1000010
 6 long long s[MAX],isprime[MAX];
 7 int prime()
 8 {
 9     int i,j,k=0;
10     memset(isprime,1,sizeof(isprime));
11     isprime[0]=0;
12     isprime[1]=0;
13     for(i=2;i<=MAX;i++)
14     {
15         if(isprime[i])
16         {
17             s[k++]=i;
18         }
19         for(j=i*2;j<=MAX;j+=i)
20         {
21             isprime[j]=0;
22         }
23     }
24     return k;
25 }
26 int main()
27 {
28     int i,j,x,n,m,num,sum,min1,min2,mins;
29     int a[100][100];
30     scanf("%d%d",&n,&m);
31     memset(a,0,sizeof(a));
32     for(i=0;i<n;i++)
33     {
34         for(j=0;j<m;j++)
35         {
36             scanf("%d",&num);
37             for(x=0;x<prime();x++)
38             {
39                 if(s[x]>=num)
40                 {
41                     break;
42                 }
43             }
44             a[i][j]=s[x]-num;
45         }
46     }
47     min1=9999999999;
48     min2=9999999999;
49     for(i=0;i<n;i++)///行最小
50     {
51         sum=0;
52         for(j=0;j<m;j++)
53         {
54             sum=sum+a[i][j];
55         }
56         min1=min(min1,sum);
57     }
58     for(j=0;j<m;j++)
59     {
60         sum=0;
61         for(i=0;i<n;i++)
62         {
63             sum=sum+a[i][j];
64         }
65         min2=min(min2,sum);
66     }
67     mins=min(min1,min2);
68     printf("%d\n",mins);
69     return 0;
70 }

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162408&siteId=291194637