Binary search/violence codeforces Round #166 (Div. 2) B. Prime Matrix

Source: Internet
Author: User

Topic Portal

1 /*2 binary Search/violence: First, the pre-screening, then the violence for each row of each column is not a prime number of binary find the nearest prime, update the minimum3 */4#include <cstdio>5#include <cstring>6#include <algorithm>7 using namespacestd;8 9 Const intMAXN = 5e2 +Ten;Ten Const intMAXM = 1e6 +Ten; One Const intINF =0x3f3f3f3f; A intA[MAXN][MAXN]; - intMN_R[MAXN]; - intMN_C[MAXN]; the BOOLIS_PRIME[MAXM]; - intPRIME[MAXM]; - intN, m, x, y; - inttot; +  - voidSolvevoid) + { A      for(intI=1; i<=1e6; ++i) Is_prime[i] =true; atis_prime[1] =false; -      for(intI=2; i<=1e6; ++i) -     { -         if(Is_prime[i]) -         { -Prime[++tot] =i; in              for(intj=i*2; j<=1e6; j+=i) Is_prime[j] =false; -         } to     } + } -  the BOOLCheck_r (void) * { $memset (Mn_r,0,sizeof(Mn_r));Panax Notoginseng     BOOLOK =true; -      for(intI=1; i<=n; ++i) the     { +          for(intj=1; j<=m; ++j) A         { the             if(!Is_prime[a[i][j]]) +             { -OK =false; mn_r[j]++; $             } $         } -     } -  the     returnOK; - }Wuyi  the BOOLCheck_c (void) - { Wumemset (Mn_c,0,sizeof(Mn_c)); -     BOOLOK =true; About      for(intj=1; j<=m; ++j) $     { -          for(intI=1; i<=n; ++i) -         { -             if(!Is_prime[a[i][j]]) A             { +OK =false; mn_c[j]++; the             } -         } $     } the  the     returnOK; the } the  - intSum_r (void) in { the     intans = INF;intTMP =0; the      for(intI=1; i<=n; ++i) About     { theTMP =0;BOOLOK =true; the          for(intj=1; j<=m; ++j) the         { +             if(!Is_prime[a[i][j]]) -             { the                 intp = Lower_bound (prime+1, prime+1+tot, A[i][j])-Prime;Bayi                 if(P <= tot) tmp + = Prime[p]-A[i][j]; the                 Else{OK =false; Break;} the             } -         } -         if(ok) ans =min (ans, tmp); the     } the  the     returnans; the } -  the intSum_c (void) the { the     intans = INF;intTMP =0;94      for(intj=1; j<=m; ++j) the     { theTMP =0;BOOLOK =true; the          for(intI=1; i<=n; ++i)98         { About             if(!Is_prime[a[i][j]]) -             {101                 intp = Lower_bound (prime+1, prime+1+tot, A[i][j])-Prime;102                 if(P <= tot) tmp + = Prime[p]-A[i][j];103                 Else{OK =false; Break;}104             } the         }106         if(ok) ans =min (ans, tmp);107     }108 109     returnans; the }111  the intMainvoid)//codeforces Round #166 (Div. 2) B. Prime Matrix113 { the //freopen ("b.in", "R", stdin); the  the solve ();117      while(SCANF ("%d%d", &n, &m) = =2)118     {119          for(intI=1; i<=n; ++i) -         {121              for(intj=1; j<=m; ++j)122scanf ("%d", &a[i][j]);123         }124  the         if(Check_r () | | Check_c ()) {Puts ("0");Continue;}126         Elseprintf ("%d\n", Min (Sum_r (), Sum_c ()));127     } - 129     return 0; the}

Binary search/violence codeforces Round #166 (Div. 2) B. Prime Matrix

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.