結果

問題 No.2326 Factorial to the Power of Factorial to the...
ユーザー AckvyAckvy
提出日時 2023-05-28 15:34:18
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,256 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 74,348 KB
実行使用メモリ 58,096 KB
最終ジャッジ日時 2023-08-27 12:26:16
合計ジャッジ時間 6,179 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,648 KB
testcase_01 WA -
testcase_02 AC 122 ms
55,848 KB
testcase_03 AC 122 ms
54,256 KB
testcase_04 WA -
testcase_05 AC 123 ms
55,764 KB
testcase_06 AC 120 ms
55,432 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 121 ms
55,432 KB
testcase_11 AC 121 ms
56,156 KB
testcase_12 AC 121 ms
56,140 KB
testcase_13 WA -
testcase_14 AC 120 ms
55,804 KB
testcase_15 WA -
testcase_16 AC 120 ms
55,784 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 126 ms
56,032 KB
testcase_20 AC 121 ms
55,788 KB
testcase_21 AC 121 ms
55,984 KB
権限があれば一括ダウンロードができます

ソースコード

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();

		int primes[] = new int[1000];
		int primeNum = 1;
		primes[0] = 2;
		for(int i = 3; i < 1000; i++){
			boolean isPrime = true;
			for(int j = 0; j < primeNum; j++){
				if(i % primes[j] == 0){
					isPrime = false;
					break;
				}
			}
			if(isPrime){
				primes[primeNum] = i;
				primeNum++;
			}
		}

		int dum = P;
		int dividePrime[] = new int[primeNum+1];
		int productPrime[] = new int[primeNum+1];
		for(int i = 0; i < primeNum; i++){
			dividePrime[i] = 0;
			productPrime[i] = 1;
			while(dum % primes[i] == 0){
				dividePrime[i]++;
				productPrime[i] *= primes[i];
				dum /= primes[i];
			}
		}
		if(dum != 1){
			primes[primeNum] = dum;
			dividePrime[primeNum] = 1;
			productPrime[primeNum] = dum;
			primeNum++;
		}

		int maxProdInd = 0;
		for(int i = 0; i < primeNum; i++){
			if(productPrime[maxProdInd] < productPrime[i]){
				maxProdInd = i;
			}
		}

		int maxPrime = primes[maxProdInd];
		int divide = dividePrime[maxProdInd];
//		System.out.println("["+maxPrime+","+divide+"]");

		if(maxPrime > N){
			System.out.println(0);
			return;
		}


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

		long amariP = numP % divide;
		numP /= divide;
//		System.out.println("["+numP+","+amariP+"]");


		long nM = 1;
		long amariNM = 1;
		for(int i = 1; i <= N; i++){
			if(i != divide){
				amariNM *= i;
			}
			nM *= i;
			nM %= MOD;
			amariNM %= MOD;
		}
//		System.out.println(nM);

		char[] S = Long.toBinaryString(nM-1).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;
		}
//		System.out.println();
		long amariAns = amariP * amariNM;
		amariAns %= MOD;
		amariAns *= ans;
		amariAns %= MOD;
//		System.out.println(amariAns);

		ans *= numP;
		ans %= MOD;
		ans *= nM;
		ans %= MOD;
//		System.out.println(ans);
		ans += amariAns;
		ans %= MOD;

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