POJ2248 Addition Chains
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2248 Addition Chains相关的知识,希望对你有一定的参考价值。
Addition Chains
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 5273 | Accepted: 2833 | Special Judge |
Description
An addition chain for n is an integer sequence with the following four properties:
You are given an integer n. Your job is to construct an addition chain for n with minimal length. If there is more than one such sequence, any one is acceptable.
For example, <1,2,3,5> and <1,2,4,5> are both valid solutions when you are asked for an addition chain for 5.
- a0 = 1
- am = n
- a0 < a1 < a2 < ... < am-1 < am
- For each k (1<=k<=m) there exist two (not necessarily different) integers i and j (0<=i, j<=k-1) with ak=ai+aj
You are given an integer n. Your job is to construct an addition chain for n with minimal length. If there is more than one such sequence, any one is acceptable.
For example, <1,2,3,5> and <1,2,4,5> are both valid solutions when you are asked for an addition chain for 5.
Input
The
input will contain one or more test cases. Each test case consists of
one line containing one integer n (1<=n<=100). Input is terminated
by a value of zero (0) for n.
Output
For each test case, print one line containing the required integer sequence. Separate the numbers by one blank.
Hint: The problem is a little time-critical, so use proper break conditions where necessary to reduce the search space.
Hint: The problem is a little time-critical, so use proper break conditions where necessary to reduce the search space.
Sample Input
5 7 12 15 77 0
Sample Output
1 2 4 5 1 2 4 6 7 1 2 4 8 12 1 2 4 5 10 15 1 2 4 8 9 17 34 68 77
Source
【题解】
搜索层数较少,使用ID-DFS
剪枝1:ID升序枚举
剪枝2:数列降序枚举
剪枝3:保证数列递增
剪枝4:step > ID跳出
然而无论怎么剪枝,都比不上打表
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #define min(a, b) ((a) < (b) ? (a) : (b)) 6 #define max(a, b) ((a) > (b) ? (a) : (b)) 7 8 inline void read(int &x) 9 { 10 x = 0;char ch = getchar(), c = ch; 11 while(ch < ‘0‘|| ch > ‘9‘)c = ch, ch = getchar(); 12 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 13 if(c == ‘-‘)x = -x; 14 } 15 16 const int MAXN = 100 + 10; 17 18 int num[MAXN], n, b[MAXN], ID; 19 20 int dfs(int step) 21 { 22 if(num[step - 1] == n) return 1; 23 if(step > ID)return 0; 24 register int tmp; 25 for(register int i = step - 1;i >= 1;-- i) 26 for(register int j = i;j >= 1;-- j) 27 { 28 tmp = num[i] + num[j]; 29 if(tmp <= num[step - 1] || b[tmp] || tmp > n)continue; 30 num[step] = tmp; 31 b[tmp] = 1; 32 if(dfs(step + 1)) return 1; 33 b[tmp] = 0; 34 } 35 return 0; 36 } 37 38 int main() 39 { 40 num[1] = 1; 41 while(scanf("%d", &n) != EOF && n) 42 { 43 for(register int i = 1;i <= n;++ i) 44 { 45 ID = i; 46 memset(b, 0, sizeof(b)); 47 if(dfs(2)) 48 { 49 for(register int j = 1;j <= i;++ j) 50 printf("%d ", num[j]); 51 putchar(‘\n‘); 52 break; 53 } 54 } 55 } 56 return 0; 57 }
以上是关于POJ2248 Addition Chains的主要内容,如果未能解决你的问题,请参考以下文章
poj2248 Addition Chains 迭代加深搜索
POJ 2248 Addition Chains dfs(水)