zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    新手如何运营自媒体?必看!
    公众号停更,短视频岗位暴增,2020年,新媒体人如何更值钱?
    别再费力讨好,先看看你的标题有没有入这些坑!
    经常反思自己的自媒体账号,为什么还只是几百的阅读量?
    文章发布显示“敏感词汇”怎么办?如何提升文章原创率?
    如何利用标题最大化引流,让属于自己原创、混剪视频的推荐量直线上升?
    【转载】JAVA字符串格式化-String.format()的使用
    【转载】浅谈大型网络入侵检测建设
    渗透测试工具 —— Nmap
    【转载】任意用户密码重置的10种常见姿势
  • 原文地址:https://www.cnblogs.com/cszlg/p/2914783.html
Copyright © 2011-2022 走看看