二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门

  1 /*
  2     二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值
  3 */
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <algorithm>
  7 using namespace std;
  8
  9 const int MAXN = 5e2 + 10;
 10 const int MAXM = 1e6 + 10;
 11 const int INF = 0x3f3f3f3f;
 12 int a[MAXN][MAXN];
 13 int mn_r[MAXN];
 14 int mn_c[MAXN];
 15 bool is_prime[MAXM];
 16 int prime[MAXM];
 17 int n, m, x, y;
 18 int tot;
 19
 20 void solve(void)
 21 {
 22     for (int i=1; i<=1e6; ++i)    is_prime[i] = true;
 23     is_prime[1] = false;
 24     for (int i=2; i<=1e6; ++i)
 25     {
 26         if (is_prime[i])
 27         {
 28             prime[++tot] = i;
 29             for (int j=i*2; j<=1e6; j+=i)    is_prime[j] = false;
 30         }
 31     }
 32 }
 33
 34 bool check_r(void)
 35 {
 36     memset (mn_r, 0, sizeof (mn_r));
 37     bool ok = true;
 38     for (int i=1; i<=n; ++i)
 39     {
 40         for (int j=1; j<=m; ++j)
 41         {
 42             if (!is_prime[a[i][j]])
 43             {
 44                 ok = false;    mn_r[j]++;
 45             }
 46         }
 47     }
 48
 49     return ok;
 50 }
 51
 52 bool check_c(void)
 53 {
 54     memset (mn_c, 0, sizeof (mn_c));
 55     bool ok = true;
 56     for (int j=1; j<=m; ++j)
 57     {
 58         for (int i=1; i<=n; ++i)
 59         {
 60             if (!is_prime[a[i][j]])
 61             {
 62                 ok = false;    mn_c[j]++;
 63             }
 64         }
 65     }
 66
 67     return ok;
 68 }
 69
 70 int sum_r(void)
 71 {
 72     int ans = INF;    int tmp = 0;
 73     for (int i=1; i<=n; ++i)
 74     {
 75         tmp = 0;    bool ok = true;
 76         for (int j=1; j<=m; ++j)
 77         {
 78             if (!is_prime[a[i][j]])
 79             {
 80                 int p = lower_bound (prime+1, prime+1+tot, a[i][j]) - prime;
 81                 if (p <= tot)    tmp += prime[p] - a[i][j];
 82                 else    {ok = false;    break;}
 83             }
 84         }
 85         if (ok) ans = min (ans, tmp);
 86     }
 87
 88     return ans;
 89 }
 90
 91 int sum_c(void)
 92 {
 93     int ans = INF;    int tmp = 0;
 94     for (int j=1; j<=m; ++j)
 95     {
 96         tmp = 0;    bool ok = true;
 97         for (int i=1; i<=n; ++i)
 98         {
 99             if (!is_prime[a[i][j]])
100             {
101                 int p = 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             }
105         }
106         if (ok) ans = min (ans, tmp);
107     }
108
109     return ans;
110 }
111
112 int main(void)        //Codeforces Round #166 (Div. 2) B. Prime Matrix
113 {
114 //    freopen ("B.in", "r", stdin);
115
116     solve ();
117     while (scanf ("%d%d", &n, &m) == 2)
118     {
119         for (int i=1; i<=n; ++i)
120         {
121             for (int j=1; j<=m; ++j)
122                 scanf ("%d", &a[i][j]);
123         }
124
125         if (check_r () || check_c ())    {puts ("0");    continue;}
126         else    printf ("%d\n", min (sum_r (), sum_c ()));
127     }
128
129     return 0;
130 }
时间: 2024-10-21 22:28:15

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix的相关文章

数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

题目传送门 1 /* 2 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11

暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

题目传送门 1 /* 2 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 #include <string> 9 using namespace std; 10 11 const int MAXN = 5e2 + 10; 12 const int

数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

题目传送门 1 /* 2 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 3 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 4 详细解释:http://blog.csdn.net/u014357885/article/details/46044287 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #in

暴力 Codeforces Round #183 (Div. 2) A. Pythagorean Theorem II

题目传送门 1 /* 2 暴力:O (n^2) 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 #include <vector> 9 using namespace std; 10 11 const int MAXN = 1e4 + 10; 12 const int INF = 0x3f3f3f3f; 13 14

构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

题目传送门 1 /* 2 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 8:43:02 7 File Name :A.cpp 8 *************************************************/ 9 1

Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's call some square matrix with integer values in its cells palind

Codeforces Round #166 (Div. 2)---D. Good Substrings(字符串)

You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring s[l-r] (1?≤?l?≤?r?≤?|s|) of string s??=??s1s2-s|s| (where |s| is the length of string s) is string ?slsl?+?1-sr. The substri

codeforces水题100道 第十三题 Codeforces Round #166 (Div. 2) A. Beautiful Year (brute force)

题目链接:http://www.codeforces.com/problemset/problem/271/A题意:给你一个四位数,求比这个数大的最小的满足四个位的数字不同的四位数.C++代码: #include <iostream> #include <algorithm> using namespace std; bool chk(int x) { int a[4]; for (int i = 0; i < 4; i ++) { a[i] = x % 10; x /= 1

Codeforces Round #166 (Div. 2)

B. Prime Matrix 题意很简单,就是求从给定矩阵基础上构造一个素数矩阵需要最少进行多少次加1操作,所谓的素数矩阵是矩阵一行或者一列全为素数.这里可以换一个思路思考,我们可以找出矩阵某行(或某列)元素距其最近的素数的差值,然后对这些差进行排序,最小的数即为所求.当然我们需要预先求一个素数数组存储起来,这也是需要学会的. 1 #include<iostream> 2 #include<algorithm> 3 #define N 1000011 4 using namesp