Codeforces Round #166 (Div. 2)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.