codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

271B.md (2084B)


      1 Source: https://codeforces.com/problemset/problem/271/B
      2 
      3 B. Prime Matrix
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 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.
     22 
     23 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.
     24 
     25 A matrix is prime if at least one of the two following conditions fulfills:
     26 
     27 * the matrix has a row with prime numbers only;
     28 * the matrix has a column with prime numbers only;
     29 
     30 Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
     31 
     32 Input
     33 
     34 The first line contains two integers *n*, *m* (1 ≤ *n*, *m* ≤ 500) — the number of rows and columns in the matrix, correspondingly.
     35 
     36 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.
     37 
     38 The numbers in the lines are separated by single spaces.
     39 
     40 Output
     41 
     42 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.
     43 
     44 Examples
     45 
     46 Input
     47 
     48 ```
     49 3 3  
     50 1 2 3  
     51 5 6 1  
     52 4 4 1
     53 ```
     54 
     55 Output
     56 
     57 ```
     58 1
     59 ```
     60 
     61 Input
     62 
     63 ```
     64 2 3  
     65 4 8 8  
     66 9 2 9
     67 ```
     68 
     69 Output
     70 
     71 ```
     72 3
     73 ```
     74 
     75 Input
     76 
     77 ```
     78 2 2  
     79 1 3  
     80 4 2
     81 ```
     82 
     83 Output
     84 
     85 ```
     86 0
     87 ```
     88 
     89 Note
     90 
     91 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.
     92 
     93 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.
     94 
     95 In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.