B. Prime Matrix
Test instructions is very simple, that is, to construct a prime matrix based on a given matrix requires a minimum number of times plus 1 operations, the so-called Prime matrix is a matrix row or a column is all prime. Here we can think of a different way of thinking, we can find the matrix of a row (or a column) of the element from its nearest prime number of the difference, and then the difference is sorted, the minimum is the desired. Of course, we need to pre-order a prime array to store, which is also needed to learn.
1#include <iostream>2#include <algorithm>3 #defineN 10000114 using namespacestd;5 BOOLP[n];6 intS[n];7 intx[501][501];8 intr[501], c[501];9 voidInit ()//Create a list of primesTen { OneMemset (P,1,sizeof(P)); A for(inti =2; I*i < N; i++) - { - for(intj = i; I*j < N; J + +) theP[I*J] =0; - } -s[0] =0; - for(inti =2; i<n; i++) + if(P[i]) -s[++s[0]] =i; + } A intSolveintX//find the distance from known quantity to the nearest prime number at { - intpos = Lower_bound (S +1, S +1+ s[0], X)-s; - returnS[pos]-x; - } - intMain () - { in intN, M,i,j,ans; - init (); to while(Cin >> N >>m) + { -Memset (R,0,sizeof(R)); theMemset (c,0,sizeof(c)); * for(i =1; I <= N; i++){ $ for(j =1; J <= M; J + +)Panax Notoginseng { -CIN >>X[i][j]; theR[i] + =solve (x[i][j]); +C[J] + =solve (x[i][j]); A } the } +Sort (R +1, R +1+n); -Sort (c +1, C +1+m); $ans = min (r[1], c[1]); $cout << ans <<Endl; - return 0; - } the}
Codeforces Round #166 (Div. 2)