small factorial

factorial
small
tle

#1

import java.util.*;

import java.io.*;  

import java.math.BigInteger;


class test {
public static void main(String [] args)
{

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
 
 try{

BigInteger one = BigInteger.ONE;
ArrayList<BigInteger> arr = new ArrayList<BigInteger>();

String str =br.readLine();     
String s= str;

int a = Integer.parseInt(s);          

int lin = a;


while(a>0)
{
String str1 = br.readLine();
String s1 = str1;

int num=Integer.parseInt(s1);       


for (int p=1; p<=num; p++) {
one = one.multiply(BigInteger.valueOf(p));
}
arr.add(one);       
a--;
}
for(int j=0;j<lin;j++)
{
System.out.println(arr.get(j));     
}
}
 catch(IOException ioe)
 {
     ioe.printStackTrace();
 }
}
} 

could not get why i am getting time limit exceede in this code ??? plz hlp


#2

@zargus : You are solving each test case seperately .

You should precalculate all factorials till 100 and simply retrieve the required factorial when asked as a test case . That will reduce the running time 100 times .


#3

thanks fr da hint i solved it da way u said :wink: