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. 1 10 2
  2. 1 10 3
  3. 1 10 5
  4. 40 60 7
  5. 0 0 0

Sample Output

  1. 1,3,5,4,2,6,9,7,8,10
  2. 1,3,5,4,6,2,10,8,7,9
  3. No anti-prime sequence exists.
  4. 40,41,43,42,44,46,45,47,48,50,55,53,52,60,56,49,51,59,58,57,54

需要一个 l 到 r 的排列,使得任意一个长度是2,3,...,k的子串当中子串和都不是质数

直接搜就是了

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #define LL long long
  5. using namespace std;
  6. inline LL read()
  7. {
  8. LL x=,f=;char ch=getchar();
  9. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  10. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  11. return x*f;
  12. }
  13. int l,r,d,haveans=;
  14. bool mk[];
  15. bool mrk[];
  16. int s[];
  17. int p[],len;
  18. inline void getp()
  19. {
  20. for (int i=;i<=;i++)
  21. {
  22. if (!mk[i])
  23. {
  24. p[++len]=i;
  25. for (int j=*i;j<=;j+=i)mk[j]=;
  26. }
  27. }
  28. }
  29. inline void dfs(int now)
  30. {
  31. if (now==r+)
  32. {
  33. for (int i=l;i<r;i++)printf("%d,",s[i]);
  34. printf("%d\n",s[r]);
  35. haveans=;
  36. return;
  37. }
  38. for (int i=l;i<=r;i++)
  39. {
  40. if (mrk[i])continue;
  41. int sum=i,mrk2=;
  42. for (int j=now-;j>=max(l,now-d+);j--)
  43. {
  44. sum+=s[j];
  45. if(!mk[sum]){mrk2=;break;}
  46. }
  47. if (!mrk2)continue;
  48. mrk[i]=;
  49. s[now]=i;
  50. dfs(now+);
  51. if (haveans)return;
  52. s[now]=;
  53. mrk[i]=;
  54. }
  55. }
  56. int main()
  57. {
  58. getp();
  59. while (~scanf("%d%d%d",&l,&r,&d)&&l+r+d)
  60. {
  61. memset(mrk,,sizeof(mrk));
  62. haveans=;
  63. dfs(l);
  64. if (!haveans)puts("No anti-prime sequence exists.");
  65. }
  66. }

poj 2034

[暑假集训--数论]poj2034 Anti-prime Sequences的更多相关文章

  1. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  2. [暑假集训--数论]poj1365 Prime Land

    Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...

  3. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  4. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

  5. [暑假集训--数论]poj2262 Goldbach's Conjecture

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...

  6. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  7. [暑假集训--数论]poj2773 Happy 2006

    Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD ...

  8. [暑假集训--数论]hdu1019 Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  9. [暑假集训--数论]poj2115 C Looooops

    A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...

随机推荐

  1. Springboot端口设置

    application.properties 加入 server.port=80

  2. linux下vim命令

    进入vim的命令 vim filename :打开或新建文件,并将光标置于第一行首 vim +n filename :打开文件,并将光标置于第n行首 vim + filename :打开文件,并将光标 ...

  3. vue系列之vue cli 3引入ts

    插件 Vue2.5+ Typescript 引入全面指南 vue-class-component强化 Vue 组件,使用 TypeScript/装饰器 增强 Vue 组件 vue-property-d ...

  4. Python 正则表达式 贪心匹配和非贪心匹配

    Python的正则表达式默认是“贪心匹配”,即在有第二义的情况下,尽可能匹配最长的字符串,在正则表达式的花括号后面跟上问号,可以变为非贪心模式 >>> >>> ha ...

  5. Gender Equality in the Workplace【职场上的性别平等】

    Gender Equality in the Workplace A new batch of young women - members of the so-called Millennial ge ...

  6. Ubuntu下的定时备份数据库

    1.编写备份数据库的shell脚本 mysqldump -uUserName -pPassword dbName >/XXX/XXXX/XXXX/fileName_$(date +%Y%m%d_ ...

  7. 重写BaseAdapter实现ListView

    public class BaseAdapterActivity extends BaseActivity { private ListView base_adapter_listView; priv ...

  8. Kinect安装

    在连接kinect机器前,需要先安装两个软件,而在安装这两个软件前需要有vs2010(专业版本和快速版),因为需要包含.net framework 4.0 kinect sdk http://www. ...

  9. java中equals和==

    https://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html

  10. 云计算之路-阿里云上:在SLB上部署https遇到的问题及解决方法

    一.问题场景 这个问题只会出现在云服务器操作系统使用Windows Server 2012的场景,如果使用的是Windows Server 2008 R2则不存在这个问题. 二.https部署场景 1 ...