POJ 1207 The 3n + 1 problem
The 3n + 1 problem
Time Limit: 1000MS | Memory Limit: 10000K |
Description
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs. Consider the following algorithm:
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed before the 1 is printed. For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n <-- 3n+1
5. else n <-- n/2
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed before the 1 is printed. For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 10,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174
【题目背景】
3n+1问题是一个简单有趣而又没有解决的数学问题。这个问题是由L. Collatz在1937年提出的。克拉兹问题(Collatz problem)也被叫做hailstone问题、3n+1问题、Hasse算法问题、Kakutani算法问题、Thwaites猜想或者Ulam问题。
问题如下:
(1)输入一个正整数n;
(2)如果n=1则结束;
(3)如果n是奇数,则n变为3n+1,否则n变为n/2;
(4)转入第(2)步。
克拉兹问题的特殊之处在于:尽管很容易将这个问题讲清楚,但直到今天仍不能保证这个问题的算法对所有可能的输入都有效——即至今没有人证明对所有的正整数该过程都终止。
【题目要求】
对于[i,j]之内的数,输出最大的歩数。
【提示】
1、注意i,j的大小,如果 i>j 则计算的时候要交换一下,输出按原来的顺序
2、如果i,j的值比较大,应该注意溢出
2、如果i,j的值比较大,应该注意溢出
【代码---直接暴力(不考虑提示2)】
1 /*===========================================================================*\ 2 * POJ 1207 The 3n + 1 problem 3 * @author CocoonFan 4 * @date 3/2/13 5 \*===========================================================================*/ 6 #include<iostream> 7 8 using namespace std; 9 10 int steps(int n); 11 12 int main() 13 { 14 int i,j,n,k; 15 int i1,j1; 16 17 while(cin >> i >> j){ 18 int max = 0; 19 i1 = i; 20 j1 = j; 21 if(i>j){ 22 int t = i;i = j;j = t; 23 } 24 25 max = steps(i1); 26 for(k = i+1; k <= j; ++k){ 27 if(max < steps(k)) max = steps(k); 28 } 29 30 cout << i1 << " " << j1 << " " << max << endl; 31 } 32 33 34 return 0; 35 } 36 37 int steps(int n) 38 { 39 int count = 1; 40 while(n != 1){ 41 if(n%2){ //[3(2k+1)+1]/2 = 3k +2 /// ---- k = n/2 42 n = n/2*3 +2; 43 count += 2; 44 } else { 45 n /= 2; 46 count++; 47 } 48 } 49 return count; 50 }
【记忆优化搜索】
1 /*============================================================================*\ 2 * POJ 1207 The 3n + 1 problem 3 * @author 王小迪 4 * @blog http://blog.sina.com.cn/s/blog_ac43e9c301017cgf.html 5 * @date 3/2/13 6 \*============================================================================*/ 7 8 9 #include <iostream> 10 #include <string.h> 11 #include <stdio.h> 12 using namespace std; 13 14 const int maxn = 1000010; 15 int ans[maxn]; 16 17 int dfs(__int64 i){ 18 if(i >= maxn){ 19 if(i%2==0) 20 return dfs(i/2)+1; 21 else 22 return dfs(i*3+1)+1; 23 } 24 else 25 { 26 if(ans[i]!=-1) 27 return ans[i]; 28 if(i%2==0) 29 return ans[i]=dfs(i/2)+1; 30 else 31 return ans[i]=dfs(i*3+1)+1; 32 } 33 } 34 int main() 35 { 36 int n,m; 37 38 while(cin>>n>>m) 39 { 40 bool flag=0; 41 if(n>m) 42 { 43 flag=1; 44 int temp=n; 45 n=m; 46 m=temp; 47 } 48 memset(ans,-1,sizeof(ans));//赋初值 49 ans[1] = 1; 50 int max=0; 51 for(int i = n;i<=m;i++) 52 { 53 if(dfs(i)>max) 54 max=dfs(i); 55 56 } 57 if(flag) 58 printf("%d %d %d\n",m,n,max); 59 else 60 printf("%d %d %d\n",n,m,max); 61 } 62 63 return 0; 64 }