zoukankan      html  css  js  c++  java
  • Prime Matrix(暴力出奇迹)

    Description

    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 Input

    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

    Hint

    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,问最少需要多少次这样的操作才能把这个矩阵变成一个素数矩阵。

    解题思路:

    先通过素数打表,对矩阵中的每一个数求其到大于这个数的最小素数的差值,保存到一个数组中,这个差值就是需要的操作,暴力每一行每一列得到最小操作次数。 

    上代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 #define MAX  1000010
     6 long long s[MAX],isprime[MAX];
     7 int prime()
     8 {
     9     int i,j,k=0;
    10     memset(isprime,1,sizeof(isprime));
    11     isprime[0]=0;
    12     isprime[1]=0;
    13     for(i=2;i<=MAX;i++)
    14     {
    15         if(isprime[i])
    16         {
    17             s[k++]=i;
    18         }
    19         for(j=i*2;j<=MAX;j+=i)
    20         {
    21             isprime[j]=0;
    22         }
    23     }
    24     return k;
    25 }
    26 int main()
    27 {
    28     int i,j,x,n,m,num,sum,min1,min2,mins;
    29     int a[100][100];
    30     scanf("%d%d",&n,&m);
    31     memset(a,0,sizeof(a));
    32     for(i=0;i<n;i++)
    33     {
    34         for(j=0;j<m;j++)
    35         {
    36             scanf("%d",&num);
    37             for(x=0;x<prime();x++)
    38             {
    39                 if(s[x]>=num)
    40                 {
    41                     break;
    42                 }
    43             }
    44             a[i][j]=s[x]-num;
    45         }
    46     }
    47     min1=9999999999;
    48     min2=9999999999;
    49     for(i=0;i<n;i++)///行最小
    50     {
    51         sum=0;
    52         for(j=0;j<m;j++)
    53         {
    54             sum=sum+a[i][j];
    55         }
    56         min1=min(min1,sum);
    57     }
    58     for(j=0;j<m;j++)
    59     {
    60         sum=0;
    61         for(i=0;i<n;i++)
    62         {
    63             sum=sum+a[i][j];
    64         }
    65         min2=min(min2,sum);
    66     }
    67     mins=min(min1,min2);
    68     printf("%d
    ",mins);
    69     return 0;
    70 }
  • 相关阅读:
    Vue2.0 $set()的正确使用方式
    Vue中axios踩坑之路-POST传参
    vue里使用create、mounted调用方法的正确姿势
    cmd返回上一级和根目录
    【积累】排序题积累
    【图论】2019 ICPC Malaysia National G(拓扑排序)
    2019 ICPC Malaysia National F(状态压缩)
    【线段树】扫描线学习笔记
    组合数(阶乘数质因子分解)
    【线段树】2019CCPC网络选拔赛 array(权值线段树)
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8982265.html
Copyright © 2011-2022 走看看