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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #define M 100000+10
     5 
     6 int a[509][509];
     7 int prime[100000+10];
     8 int main(void)
     9 {
    10   int n, m;
    11   //freopen("271b.in", "r", stdin);
    12   prime[0] = prime[1] = 1;
    13   for (int i = 2; i * i < M; ++i)
    14     if (!prime[i])
    15       for (int j = i * 2; j < M; j+=i)
    16         prime[j] = 1;
    17   while (~scanf("%d%d", &n, &m))
    18   {
    19     for (int i = 0; i < n; ++i)
    20       for (int j = 0; j < m; ++j)
    21       {
    22         scanf("%d", &a[i][j]);
    23         for (int k = a[i][j]; k < M; ++k)
    24           if (!prime[k])
    25           {
    26             a[i][j] = k - a[i][j]; 
    27             break;
    28           }
    29       }
    30     int min = (M)*10000;
    31     for (int i = 0; i < n; ++i)
    32     {
    33       int sum = 0;
    34       for (int j = 0; j < m; ++j)
    35         sum += a[i][j];
    36       if (sum < min)
    37         min = sum;
    38     }
    39     for (int i = 0; i < m; ++i)
    40     {
    41       int sum = 0;
    42       for (int j = 0; j < n; ++j)
    43         sum += a[j][i];
    44       if (sum < min)
    45         min = sum;
    46     }
    47     printf("%d\n", min);
    48   }
    49 
    50   return 0;
    51 }

    用到了素数筛选法。

    这道题目关键是要有想法。

    求出矩阵里面所有的数字和比它大的最小的素数的差的绝对值,然后求出每行,每列之和的最小值即可。

  • 相关阅读:
    QT中的qmake详解
    Qt setStyleSheet 添加背景色/背景图片(取消背景色,读取本地文件作为背景色)
    目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的选择
    分页写法小结
    SharePoint 2013 搜索高级配置(Search Scope)
    Scut游戏服务器免费开源框架-3
    物理数据模型(PDM)->概念数据模型 (CDM)->面向对象模型 (OOM):适用于已经设计好数据库表结构了。
    一种不错的扩展方式
    ORM框架
    代码最简化
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2912817.html
Copyright © 2011-2022 走看看