早教吧作业答案频道 -->其他-->
求斐波那契数列log(n) pascal算法程序如题,注意是Log(n)
题目详情
求斐波那契数列log(n) pascal算法程序
如题,注意是Log(n)
如题,注意是Log(n)
▼优质解答
答案和解析
用矩阵加速
[ f(n+1) ] [1 1] [ f(n) ]
=
[ f(n) ] [1 0] [ f(n-1)]
不停的迭代就行了
递归求解,log(n)的
program fibonacci;
type
matrix=array[1..2,1..2] of qword;
var
c,cc:matrix;
n:integer;
function multiply(x,y:matrix):matrix;
var
temp:matrix;
begin
temp[1,1]:=x[1,1]*y[1,1]+x[1,2]*y[2,1];
temp[1,2]:=x[1,1]*y[1,2]+x[1,2]*y[2,2];
temp[2,1]:=x[2,1]*y[1,1]+x[2,2]*y[2,1];
temp[2,2]:=x[2,1]*y[1,2]+x[2,2]*y[2,2];
exit(temp);
end;
function getcc(n:integer):matrix;
var
temp:matrix;
t:integer;
begin
if n=1 then exit(c);
t:=n div 2;
temp:=getcc(t);
temp:=multiply(temp,temp);
if odd(n) then exit(multiply(temp,c))
else exit(temp);
end;
procedure init;
begin
readln(n);
c[1,1]:=1;
c[1,2]:=1;
c[2,1]:=1;
c[2,2]:=0;
if n=1 then
begin
writeln(1);
halt;
end;
if n=2 then
begin
writeln(1);
halt;
end;
cc:=getcc(n-2);
end;
procedure work;
begin
writeln(cc[1,1]+cc[1,2]);
end;
begin
init;
work;
end.
[ f(n+1) ] [1 1] [ f(n) ]
=
[ f(n) ] [1 0] [ f(n-1)]
不停的迭代就行了
递归求解,log(n)的
program fibonacci;
type
matrix=array[1..2,1..2] of qword;
var
c,cc:matrix;
n:integer;
function multiply(x,y:matrix):matrix;
var
temp:matrix;
begin
temp[1,1]:=x[1,1]*y[1,1]+x[1,2]*y[2,1];
temp[1,2]:=x[1,1]*y[1,2]+x[1,2]*y[2,2];
temp[2,1]:=x[2,1]*y[1,1]+x[2,2]*y[2,1];
temp[2,2]:=x[2,1]*y[1,2]+x[2,2]*y[2,2];
exit(temp);
end;
function getcc(n:integer):matrix;
var
temp:matrix;
t:integer;
begin
if n=1 then exit(c);
t:=n div 2;
temp:=getcc(t);
temp:=multiply(temp,temp);
if odd(n) then exit(multiply(temp,c))
else exit(temp);
end;
procedure init;
begin
readln(n);
c[1,1]:=1;
c[1,2]:=1;
c[2,1]:=1;
c[2,2]:=0;
if n=1 then
begin
writeln(1);
halt;
end;
if n=2 then
begin
writeln(1);
halt;
end;
cc:=getcc(n-2);
end;
procedure work;
begin
writeln(cc[1,1]+cc[1,2]);
end;
begin
init;
work;
end.
看了 求斐波那契数列log(n) ...的网友还看了以下:
计算1.求级数∑∞(x-1)^n/n的收敛域与和函数.2.试将函数f(x)=arcsinx/x展成 2020-04-12 …
为什么人会有喜怒哀乐?如果没有,那么就不用因为一些小事儿而伤心了为什么人会有喜怒哀乐?如果没有,那 2020-05-13 …
设f(x)是定义在对称区间(-L,L)内的任何函数,证明……设f(x)是定义在对称区间(-L,L) 2020-05-16 …
写出下列算法的功能LinkListdemo(LinkListL){ListNode*q,*p;If 2020-05-17 …
已知椭圆x^2/4+y^2/3=1的左右焦点为F1,F2,左右顶点为A1,A2,p是椭圆上一点,直 2020-06-05 …
(1)已知圆的方程为x的平方+y的平方-2x+2y=0,求圆的周长.(2)已知直...(1)已知圆 2020-06-08 …
设函数f(x)是定义在(-L,L)内的奇函数(L〉0),证明若f(x)在(-L,0)内单调增加,则 2020-06-09 …
求助L-谷氨酸为什么用乙醇溶不了?前面我到网上搜了许多论文,想找一下用什么溶解L-谷氨酸,结果文献 2020-06-27 …
定义点P(x0,y0)到直线l:Ax+By+C=0(A2+B2≠0)的有向距离为d=Ax0+By0 2020-07-09 …
用L,l,m表示k某种弹簧秤原来的长度为l,悬挂重物后的长度L可用公式L=l+k分之m表示某种弹簧秤 2020-12-05 …