USACO 6.4 The Primes
The Primes
IOI'94
In the square below, each row, each column and the two diagonals can be read as a five digit prime number. The rows are read from left to right. The columns are read from top to bottom. Both diagonals are read from left to right.
+---+---+---+---+---+
| 1 | 1 | 3 | 5 | 1 |
+---+---+---+---+---+
| 3 | 3 | 2 | 0 | 3 |
+---+---+---+---+---+
| 3 | 0 | 3 | 2 | 3 |
+---+---+---+---+---+
| 1 | 4 | 0 | 3 | 3 |
+---+---+---+---+---+
| 3 | 3 | 3 | 1 | 1 |
+---+---+---+---+---+
- The prime numbers' digits must sum to the same number.
- The digit in the top left-hand corner of the square is pre-determined (1 in the example).
- A prime number may be used more than once in the same square.
- If there are several solutions, all must be presented (sorted in numerical order as if the 25 digits were all one long number).
- A five digit prime number cannot begin with a zero (e.g., 00003 is NOT a five digit prime number).
PROGRAM NAME: prime3
INPUT FORMAT
A single line with two space-separated integers: the sum of the digits and the digit in the upper left hand corner of the square.
SAMPLE INPUT (file prime3.in)
11 1
OUTPUT FORMAT
Five lines of five characters each for each solution found, where each line in turn consists of a five digit prime number. Print a blank line between solutions. If there are no prime squares for the input data, output a single line containing "NONE".
SAMPLE OUTPUT (file prime3.out)
The above example has 3 solutions.
11351
14033
30323
53201
13313 11351
33203
30323
14033
33311 13313
13043
32303
50231
13331 ————————————————————————题解
其实这又是一道搜索顺序至关重要的搜索题
计算机比人脑强大的地方就是能做大量重复的复杂运算
我们发现主对角线其实影响最大,然后是次对角线,然后逐渐再将限制较多的行或列填上,例如最后一行或者最后一列的数必须是1,3,7,9
预处理出所有5位、各数位的和为n的素数。
按照这样的搜索顺序
1 4 6 5 2
8 1 7 2 8
10 4 1 5 10
9 2 7 1 9
2 3 3 3 1
这样我们可以就可以少打几个循环了……
在计算素数的时候同时求一些一些数位固定的数
【其中XYZ为已知数,.为未知数,_ 为1,3,7,9】
例如枚举1,X ... _
枚举2,_ . X . _
枚举3,X_ _ _ Y
枚举4,5 .X . Y Z
6可以直接计算
枚举7 X . Y . Z
枚举8,9 . X Y Z _
10可以直接计算
然后时限为2s的题就可以0.011过了
/*
LANG: C++
PROG: prime3
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define siji(i,x,y) for(int i=(x); i<=(y) ; ++i)
#define gongzi(j,x,y) for(int j=(x); j>=(y) ; --j)
#define ivorysi
using namespace std;
int n,fi;
int p[][],sum,prime[],tot,all;
bool noprime[];
typedef struct {
int l,r[];
}RECORD200;
/*typedef struct {
int l,r[50];
}RECORD50;*/
typedef struct {
int l,r[];
}RECORD20;
//.0-9 _ 1.3.7.9 - non-zero
RECORD200 pat1[];//X..._ 4*10*10
RECORD200 pat2[];//_.X._ 4*4*10
RECORD20 pat3[][];//X___Y 4*4
RECORD20 pat4[][][];//-X.YZ 9
RECORD20 pat7[][][];//X.Y.Z 10
RECORD20 pat8[][][];//.XYZ_ 4
string sol[];
int marks[][];
bool check(int x) {
if(x%== || x%==) return ;
return ;
}
void makeprime() {
siji(i,,) {
if(!noprime[i]) {
prime[++tot]=i;
if(i>) {
int temp=;
for(int k=;k<=;k*=) {
temp+=i/k%;
}
if(temp==n) {
++sum;
for(int k=,m=;k<= && m>=;k*=,--m) {
p[sum][m]=i/k%;
}
int t1,t2,t3,t4,t5;
t1=p[sum][],t2=p[sum][],t3=p[sum][],t4=p[sum][],t5=p[sum][];
pat1[t1].r[++pat1[t1].l]=sum;
if(check(t1)) pat2[t3].r[++pat2[t3].l]=sum;
if(check(t2)&&check(t3)&&check(t4)) {
pat3[t1][t5].r[++pat3[t1][t5].l]=sum;
}
pat4[t2][t4][t5].r[++pat4[t2][t4][t5].l]=sum;
pat7[t1][t3][t5].r[++pat7[t1][t3][t5].l]=sum;
pat8[t2][t3][t4].r[++pat8[t2][t3][t4].l]=sum;
}
}
}
for(int j=;j<=tot && i</prime[j];++j) {
noprime[prime[j]*i]=;
if(i%prime[j]==) break;
}
}
}
void addsolution() {
++all;
siji(i,,) {
siji(j,,) {
sol[all].append(,marks[i][j]+'');
}
}
}
int num(int x) {
int res=;
siji(i,,) res=res*+p[x][i];
return res;
}
void solve() {
scanf("%d%d",&n,&fi);
makeprime();
int us,tj,tk,tm,tw,ty,tx,tq,temp,temp1,temp2;
for(int i=;i<=pat1[fi].l;++i) {
memset(marks,,sizeof(marks));
us=pat1[fi].r[i];
siji(iv,,) marks[iv][iv]=p[us][iv];
tj=pat2[marks[][]].l;
for(int j=;j<=tj;++j) {
us=pat2[marks[][]].r[j];
siji(iv,,) marks[-iv+][iv]=p[us][iv];
tk=pat3[marks[][]][marks[][]].l;
for(int k=;k<=tk;++k) {
us=pat3[marks[][]][marks[][]].r[k];
siji(iv,,) marks[][iv]=p[us][iv];
tm=pat4[marks[][]][marks[][]][marks[][]].l;
for(int m=;m<=tm;++m) {
us=pat4[marks[][]][marks[][]][marks[][]].r[m];
marks[][]=p[us][];
marks[][]=p[us][];
tw=pat4[marks[][]][marks[][]][marks[][]].l;
for(int w=;w<=tw;++w) {
us=pat4[marks[][]][marks[][]][marks[][]].r[w];
marks[][]=p[us][];
marks[][]=p[us][];
marks[][]=;
temp=;
siji(iv,,) temp+=marks[][iv];
marks[][]=n-temp;
if(marks[][]< || marks[][]>) continue;
temp=;
siji(iv,,) {
temp=temp*+marks[][iv];
}
if(noprime[temp]) continue;
ty=pat7[marks[][]][marks[][]][marks[][]].l;
for(int y=;y<=ty;++y) {
us=pat7[marks[][]][marks[][]][marks[][]].r[y];
marks[][]=p[us][];
marks[][]=p[us][];
tx=pat8[marks[][]][marks[][]][marks[][]].l;
for(int x=;x<=tx;++x) {
us=pat8[marks[][]][marks[][]][marks[][]].r[x];
marks[][]=p[us][];
marks[][]=p[us][];
tq=pat8[marks[][]][marks[][]][marks[][]].l;
for(int q=;q<=tq;++q) {
us=pat8[marks[][]][marks[][]][marks[][]].r[q];
marks[][]=p[us][];
marks[][]=p[us][];
marks[][]=marks[][]=;
temp1=temp2=;
siji(iv,,) temp1+=marks[iv][],temp2+=marks[iv][];
marks[][]=n-temp1;marks[][]=n-temp2;
if(marks[][]< || marks[][]< ||marks[][]> || marks[][]>)
continue;
temp1=temp2=;
siji(iv,,)
temp1=temp1*+marks[iv][],temp2=temp2*+marks[iv][];
if(noprime[temp1]||noprime[temp2]) continue;
temp1=temp2=;
siji(iv,,)
temp1=temp1*+marks[][iv],temp2+=marks[][iv];
if(noprime[temp1] || temp2!=n) continue;
addsolution();
}
}
}
}
}
}
}
}
if(all==) puts("NONE");
sort(sol+,sol+all+);
siji(i,,all) {
for(int j=;j<;++j) {
cout<<sol[i].substr(j*,)<<endl;
}
if(i!=all) puts("");
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("prime3.in","r",stdin);
freopen("prime3.out","w",stdout);
#else
freopen("f1.in","r",stdin);
freopen("f1.out","w",stdout);
#endif
solve();
return ;
}
USACO 6.4 The Primes的更多相关文章
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- USACO 6.4 章节
The Primes 题目大意 5*5矩阵,给定左上角 要所有行,列,从左向右看对角线为质数,没有前导零,且这些质数数位和相等(题目给和) 按字典序输出所有方案... 题解 看上去就是个 无脑暴搜 题 ...
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
随机推荐
- Hadoop生态圈-使用MapReduce处理HBase数据
Hadoop生态圈-使用MapReduce处理HBase数据 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.对HBase表中数据进行单词统计(TableInputFormat) ...
- Hadoop生态圈-桶表和分区表
Hadoop生态圈-桶表和分区表 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- 我的Pycharm,我做主
之间花了一周多的时间把Pycharm官方帮助文档翻译了一遍,一共43篇博客,累得要屎,感悟颇多. 发牢骚之前先总结点干货,这里把所有的翻译文档列成如下目录,方便大家索引: 最全Pycharm教程(1) ...
- 详解 Cookie 纪要(vue.cookie,jquery.cookie简化)
今天看到一篇cookie的文章,写的特别详细,感谢 晚晴幽草轩 的分享,原文链接http://www.jeffjade.com/2016/10/31/115-summary-of-cookie/ 原文 ...
- 如何利用mount命令挂载另一台服务器上的目录
文件服务器(被挂载机):192.168.1.100 操作机(挂载到机):192.168.1.200 也就是说,你在操作机上进行的操作,实际上都到文件服务器上去了: 1. 开启NFS服务: 在文件服务器 ...
- 遇到can not resolve app 依赖包的问题
1.第一种解决方式:build->sdkmannger->sdk tool ->安装android support responsitory和google responsitory ...
- Django 2.0.1 官方文档翻译: 快速安装向导 (Page5)
快速安装向导 (Page 5) 在你使用 Django 前,你需要先安装它.我们有一个完整的安装向导,它包含所有涉及的内容,这个向导会指导你进行一个简单的.最小化的安装,当你通过浏览介绍内容的时候,这 ...
- JedisCluster实践
1. Spring中运用JedisCluster http://blog.csdn.net/u010739551/article/details/52438101[spring集成 JedisClus ...
- spring boot(三):spring data jpa的使用
@RequestMapping("/queryUserListByPageNativeQuery") public String queryUserListByPageNative ...
- 浅谈ASP.net处理XML数据
XML是一种可扩展的标记语言,比之之前谈到的html有着很大的灵活性,虽然它只是与HTML仅有一个字母只差,但两者有很大的区别. XML也是标记语言,所以它每个标签必须要闭合,而HTML偶尔忘了闭合也 ...