1002.Anti-prime Sequences
来源:互联网 发布:桃之卵淘宝 编辑:程序博客网 时间:2024/04/30 01:03
Constraints
Time Limit: 3 secs, Memory Limit: 32 MB
Description
Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement of these integers so that each adjacent pair of integers sums to a composite (non-prime) number. For example, if n = 1 and m = 10, one such anti-prime sequence is 1,3,5,4,2,6,9,7,8,10. This is also the lexicographically first such sequence. We can extend the definition by defining a degree danti-prime sequence as one where all consecutive subsequences of length 2,3,...,d sum to a composite number. The sequence above is a degree 2 anti-prime sequence, but not a degree 3, since the subsequence 5, 4, 2 sums to 11. The lexicographically .rst degree 3 anti-prime sequence for these numbers is 1,3,5,4,6,2,10,8,7,9.
Input
Input will consist of multiple input sets. Each set will consist of three integers, n, m, and d on a single line. The values of n, m and d will satisfy 1 <= n < m <= 1000, and 2 <= d <= 10. The line 0 0 0 will indicate end of input and should not be processed.
Output
For each input set, output a single line consisting of a comma-separated list of integers forming a degree danti-prime sequence (do not insert any spaces and do not split the output over multiple lines). In the case where more than one anti-prime sequence exists, print the lexicographically first one (i.e., output the one with the lowest first value; in case of a tie, the lowest second value, etc.). In the case where no anti-prime sequence exists, output No anti-prime sequence exists.
Sample Input
1 10 21 10 31 10 540 60 70 0 0
Sample Output
1,3,5,4,2,6,9,7,8,101,3,5,4,6,2,10,8,7,9No anti-prime sequence exists.40,41,43,42,44,46,45,47,48,50,55,53,52,60,56,49,51,59,58,57,54
看题第一感觉,应该是全排列,然后不断回朔,并且找到一个答案便停止搜索,Code如下:
#include <stdio.h>#include <stdlib.h>int flag=0;void findanswer(int *,int *,int ,int ,int);int judge(int );int main(){ int N,M,D; int *first,*second,i; while (scanf("%d%d%d",&N,&M,&D)==3 && !(N==0 && M==0 && D==0)) { getchar(); first=(int *)malloc((M-N+1)*sizeof(int)); second=(int *)malloc((M-N+1)*sizeof(int)); for (i=0;i<M-N+1;i++) { second[i]=N+i; first[i]=0; } findanswer(first,second,M-N+1,M-N+1,D); if (flag==0) printf("No anti-prime sequence exists.\n"); free(first); free(second); flag=0; } return 0;}void findanswer(int *first,int *second,int left,int lenth,int degree){ int i,n,tmp_num,degree_now; if (flag==1) return ; if ((lenth-left)>=2) { for (degree_now=2;degree_now<=degree;degree_now++) { for (i=lenth-left-1,n=degree_now,tmp_num=0;n>0;i--,n--) tmp_num+=first[i]; if (judge(tmp_num)==0) return ; } } if (left==0) { for (i=0;i<lenth-1;i++) printf("%d,",first[i]); printf("%d\n",first[lenth-1]); flag=1; return ; } else { for (i=0;i<lenth;i++) { if (second[i]!=0) { int *first_now,*second_now; first_now=(int *)malloc(lenth*sizeof(int)); second_now=(int *)malloc(lenth*sizeof(int)); for (n=0;n<lenth;n++) { first_now[n]=first[n]; second_now[n]=second[n]; } first_now[lenth-left]=second[i]; second_now[i]=0; findanswer(first_now,second_now,left-1,lenth,degree); free(first_now); free(second_now); } } return ; } }int judge(int num){ int i; for (i=2;i<=sqrt((double)num);i++) if (num%i==0) return 1; return 0;}
剪枝,当前面出现不符合序列时,立即退出
这是网上的一段代码,可以参考:
- #include <iostream>
- #include <cstring>
- using namespace std;
- const int N = 10000;
- int n, m, d;
- int ans[1100]; //保存答案
- bool prime[N]; //素数表
- bool vis[1100]; //访问标识
- bool f; //标记是否找到答案
- void get_prime()
- {
- memset(prime, true, sizeof(prime));
- for (int i = 2; i <= 100; i++) {
- if (prime[i]) {
- for (int j = 2; j*i <= N; j++)
- prime[j*i] = false;
- }
- }
- }
- bool check_prime(int cnt)
- {
- int sum = ans[cnt];
- int j = cnt - 1;
- for (int i = 2; i <= d && j >= 0; i++, j--) {
- sum += ans[j];
- if (prime[sum])
- return false;
- }
- return true;
- }
- void dfs(int cnt)
- {
- if (cnt == m-n+1) {
- f = true;
- return ;
- }
- for (int i = n; i <= m; i++) {
- if (vis[i])
- continue;
- ans[cnt] = i;
- if (check_prime(cnt))
- {
- vis[i] = true;
- dfs(cnt+1);
- if (f) { //已经找到答案就直接返回!
- return;
- }
- vis[i] = false;
- }
- }
- }
- int main()
- {
- get_prime();
- while (cin >> n >> m >> d) {
- if (n == 0)
- break;
- int cnt = 0;
- f = false;
- memset(vis, false, sizeof(vis));
- dfs(cnt);
- if (f) {
- for (int i = 0; i < m-n; i++) {
- cout << ans[i] << ',';
- }
- cout << ans[m-n] << endl;
- }
- else
- cout << "No anti-prime sequence exists.\n";
- }
- return 0;
- }
- 1002. Anti-prime Sequences
- 1002. Anti-prime Sequences
- 1002. Anti-prime Sequences
- 1002. Anti-prime Sequences
- 1002. Anti-prime Sequences
- 1002.Anti-prime Sequences
- 1002.Anti-prime Sequences
- [sicily]1002. Anti-prime Sequences
- [sicily online]1002. Anti-prime Sequences
- 1002: Anti-prime Sequences
- soj1002 Anti-prime Sequences
- pku 2034 Anti-prime Sequences
- POJ 2034 Anti-prime Sequences
- poj 2034 Anti-prime Sequences
- POJ 2034 Anti-prime Sequences
- sicily 1002Anti-prime Sequences
- POJ-2034-Anti-prime Sequences
- POJ 2034 Anti-prime Sequences
- 3-05 嵌入式Linux内核模块开发
- Leetcode-Sudoku Solver(数独)
- 2016-10-11要想学得精,必须得多看书
- MQTT协议中信息长度MSG len字段分析
- SOA理论篇 2(面向服务的计算环境)
- 1002.Anti-prime Sequences
- Linux系统中查看图片信息
- 学习sqlserve的一些笔记
- unity知识点收集笔记
- GridView CheckBox
- 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
- 汇编基础(四)
- Win操作系统版本号对照
- Android的事件处理