输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 %)          
        例如:n = 10, P = 11,10! = 3628800    
          3628800 % 11 = 10    
         Input   
    两个数N,P,中间用空格隔开。(N < 10000, P < 10^9)
     Output   
    输出N! mod P的结果。
     Input示例   
    10 11
     Output示例   
    10
思路:这道题要注意精度问题,其次我还发现了对每次结果取模(多次取模),跟最后一次结果(一次取模)的结果是一样的,但是多次取模明显数会
变小许多。
code: ac
 import java.util.Scanner;
 import java.io.*;
 public class Main{
     public static void main(String[] args){
         Scanner s = new Scanner(System.in);
         PrintWriter out = new PrintWriter(System.out);
         long N = s.nextLong();
         long p = s.nextLong();
         long sum =1;
         for(long i=1; i<=N; ++i){
             sum*=i;
             sum%=p;
         }
         out.println(sum);
         out.flush();
     }
 }
 
 

 京公网安备 11010502036488号
京公网安备 11010502036488号