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.
InputThe 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.
OutputPrint 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.
Examples3 3 1 2 3 5 6 1 4 4 1
1
2 3 4 8 8 9 2 9
3
2 2 1 3 4 2
0
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.
第一次的代码时间复杂度太高,超时for循环次数太多 代码:
- #include<iostream>
- #include<vector>
- #include<algorithm>
- using namespace std;
- int a[1000][1000];
- int sushu(int n)
- {
- if(n==1) return 0;
- else if(n==2) return 1;
- else
- for(int i=2;i*i<=n;i++)
- {
- if(n%i==0) return 0;
- }
- return 1;
- }
- int main()
- {
- int i,j,m,n,k,num=0,t,flag=0,flag1=0
- vector <int> b;
- cin>>m>>n;
- for(i=0;i<m;i++)
- for(j=0;j<n;j++)
- cin>>a[i][j];
- for(i=0;i<m;i++)
- {
- num=0;
- for(j=0;j<n;j++)
- {
- if(sushu(a[i][j])==0)
- {
- for(k=a[i][j];;k++)
- {
- num++;
- if(sushu(k))
- {
- //b.push_back(num);
- //flag=1;
- break;
- }
- }
- //if(flag) break;
- }
- }
- b.push_back(num);
- }
- for(j=0;j<n;j++)
- {
- num=0;
- for(i=0;i<m;i++)
- {
- if(sushu(a[i][j])==0)
- {
- for(k=a[i][j];;k++)
- {
- num++;
- if(sushu(k))
- {
- //b.push_back(num);
- //flag1=1;
- break;
- }
- }
- //if(flag1) break;
- }
- }
- }
- sort(b.begin(),b.end());
- cout<<b[0]<<endl;
- // for(i=0;i<b.size();i++)
- // cou
- return 0;
- }
第二次优化之后的代码:
- #include<iostream>
- #include<algorithm>
- using namespace std;
- int a[1000][1000],H[2500]={0},dp[100050]={0};
- int sushu(int n)
- {
- if(n==1) return 0;
- else if(n==2) return 1;
- else
- for(int i=2;i*i<=n;i++)
- {
- if(n%i==0) return 0;
- }
- return 1;
- }
- int main()
- {
- int i,j,m,n,k,num=0,t;
- cin>>m>>n;
- for(i=0;i<m;i++)
- for(j=0;j<n;j++)
- cin>>a[i][j];
- int q=0;
- for(i=0;i<m;i++,q++)
- {
- for(j=0;j<n;j++)
- {
- if(sushu(a[i][j])==0&&dp[a[i][j]]==0)
- {
- for(k=a[i][j];;k++)
- {
- if(sushu(k))
- break;
- dp[a[i][j]]++;
- }
- }
- H[q]+=dp[a[i][j]];
- }
- }
- for(int i=0;i<n;i++,q++)
- for(int j=0;j<m;j++)
- {
- H[q]+=dp[a[j][i]];
- }
- sort(H,H+q);
- cout<<H[0]<<endl;
- return 0;
- }