結果

問題 No.109 N! mod M
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-15 16:45:58
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,544 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 75,016 KB
実行使用メモリ 56,840 KB
最終ジャッジ日時 2023-08-25 17:16:33
合計ジャッジ時間 3,915 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
49,744 KB
testcase_01 AC 104 ms
52,300 KB
testcase_02 AC 105 ms
52,448 KB
testcase_03 AC 50 ms
49,816 KB
testcase_04 AC 73 ms
51,132 KB
testcase_05 AC 306 ms
56,840 KB
testcase_06 AC 125 ms
53,992 KB
testcase_07 WA -
testcase_08 AC 51 ms
49,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.math.BigInteger;


class Main {
    static long powerMod(long x, long exponent, long MOD) {
	long prod = 1;
	for (int i = 63; i >= 0; --i) {
	    prod = (prod * prod) % MOD;
	    if ((exponent & 1L << i) != 0) {
		prod = (prod * x) % MOD;
	    }
	}
	return prod;
    }
    static long f(long n,long m){
        if(n>=m)return 0;
        if(m==4||2*m>=3*n){
            // brute
            long ans=1;
            for(long i=1;i<=n;++i)ans=ans*i%m;
            return ans;
        }
        boolean pr=BigInteger.valueOf(m).isProbablePrime(20);
        if(pr){
            // Wilson's theorem
            long ans=m-1;
            for(long i=m-1;i>n;--i){
                ans=ans*i%m;
            }
            return powerMod(ans,m-2,m);
        }
        return 0;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int t=sc.nextInt();
        while(t-->0){
            long n=sc.nextLong();
            long m=sc.nextLong();
            out.println(f(n,m));
        }
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
0