Pratik and Magical Array
All submissions for this problem are available.
Pratik's favourite hobby is to solve puzzles. His mathematics teacher K.R. Atal Sir has given him a unique problem to solve and for the first time Pratik needs help. The problem goes as follows.
You are given a 2-D array of size N x M consisting of integers to which you can apply the following two operations with an associated cost :-
1. Choose an element from the array and increment its value by unity.
2. Choose an element from the array and decrement its value by unity.
The 2-D array is magical only if it fulfills the following criterion:
The matrix has a particular row or column consisting of only prime numbers.
Print the minimum possible cost required to convert this 2-D array to a Magical Array using the given operations. All the elements in the array do not exceed 10^5.
First line contains two space separated integers N and M denoting the number of rows and columns of the matrix respectively.
Second line contains two space separated integers A and B, the cost associated with the two operations respectively.
Each of the following N lines contains M space separated integers, the initial rectangular array.
For every test case, print a single integer - the minimum cost to convert the rectangular array to a magical array.
1 ≤ N,M ≤ 500
1 ≤ A,B ≤ 10^7
1 2 8
5 6 1
4 4 1
For minimum possible cost one can increment the cell at (1,1) and (3,1) by unity. So the total associated cost with these two operations is 5+5 = 10.
Alternatively, one can also increment the cell at (2,2) and (2,3) by unity to get the minimum cost of 10.
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS|
Fetching successful submissions