結果

問題 No.2326 Factorial to the Power of Factorial to the...
ユーザー Ackvy
提出日時 2023-05-28 14:09:03
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 770 bytes
コンパイル時間 5,455 ms
コンパイル使用メモリ 83,808 KB
実行使用メモリ 41,464 KB
最終ジャッジ日時 2024-12-26 23:42:44
合計ジャッジ時間 8,932 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 11 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main{
	static final int MOD = 1000000007;

	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int P = sc.nextInt();

		if(N < P){
			System.out.println(0);
			return;
		}

		Long numP = 0l;
		for(int i = 1; i <= N; i++){
			int n = i;
			while(n % P == 0){
				numP++;
				n /= P;
			}
		}

		long nM = 1;
		for(int i = 1; i <= N; i++){
			nM *= i;
			nM %= MOD;
		}

		char[] S = Long.toBinaryString(nM).toCharArray();
		long binM = nM;
		long ans = 1;

		for(int i = S.length-1; i >= 0; i--){
//			System.out.print(S[i]);
			if(S[i] == '1'){
				ans *= binM;
				ans %= MOD;
			}
			binM *= binM;
			binM %= MOD;
		}

		ans *= numP;
		ans %= MOD;

		System.out.println(ans);
	}
}
0