ACM (UVa) : 10579


#include <stdio.h>

int main ()
{
	int I, LenB, LenC, X, Carry, N;
	
	while (scanf ("%d", &N)!=EOF) {
	
		if (N==1 || N==2) {
			printf("1\n");
			continue;
		}
		
		int A [1005] = {0}, B [1005] = {0}, C [1005] = {0};
		
		A [0] = B [0] = LenB = 1; // Fibonacci Initialization

		for (I=1; I<N-1; I++){ // Calculate Fibonacci
			LenC = Carry = 0;
			for (X=0; X<LenB; X++){
				Carry = A[X] + B[X] + Carry;
				C[LenC++] = Carry % 10;
				Carry/=10;
			}
			while (Carry!=0){
				C[LenC++] = Carry % 10;
				Carry/=10;
			}

			// Fibonacci Initialization
			for (X=0; X<LenB; X++)
				A[X] = B[X];
			LenB = LenC;
			for (X=0; X<LenC; X++)
				B[X] = C[X]; 
		}

		for (X=LenC-1; X>=0; X--)
			printf("%d", C[X]);
		printf("\n");
	}
	return 0;
}

2 thoughts on “ACM (UVa) : 10579

  1. Try using java. It is very Interesting. 10529 code is as following

    import java.util.*;
    import java.io.*;
    import java.math.*;
    public class Main{
    	public static void main(String[] args){
    	  try{
    			//File file=new File("a.in");
    			//Scanner in=new Scanner(file);
    			int i;
    			Scanner in = new Scanner(System.in);
    			BigInteger[] Fib = new BigInteger[10000];
    			Fib[1] = BigInteger.valueOf(1);
    			Fib[2] = BigInteger.valueOf(1);
    			for( i = 3 ;i<=9998;i++)
    				Fib[i] = Fib[i-1].add(Fib[i-2]);
    				
    			while(in.hasNextInt()){
    		    	int n = in.nextInt();
    		    	System.out.println(Fib[n]);
    		    }
    		}
    		catch(Exception ex){
    			//System.out.println("exception"+ ex.getMessage()+"caught");
    		}		
    	}
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s