CodeForces - 271B

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.

第一次的代码时间复杂度太高,超时for循环次数太多 代码:

  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5. int a[1000][1000];
  6. int sushu(int n)
  7. {
  8. if(n==1) return 0;
  9. else if(n==2) return 1;
  10. else
  11. for(int i=2;i*i<=n;i++)
  12. {
  13. if(n%i==0) return 0;
  14. }
  15. return 1;
  16. }
  17. int main()
  18. {
  19. int i,j,m,n,k,num=0,t,flag=0,flag1=0
  20. vector <int> b;
  21. cin>>m>>n;
  22. for(i=0;i<m;i++)
  23. for(j=0;j<n;j++)
  24. cin>>a[i][j];
  25. for(i=0;i<m;i++)
  26. {
  27. num=0;
  28. for(j=0;j<n;j++)
  29. {
  30. if(sushu(a[i][j])==0)
  31. {
  32. for(k=a[i][j];;k++)
  33. {
  34. num++;
  35. if(sushu(k))
  36. {
  37. //b.push_back(num);
  38. //flag=1;
  39. break;
  40. }
  41. }
  42. //if(flag) break;
  43. }
  44. }
  45. b.push_back(num);
  46. }
  47. for(j=0;j<n;j++)
  48. {
  49. num=0;
  50. for(i=0;i<m;i++)
  51. {
  52. if(sushu(a[i][j])==0)
  53. {
  54. for(k=a[i][j];;k++)
  55. {
  56. num++;
  57. if(sushu(k))
  58. {
  59. //b.push_back(num);
  60. //flag1=1;
  61. break;
  62. }
  63. }
  64. //if(flag1) break;
  65. }
  66. }
  67. }
  68. sort(b.begin(),b.end());
  69. cout<<b[0]<<endl;
  70. // for(i=0;i<b.size();i++)
  71. // cou
  72. return 0;
  73. }

第二次优化之后的代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int a[1000][1000],H[2500]={0},dp[100050]={0};
  5. int sushu(int n)
  6. {
  7. if(n==1) return 0;
  8. else if(n==2) return 1;
  9. else
  10. for(int i=2;i*i<=n;i++)
  11. {
  12. if(n%i==0) return 0;
  13. }
  14. return 1;
  15. }
  16. int main()
  17. {
  18. int i,j,m,n,k,num=0,t;
  19. cin>>m>>n;
  20. for(i=0;i<m;i++)
  21. for(j=0;j<n;j++)
  22. cin>>a[i][j];
  23. int q=0;
  24. for(i=0;i<m;i++,q++)
  25. {
  26. for(j=0;j<n;j++)
  27. {
  28. if(sushu(a[i][j])==0&&dp[a[i][j]]==0)
  29. {
  30. for(k=a[i][j];;k++)
  31. {
  32. if(sushu(k))
  33. break;
  34. dp[a[i][j]]++;
  35. }
  36. }
  37. H[q]+=dp[a[i][j]];
  38. }
  39. }
  40. for(int i=0;i<n;i++,q++)
  41. for(int j=0;j<m;j++)
  42. {
  43. H[q]+=dp[a[j][i]];
  44. }
  45. sort(H,H+q);
  46. cout<<H[0]<<endl;
  47. return 0;
  48. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值