Codeforces Round #166 (Div. 2)

B. Prime Matrix

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

 1 #include<iostream>
 2 #include<algorithm>
 3 #define N 1000011
 4 using namespace std;
 5 bool p[N];
 6 int s[N];
 7 int x[501][501];
 8 int r[501], c[501];
 9 void init()//建立一个素数表
10 {
11     memset(p, 1, sizeof(p));
12     for (int i = 2; i*i < N; i++)
13     {
14         for (int j = i; i*j < N; j++)
15             p[i*j] = 0;
16     }
17     s[0] = 0;
18     for (int i = 2; i<N; i++)
19     if (p[i])
20         s[++s[0]] = i;
21 }
22 int solve(int x)//求已知数到最近素数的距离
23 {
24     int pos = lower_bound(s + 1, s + 1 + s[0], x) - s;
25     return s[pos] - x;
26 }
27 int main()
28 {
29     int n, m,i,j,ans;
30     init();
31     while (cin >> n >> m)
32     {
33         memset(r, 0, sizeof(r));
34         memset(c, 0, sizeof(c));
35         for (i = 1; i <= n; i++){
36             for (j = 1; j <= m; j++)
37             {
38                 cin >> x[i][j];
39                 r[i] += solve(x[i][j]);
40                 c[j] += solve(x[i][j]);
41             }
42         }
43         sort(r + 1, r + 1 + n);
44         sort(c + 1, c + 1 + m);
45         ans = min(r[1], c[1]);
46         cout << ans << endl;
47         return 0;
48     }
49 }
时间: 2024-11-09 00:18:00

Codeforces Round #166 (Div. 2)的相关文章

二分查找/暴力 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 = 0

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)---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 Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 1 #I hate that I love that I hate it 2 n = int(raw_input()) 3 s = "" 4 a = ["I hate that ","I love that ", "I hate it","I love it"] 5 fo

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我