算法提高 阶乘模(蓝桥杯)

算法提高 阶乘模(蓝桥杯),第1张

资源限制

内存限制: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));
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/919804.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存