UVa 100 / HDU 1032 / POJ 1207 The 3n + 1 problem (数论&Collatz序列周期)

100 - The 3n + 1 problem

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

Background

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.

The Problem

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

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.

The Output

For each pair of input integers i and j you should output ij, 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


介绍:

这题说的是奇偶归一猜想英语Collatz conjecture),又称为3n+1猜想冰雹猜想角谷猜想哈塞猜想乌拉姆猜想叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1。

f(n) = \begin{cases} n/2 &\mbox{if } n \equiv 0 \\ 3n+1 & \mbox{if } n\equiv 1 \end{cases} \pmod{2}.
参考《数论中未解决的问题》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;
}

你可能感兴趣的:(acm之路--数学,数论,UVa,HDU,POJ,acm,C++,UVa,poj,hdu)