#include<stdio.h>
#define MX 10000000
#define swap(xxx, yyy) (xxx) ^= (yyy) ^= (xxx) ^= (yyy)
unsigned long DP[MX];
unsigned long tmp,max,maxItem,k,t;
unsigned long cycleLen(unsigned long i,unsigned long n,unsigned long cnt)
{
if(i<MX && DP[i]){ DP[n]=cnt+DP[i];return DP[n]; }
if(i==1) { DP[n]=cnt;return DP[n]; }
if(i%2 == 0) return cycleLen(i/2,n,cnt+1);
return cycleLen(3*i+1,n,cnt+1);
}
unsigned long mxLen(unsigned long i,unsigned long j)
{
if(i>j) swap(i,j);
if(i==1){
max=3+1;
maxItem=1;
}
else{
max=cycleLen(i,i,0)+1;
maxItem=i;
}
for(k=i+1;k<=j;k++){
t=cycleLen(k,k,0)+1;
if(max<t){max=t;maxItem=k;}
}
return max;
}
int main(){
unsigned long a,b,mx;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
while(1)
{
scanf("%lu %lu",&a,&b);
if(a==0 && b==0) break;
mx=mxLen(a,b);
if(a>b){swap(a,b);}
printf("Between %lu and %lu, %lu generates the longest sequence of %lu values.\n",a,b,maxItem,mx-1);
}
return 0;
}
No comments:
Post a Comment