资源限制
内存限制:256.0MB C/C++时间限制:1.0s Java时间限制:3.0s Python时间限制:5.0s
问题描述
给定n和p,输出n!对p取余的结果。
输入格式
一行两个数n和p。
输出格式
一行一个数表示结果。
样例输入
6 10000
样例输出
720
数据规模和约定
n<=10000000,p<=10000。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
BigInteger n=scanner.nextBigInteger();
BigInteger p=scanner.nextBigInteger();
BigInteger num=new BigInteger("1");
BigInteger x=new BigInteger("1");
while(n.intValue()>0) {
num=num.multiply(n);
n=n.subtract(x);
}
System.out.println(num.mod(p));
}
}