Codeforces Round #166 (Div. 2) B. Prime Matrix(素数筛选,简单)

B. Prime Matrix
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
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.

求出每个元素变成素数所需的操作次数a[i][j],然后分别求出每一行、每一列的∑a[i][j],取最小值即可。

复制代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 
 5 using namespace std;
 6 
 7 const int size = 502;
 8 int mat[size][size], n, m;
 9 bool p[100100] = {0, 1, 0, 0};  //素数为0
10 
11 void Prime()  //筛选素数
12 {
13     int a = sqrt(100105);
14     for(int i = 2; i < a; i++)
15     {
16         if(!p[i])
17             for(int j = 2; j * i < 100100; j++)
18                 p[i*j] = 1;
19     }
20    // for(int i = 1; i < 100; i++) cout << i << ": " << p[i] << endl;
21 }
22 
23 int Find(int i)  //寻找比合数i大且与i最接近的素数,返回两者差值
24 {
25     int j = i + 1;
26     for(; p[j]; j++){}
27     return j - i;
28 }
29 
30 int main()
31 {
32     Prime();
33     while(scanf("%d %d", &n, &m) != EOF)
34     {
35         int e, cmin = 100000000, rmin = 100000000;
36         for(int i = 0; i < n; i++)
37         {
38             for(int j = 0; j < m; j++)
39             {
40                 scanf("%d", &e);
41                 if(p[e]) mat[i][j] = Find(e);  //e为合数
42                 else mat[i][j] = 0;
43             }
44         }
45         //找出列中所需操作的最大值
46         for(int j = 0; j < m; j++)
47         {
48             int sum = 0;
49             for(int i = 0; i < n; i++)
50                 sum += mat[i][j];
51             if(cmin > sum) cmin = sum;
52         }
53         //找出列中所需操作的最大值
54         for(int i = 0; i < n; i++)
55         {
56             int sum = 0;
57             for(int j = 0; j < m; j++)
58                 sum += mat[i][j];
59             if(rmin > sum) rmin = sum;
60         }
61         if(rmin > cmin) printf("%d\n", cmin);
62         else printf("%d\n", rmin);
63     }
64     return 0;
65 }
复制代码

2013-02-17 21:47:02

posted on   铁树银花  阅读(342)  评论(0编辑  收藏  举报

编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
< 2013年2月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 1 2
3 4 5 6 7 8 9

导航

统计

点击右上角即可分享
微信分享提示