1 solutions

  • 0
    @ 2025-11-5 17:55:43

    C++ :

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int N = 505;
    int a[N],b[N],c[N];
    
    void add(int a[],int b[],int c[]){
    	fill(c,c+N,0);
    	c[0]=b[0];
    	for (int i=1; i<=c[0]; i++){
    		c[i]+=a[i]+b[i];
    		c[i+1]+=c[i]/10;
    		c[i]%=10;
    	}
    	if (c[c[0]+1]) c[0]++;
    }
    
    void fibo(int n){
    	if (n<=2) { c[0]=1; c[1]=n; return; }
    	a[0]=a[1]=b[0]=1; b[1]=2;
    	for (int i=3; i<=n; i++){
    		add(a,b,c);
    		memcpy(a,b,sizeof(b));
    		memcpy(b,c,sizeof(c));
    	}
    }
    
    void output(int a[]){
    	for (int i=a[0]; i>=1; i--) cout<<a[i];
    	cout<<endl;
    }
    
    int main(){
    	int m,n;
    	cin>>m>>n;
    	fibo(n-m);
    	output(c);
    	return 0;
    }
    

    Pascal :

    const maxn=10000;
    type sz=array[1..maxn] of longint;
    var a,b,c:sz;
        i,j,m,n,lena,lenb,l:longint;
    procedure add(var a,b,c:sz);
    var i:longint;
    begin
      if lena>lenb then l:=lena else l:=lenb;
      for i:=1 to l do c[i]:=a[i]+b[i];
      for i:=1 to l do
        if c[i]>=10then
         begin
            c[i]:=c[i]-10;
            c[i+1]:=c[i+1]+1;
          end;
      if c[l+1]>0 then l:=l+1;
      a:=b;lena:=lenb;b:=c;lenb:=l;
    end;
      
    begin
      readln(m,n);
      if m>n then exit
        else if n-m=1 then writeln(1)
          else
          begin
              a[1]:=1;b[1]:=1;lena:=1;lenb:=1;l:=0;
              for i:=3 to n-m+1 do
                add(a,b,c);
              for i:=l downto 1 do write(c[i]);
          end;
    end.
    
    • 1

    Information

    ID
    18648
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By