Uva-100-The 3N + 1 problem

Source: Internet
Author: User

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 36

 

The 3N+ 1 problem

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 IntegersNSuch that 0 <N<1,000,000 (and, in fact, for more numbers than this .)

Given an inputN, It is possible to determine the number of numbers printed (including the 1). For a givenNThis is calledCycle-lengthOfN. In the example above, the cycle length of 22 is 16.

For any two numbersIAndJYou are to determine the maximum cycle length over all numbersIAndJ.

The input

The input will consist of a series of pairs of IntegersIAndJ, One pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You shoshould process all pairs of integers and for each pair determine the maximum cycle length over all integers between and includingIAndJ.

You can assume that no operation overflows a 32-bit integer.

The output

For each pair of input IntegersIAndJYou shoshould outputI,J, And the maximum cycle length for integers between and includingIAndJ. These three numbers shocould 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 IntegersIAndJMust appear in the output in the same order in which they appeared in the input and shocould 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

Solution: The question means that you can find the maximum number of 3N + 1 computing steps between N and M. As N reaches 100 W, the brute force method will time out, the first thing we think of is the number of steps in Table Preprocessing for each number from 1 to for the 3N + 1 operation, and then directly calculate the number of Max steps between N and M.

About 3N + 1 problem please go to any door-http://en.wikipedia.org/wiki/Collatz_conjecture

 

1 # include <stdio. h>
2
3 # define maxn 1000100
4
5 int A [maxn];
6
7 int fun (long n ){
8 int res = 1;
9 While (1 ){
10 if (n = 1 ){
11 break;
12}
13 if (N % 2 = 0 ){
14 N/= 2;
15}
16 else {
17 N = 3 * n + 1;
18}
19 res ++;
20}
21 return res;
22}
23
24 int main (){
25 int n, m, I, T, ANS, X, Y;
26 For (I = 1; I <maxn; I ++ ){
27 A [I] = fun (long) I );
28}
29 While (scanf ("% d", & N, & M )! = EOF ){
30 x = N, y = m;
31 if (n> m) {T = n, n = m, M = T ;};
32 ans =-1;
33 for (I = N; I <= m; I ++ ){
34 ans = ans> A [I]? Ans: A [I];
35}
36 printf ("% d \ n", X, Y, ANS );
37}
38 return 0;

39}

Uva-100-The 3N + 1 problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.