Uva-the 3n + 1 problem interpretation

Source: Internet
Author: User

This problem and calculates the prime number of the phase interval. Thinking in unison.

Note the problem:

1 I may be greater than or equal to J--upload here. Read test instructions carefully, I did not say this place is not guaranteed. Special handling Required

2 The number of integers that may overflow during the calculation is greater than the maximum value, a long long must be used

About efficiency and time issues:

1 is able to use an array to save intermediate results, so execution is fast. The memory consumption is large,

2 can not use intermediate arrays. It's certainly slower to execute. But the memory consumption is small, the same can be AC.

There's no standard for all of them. It depends on the situation. Suppose you need to add an array of speed, assuming that the save memory does not use the array bar.

This kind of topic usually uses the array bar.

I use arrays here.

Blog: http://tausiq.wordpress.com/2008/12/09/uva-100-the-3n-1-problem/

Topics such as the following:

Background

Problems in computer science is often classified as belonging to a certain class of problems (e.g., NP, unsolvable, recur sive). In this problem you'll be analyzing a property of an algorithm whose classification are not known for all possible inputs .

The problem

Consider the following algorithm:

1.  N

2. n

3. N = 1 Then STOP

4. n is odd then

5. Else

6. GOTO 2

Given the input, the following sequence of numbers would 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 would terminate (when a 1 was 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 integersn such 0 < n < 1,000,000 (and, in fact, FO R many more numbers than this.)

Given an input n, it's possible to determine the number of numbers printed (including the 1). For a givenN called the cycle-length of n. The example above, the cycle length of is 16.

For any of the numbers I and J you is to determine the maximum cycle length through all numbers between i and J.

The Input

The input would consist of a series of pairs of integers i and J, one pair of integers per line. All integers'll is 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 to all integers between an D includingI and J.

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

The Output

For each pair of input integers i and J should output i, J, and the maximum cycle length for integers between and includingI 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 OUTP UT for each line of input. The integersi and J must appear in the output of the same order in which they appeared in the input and Should is followed by the maximum cycle length (on the same line).

Sample Input

1 10100 200201) 210900 1000

Sample Output

1 10 20100 200 125201 210 89900 1000 174

Note that the above problem is better than AC.

My following program is to write a class, separate multiple functions, can see the logic is very clear. Of course/2 and * * can be changed into >>1 and <<1 operation.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < queue> #include <vector> #include <stdio.h>using namespace std;static int table[1000000] = {0};//={-1} Not working properly. can only clear 0 class threenone{public:const static int max_n = 1000000; Threenone () {table[1] = 1;for (int i = 2; i < 1000000; i*=2) {table[i] = Table[i/2] + 1;} INITTBL (table);} int checktbl (int tbl[], long long i)//i be sure to overflow for longlong,int {if (i < max_n && 0! = tbl[i]) return tbl[i];if (i% 2) {if (I < max_n) Tbl[i] = Checktbl (TBL, I * 3 + 1) + 1;else return checktbl (TBL, I * 3 + 1) + 1;} else {if (I < max_n) Tbl[i] = Checktbl (TBL, I/2) + 1;else return checktbl (TBL, I/2) + 1;} return tbl[i];} void inittbl (int tbl[]) {for (int i = 3; i < 1000000; i++) {checktbl (TBL, i);}} void The3n1problem () {int i = 0, j = 0;while (cin>>i>>j) {pair<int, int> t = Minmax (i, j);//interval given is not necessarily i<= J, fine examining int ans = 0;for (long long d = t.first; d <= T.second;d++) {ans = max (ans, table[d]);//error: ans + = table[d]; Read the questions carefully, not with. But maximum}cout<<i<< ' <<j<< ' <<ans<<endl;}}};

int main () {Threenone tno;tno. The3n1problem (); return 0;}




Copyright notice: The author Heart Jing. King Space Address: http://blog.csdn.net/kenden23/. may not be reproduced without the consent of the author.

Uva-the 3n + 1 problem interpretation

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.