简单,不解释,一开始TLE,加个哈希就AC: { Author:wzx961008 Problem:UVa 100-The 3n+1 problem Verdict:Accepted Language:PASCAL Run Time:0.044s Submission Date:2011-01-11 12:06:03 } const maxnum=1000000; var i,l,r,maxT,tmp:longint; hash:array[1..maxnum]of longint; function find(n:qword):longint; begin if n>maxnum then if n and 1=1 then exit(find(3*n+1)+1) else exit(find(n shr 1)+1); if hash[n]=0 then if n and 1=1 then hash[n]:=find(3*n+1)+1 else hash[n]:=find(n shr 1)+1; find:=hash[n]; end; function max(a,b:longint):longint; begin if a>b then exit(a) else exit(b); end; procedure doit(l,r:longint); begin hash[1]:=1; for i:=l to r do begin tmp:=find(i); if tmp>maxT then maxT:=tmp; end; end; begin while not eof do begin readln(l,r); maxT:=0; if l>r then doit(r,l) else doit(l,r); writeln(l,' ',r,' ',maxT); end; end.