Problem Link : http://www.codechef.com/problems/FCTRL2
Solution :
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
class SmallFactorials_LargeMultiplicationUsingArray {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter pw = new PrintWriter(System.out);
public static int readIntLine() throws IOException {
return Integer.parseInt(br.readLine());
}
public static void main(String[] args) throws IOException {
int i = readIntLine();
while (i-- > 0) {
String str = br.readLine();
int fact = Integer.parseInt(str);
ArrayList<Long> a = getIntArrayList(fact);
while (--fact > 0) {
a = multiply(fact, a);
// System.out.println(a);
}
Collections.reverse(a);
for (Long nn : a) {
pw.print(nn);
}
pw.println();
pw.flush();
}
pw.close();
}
public static ArrayList<Long> getIntArrayList(int i) {
ArrayList<Long> templst = new ArrayList<Long>();
while (i > 0) {
templst.add(new Long(i % 10));
i /= 10;
}
return templst;
}
public static ArrayList<Long> multiply(int mul, ArrayList<Long> arr) {
ArrayList<Long> templst = new ArrayList<Long>();
long carry = 0;
for (Long i : arr) {
long temp = mul * i;
temp += carry;
carry = temp / 10;
if (temp >= 10) {
temp %= 10;
}
templst.add(temp);
}
while (carry > 0) {
// System.out.println(carry);
templst.add(carry%10);
carry/=10;
}
return templst;
}
}
No comments:
Post a Comment