ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU 1016 Prime Ring Problem

2021-01-08 13:34:59  阅读:177  来源: 互联网

标签:Prime HDU CornflowerBlue color 质数 Problem include circle 圆环


\[\color{blue}{Prime Ring Problem} \]

\[\color{green}{Time\;Limit: 4000/2000 MS (Java/Others)\quad Memory\;Limit: 65536/32768 K (Java/Others)} \]


\(\color{CornflowerBlue}{Problem \; Description}\)

A ring is compose of \(n\) circles as shown in diagram. Put natural number \(1, 2, ..., n\) into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

Note: the number of first circle should always be \(1\).


\(\color{CornflowerBlue}{Input}\)

\(n (0 < n < 20).\)


\(\color{CornflowerBlue}{Output}\)

The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from \(1\) clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

You are to write a program that completes above process.

Print a blank line after each case.


\(\color{CornflowerBlue}{Sample\;Input}\)

6
8

\(\color{CornflowerBlue}{Sample\;Output}\)

Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2

\(\color{CornflowerBlue}{Source}\)

Asia 1996, Shanghai (Mainland China)


\(\color{CornflowerBlue}{Recommend}\)

JGShining | We have carefully selected several similar problems for you: 1010 1241 1312 1072 1242


题目描述

如图,给定一个大小为\(n\)的圆环,将正整数\(1,2,\cdots,n\)放入圆环中,使得相邻圆环上的两数相加总为质数

注意:第一个圆环上的数字总为\(1\)

输入

\(n (0 < n < 20).\)

输出

按照字典序,输出所有可能的方案;具体输出格式见样例;

Pz's solution

1.首先,考虑到最大能拼成的数为\(18+19=37\),可以先预处理出\(1 \sim 40\)之间的质数;

2.在保证前一项与当前搜索项相加为质数的情况下进行搜索;

3.当搜索到最后一个数时,特判一下\(1\)与之相加是否为质数即可;

  • TAG:搜索;质数

PZ.cpp

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 40
int n,T,ans[N],p[N];
bool vis[N],used[N];
void init(){
	vis[0]=vis[1]=1;
	for(int i=2;i<N;++i){
		if(!vis[i]) p[++p[0]]=i;
		for(int j=1;j<=p[0]&&p[j]*i<N;++j){
			vis[p[j]*i]=1;
			if(i%p[j]==0) break;
		}
	}
}
void dfs(int res){
	if(res==n){
		if(!vis[1+ans[n]])
			for(int i=1;i<=n;++i){
				printf("%d%c",ans[i],(i==n ? '\n' : ' '));
			}
		return;
	}
	for(int i=2;i<=n;++i)
		if(!used[i])
			if(!vis[ans[res]+i]){
				ans[res+1]=i;
				used[i]=1;
				dfs(res+1);
				used[i]=0;
			}
}
int main(){
	init();
	while(scanf("%d",&n)!=EOF){
		++T;
		memset(used,0,sizeof(used));
		memset(ans,0,sizeof(ans));
		ans[1]=1;
		printf("Case %d:\n",T);
		dfs(1);
		putchar('\n');//注意样例输出格式 
	}
	return 0;
}

标签:Prime,HDU,CornflowerBlue,color,质数,Problem,include,circle,圆环
来源: https://www.cnblogs.com/Potrem/p/HDU_1016.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有