Prime Matrix(暴力出奇迹)
Description
You've got an n × m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.
You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.
A matrix is prime if at least one of the two following conditions fulfills:
- the matrix has a row with prime numbers only;
- the matrix has a column with prime numbers only;
Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
Input
The first line contains two integers n, m(1 ≤ n, m ≤ 500) — the number of rows and columns in the matrix, correspondingly.
Each of the following n lines contains m integers — the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.
The numbers in the lines are separated by single spaces.
Output
Print a single integer — the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
Sample Input
3 3
1 2 3
5 6 1
4 4 1
1
2 3
4 8 8
9 2 9
3
2 2
1 3
4 2
0
Hint
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.
In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.
In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.
题目意思:
素数矩阵是指一个矩阵中存在至少一行和一列全是素数的矩阵,现在给你一个矩阵,你可以选择矩阵中任意一个元素加1,问最少需要多少次这样的操作才能把这个矩阵变成一个素数矩阵。
解题思路:
先通过素数打表,对矩阵中的每一个数求其到大于这个数的最小素数的差值,保存到一个数组中,这个差值就是需要的操作,暴力每一行每一列得到最小操作次数。
上代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 1000010
long long s[MAX],isprime[MAX];
int prime()
{
int i,j,k=;
memset(isprime,,sizeof(isprime));
isprime[]=;
isprime[]=;
for(i=;i<=MAX;i++)
{
if(isprime[i])
{
s[k++]=i;
}
for(j=i*;j<=MAX;j+=i)
{
isprime[j]=;
}
}
return k;
}
int main()
{
int i,j,x,n,m,num,sum,min1,min2,mins;
int a[][];
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
scanf("%d",&num);
for(x=;x<prime();x++)
{
if(s[x]>=num)
{
break;
}
}
a[i][j]=s[x]-num;
}
}
min1=;
min2=;
for(i=;i<n;i++)///行最小
{
sum=;
for(j=;j<m;j++)
{
sum=sum+a[i][j];
}
min1=min(min1,sum);
}
for(j=;j<m;j++)
{
sum=;
for(i=;i<n;i++)
{
sum=sum+a[i][j];
}
min2=min(min2,sum);
}
mins=min(min1,min2);
printf("%d\n",mins);
return ;
}
Prime Matrix(暴力出奇迹)的更多相关文章
- SID1190471 / 烦人的幻灯片 暴力出奇迹 !!!!!!!!!!!!!!!!!!
PID221 / 烦人的幻灯片 ☆ 提交你的代码 查看讨论和题解 你还木有做过哦 我的状态 查看最后一次评测记录 质量还不能统计出来哦~ 题目评价 质量 无 ★★★★★ ★★★★☆ ★ ...
- 紫书 习题 8-2 UVa 1610 (暴力出奇迹)
这道题我真的想的非常的复杂, 拿草稿纸一直在找规律,推公式, 然后总有一些特殊的情况. 然后就WA了N次.无奈之下看了别人的博客, 然后就惊了.直接暴力枚举两个相邻字符串 里面的所有可能就可以了--真 ...
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...
- Codeforces - 570D 离散DFS序 特殊的子树统计 (暴力出奇迹)
题意:给定一棵树,树上每个节点有对应的字符,多次询问在\(u\)子树的深度为\(d\)的所有节点上的字符任意组合能否凑成一个回文串 把dfs序存储在一个二维线性表中,一个维度记录字符另一个维度记录深度 ...
- 51nod——1285 山峰和分段(暴力出奇迹)
要求每段的点数都一样,因此分的段数cnt肯定是n的因子,要求每段都有山峰,因此cnt肯定小于等于山峰数量.分段的宽度d=n/cnt,对山峰数量做一个前缀和,检查一下每一段的山峰数量是否没有增加即可. ...
- 【杂】暴力出奇迹,lz水数据
做了个填涂颜色的题qwq 洛谷上的qwq,然后我就把这道题水过去了qwq(显然这是不对的,我们不能水数据qwq)当然我本身是80分的qwq end-
- Post Lamps CodeForces - 990E(暴力出奇迹?)
题意: 在一个从0开始的连续区间上 放置几个小区间,使得这些小区间覆盖整个大区间,不同长度的小区间有不同的花费,其中有m个点,小区间的左端点不能放在这些点上 解析: 显然如果0是这m点中的一个 则无 ...
- HDU4587--TWO NODES(无向图割点,暴力出奇迹)这是我见过的时间最长的题。。。
TWO NODES Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix
题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...
随机推荐
- 集合之TreeMap
TreeMap 底层数据结构是二叉树 如何保证键的唯一: 利用存的特点 如何保证键的可排序: 利用取的特点 左跟右 在map中数据结构只对键有效TreeMap 有Map的键值对的特性:还可以进行排序, ...
- DXP常用的设置及快捷键
原文地址:http://www.cnblogs.com/NickQ/p/8799240.html 测试环境:Altium Designer Summer 16 一.快捷键 1.原理图和PCB通用快捷键 ...
- python学习第三天 -----2019年4月23日
第三周-第03章节-Python3.5-集合及其运算 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 ...
- 『Linux基础 - 5 』Linux常用命令(2)
这篇笔记的只要知识点: (1)ls查看文件信息,列表中每个字符所代表的含义 (2) 使用通配符匹配文件 (3) chmod命令:修改文件或目录权限 (4) 与用户相关命令(who.su.exit.pa ...
- MFC实现http连接、发送和接收数据
#include <afxinet.h> // 设置超时 CInternetSession session; session.SetOption(INTERNET_OPTION_CONNE ...
- java随笔一(关于定时任务)
public class ThreadTest { class MyTask implements Runnable{ public void run() { say(); } } public vo ...
- 统一建模语言——UML
一.UML概述 Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言,为软件开发的 ...
- Python-特殊变量
from test import test ''' __mame__ __file__ __cache__ __package__ ''' # import os # 获取这个当前文件的绝对路径 # ...
- OSG-基础知识-程序框架
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- WEB安全基础之sql注入基础
1.基础sql语句 注释 单行注释# %23--+ --加空格多行注释/**/ SELECT(VERSION()) SELECT(USER()) SELECT(database()) 查数据库 SEL ...