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的更多相关文章

  1. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  2. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  3. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  4. USACO 6.4 章节

    The Primes 题目大意 5*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 ...

  6. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  8. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  9. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

随机推荐

  1. Java基础-IO流对象之数据流(DataOutputStream与DataInputStream)

    Java基础-IO流对象之数据流(DataOutputStream与DataInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据流特点 操作基本数据类型 ...

  2. Java基础-程序流程控制第一弹(分支结构/选择结构)

    Java基础-程序流程控制第一弹(分支结构/选择结构) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.if语句 1>.if语句的第一种格式 if(条件表达式){ 语句体: ...

  3. Spring.profile实现开发、测试和生产环境的配置和切换

    软件开发过程一般涉及“开发 -> 测试 -> 部署上线”多个阶段,每个阶段的环境的配置参数会有不同,如数据源,文件路径等.为避免每次切换环境时都要进行参数配置等繁琐的操作,可以通过spri ...

  4. Typora 自定义主题 修改左右间距

    打开偏好设置,打开主题文件夹 比如要修改night主题中的间距,编辑night.css文件,修改#write样式即可. 修改其他样式类试.

  5. 洛谷 p2530 化工场装箱员(资源型)

    化工场装箱员 https://www.luogu.org/problem/show?pid=2530 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的 ...

  6. bootstrap使用记录

    http://v3.bootcss.com/components/#panels http://v2.bootcss.com/components.html

  7. Dynamic Rankings(动态第k大+树套树)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题目: 思路: 树套树板子题. 代码实现如下: #inclu ...

  8. TCP/IP 网络编程的理解

    一.网络各个协议:TCP/IP.SOCKET.HTTP等 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被称作媒体层,是网络工程 ...

  9. linux查看及设置别名,权限,生成ssh秘钥

    1.alias :查看系统中所有的命令别名 2.设定别名 alias 别名='原命令' 3.删除别名 unalias 别名 4.使别名永久生效    vi  ~/.bashrc  写入这个文件中即可永 ...

  10. torch.Tensor.view (Python method, in torch.Tensor)

    返回具有相同数据但大小不同的新张量.返回的张量共享相同的数据,必须具有相同数量的元素,但可能有不同的大小. Example >>> x = torch.randn(4, 4) > ...