xt 1142 3n+1 注意超32bit,暴力

来源:互联网 发布:indexof 数组 编辑:程序博客网 时间:2024/04/29 02:13

考拉兹猜想,又称为3n1猜想、冰雹猜想、角谷猜想、哈塞猜想、乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1

n = 6,根据上述数式,得出 6→3→10→5→16→8→4→2→1。步骤中最高的数是16,共有8个步骤。现在给定任意整数ab,问对所有a ≤ n ≤ b,一共经过多少步后才能都得到1,其中最高的数是多少。

Input

有多组测试数据。每组测试数据占一行,包含两个正整数1 ≤ a ≤ 1000000a ≤ b ≤ a + 10。输入以EOF结束。

Output

对每组测试数据,输出步数和最高数,用空格隔开。

Sample Input

6 6
11 12
23 33

Sample Output

8 16
23 52
360 9232



///brute force///notice it may exceed 32bit#include <stdio.h>__int64 solve(__int64 a,__int64 &s){    __int64 res = a;    while(a != 1LL)    {        ++s;        if(a&1)            a = a * 3 + 1;        else            a >>= 1;        if(res < a)            res = a;    }    return res;}int main(){    __int64 maxx,step,a,b,i,t,tt;    while(scanf("%I64d%I64d",&a,&b) != EOF)    {        step = maxx = 0LL;        for(i = a; i <= b; ++i)        {            tt = 0;            t = solve(i,tt);            step += tt;            if(maxx < t)                maxx = t;        }        printf("%I64d %I64d\n",step,maxx);    }    return 0;}/**6 611 1223 33*/