您的位置:首页 > 其它

PKU2413 how many fibs

2010-11-17 17:19 225 查看
学以致用,高精度题目一个!先用高精度加法预处理,接着一起二分搜索。

program fibs;
const
maxlen=200;
maxn=500;
stop='0 0';
type
hightype=record
num:array[0..maxlen] of longint;
len:longint;
end;
var
fib:array[0..maxn] of hightype;
limit,a,b:hightype;
tot,left,right:longint;
str,sa,sb:string;
procedure highin(st:string; var x:hightype);
var
i:longint;
begin
x.len:=length(st);
fillchar(x.num,sizeof(x.num),0);
for i:=x.len downto 1 do
x.num[x.len+1-i]:=ord(st[i])-ord('0');
end;
function compare(a,b:hightype):longint;
var
i:longint;
begin
if a.len>b.len then
exit(1);
if a.len<b.len then
exit(-1);
for i:=a.len downto 1 do
if a.num[i]>b.num[i] then
exit(1)
else if a.num[i]<b.num[i] then
exit(-1);
exit(0);
end;
function getmax(a,b:longint):longint;
begin
if a>b then
exit(a);
exit(b);
end;
procedure highplus(a,b:hightype; var c:hightype);
var
i:longint;
begin
c.len:=getmax(a.len,b.len);
fillchar(c.num,sizeof(c.num),0);
for i:=1 to c.len do
c.num[i]:=a.num[i]+b.num[i];
for i:=1 to c.len do
if c.num[i]>=10 then
begin
dec(c.num[i],10);
inc(c.num[i+1]);
end;
while c.num[c.len+1]>0 do
inc(c.len);
end;
procedure prework;
var
i:longint;
begin
limit.len:=101;
fillchar(limit.num,sizeof(limit.num),0);
limit.num[101]:=1;
fib[0].len:=1;
fillchar(fib[0].num,sizeof(fib[0].num),0);
fib[0].num[1]:=0;
fib[1].len:=1;
fillchar(fib[1].num,sizeof(fib[1].num),0);
fib[1].num[1]:=1;
fib[2].len:=1;
fillchar(fib[2].num,sizeof(fib[2].num),0);
fib[2].num[1]:=2;
for i:=3 to maxn do
begin
highplus(fib[i-2],fib[i-1],fib[i]);
if compare(fib[i],limit)>0 then
begin
tot:=i;
exit;
end;
end;
end;
procedure doit(str:string);
var
x:longint;
begin
x:=pos(' ',str);
sa:=copy(str,1,x-1);
sb:=copy(str,x+1,length(str)-x);
end;
function binarysmall(x:hightype):longint;
var
mid,left,right,key:longint;
begin
left:=0;
right:=tot;
while left<right-1 do
begin
mid:=(left+right) shr 1;
key:=compare(fib[mid],x);
if key>=0 then
right:=mid
else
left:=mid;
end;
exit(left);
end;
function binarynobig(x:hightype):longint;
var
mid,left,right,key:longint;
begin
left:=0;
right:=tot;
while left<right-1 do
begin
mid:=(left+right) shr 1;
key:=compare(fib[mid],x);
if key=0 then
exit(mid)
else if key>0 then
right:=mid
else
left:=mid;
end;
exit(left);
end;
procedure outit;
begin
writeln(right-left);
end;
procedure bigmain;
begin
readln(str);
while str<>stop do
begin
doit(str);
highin(sa,a);
highin(sb,b);
left:=binarysmall(a);
right:=binarynobig(b);
outit;
readln(str);
end;
end;
begin
assign(input,'a.in');
reset(input);
assign(output,'a.out');
rewrite(output);
prework;
bigmain;
close(input);
close(output);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: