Skip to content

Instantly share code, notes, and snippets.

@ilya-murzinov
Created May 16, 2014 19:18
Show Gist options
  • Save ilya-murzinov/4e56152f12c3e84291c6 to your computer and use it in GitHub Desktop.
Save ilya-murzinov/4e56152f12c3e84291c6 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.math.BigInteger;
public class Main {
public static String inName = "input.txt";
public static String outName = "output.txt";
public static void main(String[] args) {
try {
String line;
String result;
InputStream inputStream = new FileInputStream(inName);
InputStreamReader fileReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(fileReader);
line = bufferedReader.readLine();
int number = Integer.parseInt(line.split(" ")[0]);
int power = Integer.parseInt(line.split(" ")[1]);
BigInteger sum = BigInteger.valueOf(1);
for (int i = 1; i <= number; i++) {
int fact = 1;
for (int j = 1; j <= i; j++) {
fact = fact*j;
}
BigInteger pow = BigInteger.valueOf(1);
for (int j = 0; j < power; j++) {
pow = pow.multiply(BigInteger.valueOf(fact));
}
sum = sum.add(pow);
}
result = getLastNonZero(String.valueOf(sum));
System.out.println(sum);
System.out.println(result);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outName)));
writer.write(result);
writer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
public static String getLastNonZero(String in) {
for (int i = in.length() - 1; i >= 0; i--) {
if (in.charAt(i) != '0') {
return String.valueOf(in.charAt(i));
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment