The 3n + 1 problem
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 53448 |
|
Accepted: 16999 |
Description
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 .
Consider the following algorithm:
1. input n2. Print N3. if n = 1 then STOP4. If n is odd then n <--3n+15. else n <--n/26. 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 integers n such 0 < n < 1,000,000 (and, in fact, for many more numbers T Han this.)
Given an input n, it's possible to determine the number of numbers printed before the 1 is printed. For a given n 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.
Input
The input would consist of a series of pairs of integers I and J, one pair of integers per line. All integers is is less than 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 including I and J.
Output
For all pair of input integers I and j should output I, j, and the maximum cycle length for integers between and incl Uding 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 OUTP UT for each line of input. The integers I and J must appear in the output of the same order in which they appeared in the input and should is Followe D 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
water problem, small data, preprocessing, and then directly find the answer output. Pay attention to the handling of a>b.
#include <stdio.h> #include <string.h> #include <math.h> #define EPS 1e-9#define Max (a) (a) > (b)? (a):(b) int res[10010];int Vis[10010];int main () {int i,j,k,n,a,b,cnt,t,max;for (i=1;i<=10000;i++) {N=i;cnt=1;while (n!=1) {if (n&1) n=n*3+1;elsen/=2;cnt++;} res[i]=cnt;} while (scanf ("%d%d", &a,&b)!=eof) {printf ("%d%d", A, B), max=-1;if (a>b) t=a,a=b,b=t;for (i=a;i<=b;i++) if (Max<res[i]) max=res[i];p rintf ("%d\n", Max);} return 0;}
POJ 1207 the 3n + 1 problem (math)