您的位置:首页 > 其它

UVa OJ 100-The 3n+1 Problem

2011-01-13 15:02 507 查看
简单,不解释,一开始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
=0 then
if n and 1=1 then hash
:=find(3*n+1)+1
else hash
:=find(n shr 1)+1;
find:=hash
;
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: