题目传送门

 /*
二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = 5e2 + ;
const int MAXM = 1e6 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN][MAXN];
int mn_r[MAXN];
int mn_c[MAXN];
bool is_prime[MAXM];
int prime[MAXM];
int n, m, x, y;
int tot; void solve(void)
{
for (int i=; i<=1e6; ++i) is_prime[i] = true;
is_prime[] = false;
for (int i=; i<=1e6; ++i)
{
if (is_prime[i])
{
prime[++tot] = i;
for (int j=i*; j<=1e6; j+=i) is_prime[j] = false;
}
}
} bool check_r(void)
{
memset (mn_r, , sizeof (mn_r));
bool ok = true;
for (int i=; i<=n; ++i)
{
for (int j=; j<=m; ++j)
{
if (!is_prime[a[i][j]])
{
ok = false; mn_r[j]++;
}
}
} return ok;
} bool check_c(void)
{
memset (mn_c, , sizeof (mn_c));
bool ok = true;
for (int j=; j<=m; ++j)
{
for (int i=; i<=n; ++i)
{
if (!is_prime[a[i][j]])
{
ok = false; mn_c[j]++;
}
}
} return ok;
} int sum_r(void)
{
int ans = INF; int tmp = ;
for (int i=; i<=n; ++i)
{
tmp = ; bool ok = true;
for (int j=; j<=m; ++j)
{
if (!is_prime[a[i][j]])
{
int p = lower_bound (prime+, prime++tot, a[i][j]) - prime;
if (p <= tot) tmp += prime[p] - a[i][j];
else {ok = false; break;}
}
}
if (ok) ans = min (ans, tmp);
} return ans;
} int sum_c(void)
{
int ans = INF; int tmp = ;
for (int j=; j<=m; ++j)
{
tmp = ; bool ok = true;
for (int i=; i<=n; ++i)
{
if (!is_prime[a[i][j]])
{
int p = lower_bound (prime+, prime++tot, a[i][j]) - prime;
if (p <= tot) tmp += prime[p] - a[i][j];
else {ok = false; break;}
}
}
if (ok) ans = min (ans, tmp);
} return ans;
} int main(void) //Codeforces Round #166 (Div. 2) B. Prime Matrix
{
// freopen ("B.in", "r", stdin); solve ();
while (scanf ("%d%d", &n, &m) == )
{
for (int i=; i<=n; ++i)
{
for (int j=; j<=m; ++j)
scanf ("%d", &a[i][j]);
} if (check_r () || check_c ()) {puts (""); continue;}
else printf ("%d\n", min (sum_r (), sum_c ()));
} return ;
}

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

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

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

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

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

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

    题目传送门 /* 暴力:O (n^2) */ #include <cstdio> #include <algorithm> #include <cstring> # ...

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

    题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...

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

    题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...

  6. Codeforces Round #166 (Div. 2) A. Beautiful Year【暴力枚举/逆向思维/大于当前数且每个位数不同】

    A. Beautiful Year time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

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

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

  8. 【二分答案】Codeforces Round #402 (Div. 2) D. String Game

    二分要删除几个,然后暴力判定. #include<cstdio> #include<cstring> using namespace std; int a[200010],n, ...

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

    题目链接:http://www.codeforces.com/problemset/problem/271/A题意:给你一个四位数,求比这个数大的最小的满足四个位的数字不同的四位数.C++代码: #i ...

随机推荐

  1. Null value was assigned to a property of primitive type setter of原因及解决

    出现Null value was assigned to a property of primitive type setter of错误是由于类型不匹配,将字段的属性由hibernate的int类型 ...

  2. 阅读《Android 从入门到精通》(33)——Intent 分类

    Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...

  3. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  4. js性能优化之函数节流(分流函数)

    函数节流的原理 比如我们在window.onresize事件中要打印当前浏览器窗口的大小,在我们通过拖拽来改变窗口大小时候,打印窗口大小这个工作1s就运行了10次.而实际上我们只需要2次或者3次. 比 ...

  5. Zookeeper 3.4 官方文档翻译

    说明 个人英语水平非常一般,理解可能有偏差,假设有翻译不恰当之处,请看官指点. 1.简单介绍 分布式系统就像动物园.当中每台server就像一仅仅动物,Zookeeper就像动物园管理员,协调.服务于 ...

  6. unique函数(STL)

    unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除.他是c++中的函数,所以头文件 ...

  7. LoadRunner系列之—-03 用Java Vuser协议编写接口测试脚本

    待测试接口用java语言实现,且项目中调用该接口需要用专门的jar包.这种情况可以用Java Vuser协议实现接口调用脚本,类似java代码. 代码样例如下: /* * LoadRunner Jav ...

  8. Cleave js 使用

    1111111111111111 xxxxxx Cleave.js 键入时格式化< input />内容   信用卡号码格式 明确   美国运通:从34/37开始 34   签证:从4开始 ...

  9. 清理一下电脑垃圾,打开Eclipse发现左边的所有项目消失了

    使用360清理一下电脑垃圾,打开Eclipse发现左边的所有项目消失了,但在相应的workspace能够找到项目,这个问题已经是第三次遇到了(预计是被360清理掉Eclipse的一些部署或配置文件所导 ...

  10. java语法基础(四)

    继承 继承概述 继承是面向对象语言的三大基本特性(封装,继承,多态)之一. 一个类可以继承另外一个类,继承的类称为子类(也可以叫派生类),被继承的类称为父类(或者也叫基类,超类). 通过继承,子类可以 ...