Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=36
http://acm.hdu.edu.cn/showproblem.php?pid=1032
http://poj.org/problem?id=1207
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:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then
5. else
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 nsuch 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 (including the 1). 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.
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 1,000,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 can assume that no operation overflows a 32-bit integer.
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).
1 10 100 200 201 210 900 1000
1 10 20 100 200 125 201 210 89 900 1000 174
介绍:
这题说的是奇偶归一猜想(英语:Collatz conjecture),又称为3n+1猜想、冰雹猜想、角谷猜想、哈塞猜想、乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1。
参考《数论中未解决的问题》P275:(一种“可视化”表示)
另:论文"The 3x+1 problem and its generalizations"[Lag85]中提到,就连大名鼎鼎的匈牙利天才数学家保罗·厄多斯也说道:“Mathematics is not yet ready for such problems.”(数学还没准备好应付这类问题)
更多细节见《数论概论》P20
注意:
1. 由于题目中说“You can assume that no operation overflows a 32-bit integer.”所以用int可过这题。
2. i可能大于j,一开始读题的时候就yy会不会出给这么变态的数据,没想到真的这样。。
3. 使用一个数组保存算过的数据,可减少很多运行时间。
复杂度:Unknown
完整代码:
/*UVa: 0.045s*/
/*HDU: 0ms,2184KB*/
/*POJ: 16ms,2160KB*/
#include
using namespace std;
const int maxs = 1000000;
int cache[maxs];///记录已经算过的值
/// 对更大的maxs可能由于递归过深导致出现栈溢出的情况
int counter(int num)
{
if (num == 1) return 1;
if (num & 1) num += (num << 1) + 1;
else num >>= 1;
if (num < maxs)
{
if (!cache[num]) cache[num] = counter(num);
return 1 + cache[num];
}
return 1 + counter(num);
}
int main()
{
int a, b, mx, i;
while (~scanf("%d%d", &a, &b))
{
printf("%d %d ", a, b);
if (a > b) swap(a, b);
mx = 1;
for (i = a; i <= b; ++i) mx = max(mx, counter(i));
printf("%d\n", mx);
}
return 0;
}