DFS——poj2034Anti-prime Sequences

一、题目回顾

题目链接:Anti-prime Sequences

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.

一个由连续的整数(n,n+1,n+2,…,m)组成的序列,an anti-prime sequence就是这些整数的重新排列,使每一对相邻的整数与一个复合(非素数)数相加。例如,如果n = 1和m = 10,一个这样的anti-prime sequence就是1 3 5 4 2 6 9 7 8 10。 This is also the lexicographically first such sequence.

我们可以通过定义一个danti - prime sequence来扩展这个定义,它的长度为2,3,…,d和一个复合数。上面的序列是2次反质数序列,而不是3次,因为子序列5、4、2和11。字母顺序进行的第3次反质数序列为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 2

1 10 3

1 10 5

40 60 7

0 0 0

Sample Output

1,3,5,4,2,6,9,7,8,10

1,3,5,4,6,2,10,8,7,9

No 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

题意:求n到m的一个排列,满足任意连续的k(2<=k<=d)个数的和都不为素数。

二、解题思路

时间: 2024-10-13 15:21:42

DFS——poj2034Anti-prime Sequences的相关文章

BNU 25593 Prime Time 记忆化dp

题目链接:点击打开链接 题意: 一个游戏由3个人轮流玩 每局游戏由其中一名玩家选择一个数字作为开始 目的:获得最小的得分 对于当前玩家 O ,面对 u 这个数字 则他的操作有: 1. 计分 u +1 ,然后 u++; 2.计分 u / x, 然后 u /= x; 其中x为u的因子且x为素数 然后下一个玩家继续上述操作 3个人各操作一次 为1轮 当一轮结束后,若u==1 则游戏结束 每个人的得分为 他所有计分记录里最小的数字 若在一轮结束前 u就==1, 那么玩家的得分为本局游戏的初始数 求: 每

uva 11762 - Race to 1(马尔可夫)

题目链接:uva 11762 - Race to 1 题目大意:给出一个整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/P,否则N不变.问平均情况下需要多少次选择,才能把N变成1. 解题思路:马尔可夫,例如N=6时,f(6)=1+f(6)?13+f(4)?13+f(2)?13,1是只第一次转移,后面分别对应的是选择5,2,3的情况.所以有f(x)=∑f(x/y)+p(x)g(x),p(x)为不超过x的素数个数,g(x)为是x因子的素数个数. #include <

【搜索】【约数个数定理】[HAOI2007]反素数ant

对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数. 所以,n以内的反质数即为不超过n的约数个数最多的数. 怎样计算约数个数? 约数个数定理:对于一个大于1正整数n可以分解质因数:n=p1^a1*p2^a2*p3^a3*…*pk^ak,则n的正约数的个数就是(a1+1)(a2+1)(a3+1)…(ak+1) .其中a1.a2.a3…ak是p1.p2.p3,…pk的指数. 所以,只需枚举一个数的所

UVA 11762 - Race to 1(概率)

UVA 11762 - Race to 1 题意:给定一个n,每次随即选择一个n以内的质数,如果不是质因子,就保持不变,如果是的话,就把n除掉该因子,问n变成1的次数的期望值 思路:tot为总的质数,cnt为质因子个数,那么f(n)=(1?cnt/tot)?f(n)+∑f(n/prime)?(1/tot),然后利用记忆化搜索去做即可 代码: #include <stdio.h> #include <string.h> const int N = 1000005; int t, n,

HDU 6053 TrickGCD 容斥

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意: 给你序列a,让你构造序列b,要求 1<=b[i]<=a[i],且b序列的gcd>=2.问你方案数. 思路: 容易想到的就是我们枚举整个序列的gcd,然后a[i]/gcd就是i位置能够填的数的个数,然后每个位置累积就能得到数列为gcd时的方案数. 最后容斥一下累加就是答案.但是最大gcd可以是100000和明显这样做n^2,会超时. 那么我们把a[i]/gcd的放在一起,然后用

【数学】【CF27E】 Number With The Given Amount Of Divisors

传送门 Description 给定一个正整数\(n\),输出最小的整数,满足这个整数有n个因子 Input 一行一个整数\(n\) Output 一行一个整数,代表答案. Hint \(1~\leq~n~\leq~1000\).保证答案不超过\(10^{18}\) Solution 经典题. 引理: 对于一个唯一分解式形如\(x=p_1^{c_1}p_2^{c_2}p_3^{c^3}\cdots p_k^{c_k}\)的数字\(x\),则其因数个数为\(\prod(c_i+1)\). 证明:

Philips and Calculator

代码 #include<cstdio> #include<algorithm> using namespace std; const int N = 3 * 1e6; int l , r , p , tp , prime[105] , num[N + 5] , n , vis[105] , f[N + 5] , ok[N + 5] , ans; inline void getp() { vis[1] = vis[0] = 1; for(register int i = 2; i &

HDU 1016 Prime Ring Problem(DFS)

题目链接 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

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

Prime Ring Problem hdu-1016 DFS

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. Inputn (0 < n < 2