結果

問題 No.575 n! / m / m / m...
ユーザー 37zigen37zigen
提出日時 2017-10-07 05:55:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 1,529 bytes
コンパイル時間 2,652 ms
コンパイル使用メモリ 83,520 KB
実行使用メモリ 43,040 KB
最終ジャッジ日時 2024-04-28 12:20:07
合計ジャッジ時間 8,699 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 180 ms
42,732 KB
testcase_01 AC 182 ms
42,940 KB
testcase_02 AC 182 ms
42,896 KB
testcase_03 AC 180 ms
42,652 KB
testcase_04 AC 187 ms
42,720 KB
testcase_05 AC 182 ms
42,780 KB
testcase_06 AC 185 ms
42,524 KB
testcase_07 AC 185 ms
42,892 KB
testcase_08 AC 180 ms
42,668 KB
testcase_09 AC 179 ms
42,444 KB
testcase_10 AC 170 ms
42,764 KB
testcase_11 AC 181 ms
42,928 KB
testcase_12 AC 186 ms
42,796 KB
testcase_13 AC 185 ms
42,724 KB
testcase_14 AC 181 ms
43,016 KB
testcase_15 AC 194 ms
43,040 KB
testcase_16 AC 167 ms
42,656 KB
testcase_17 AC 179 ms
42,812 KB
testcase_18 AC 177 ms
42,584 KB
testcase_19 AC 179 ms
42,944 KB
testcase_20 AC 186 ms
42,888 KB
testcase_21 AC 179 ms
42,740 KB
testcase_22 AC 181 ms
42,696 KB
testcase_23 AC 198 ms
42,544 KB
testcase_24 AC 178 ms
42,948 KB
testcase_25 AC 205 ms
42,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		double n = sc.nextDouble();
		double m = sc.nextDouble();
		long c = Long.MAX_VALUE;
		ArrayList<long[]> lis = flis((long) (m + 1e-9));
		for (long[] f : lis) {
			long tmp = 0;
			long n_ = (long) (n + 1e-9);
			while (n_ > 1) {
				tmp += n_ / f[0];
				n_ /= f[0];
			}
			c = Math.min(c, tmp / f[1]);
		}
		if (n < 170) {
			double ret = 1;
			for (int i = 2; i <= n; ++i) {
				ret *= i;
			}
			while (ret % m == 0)
				ret /= m;
			int cnt = 0;
			while (ret >= 10) {
				++cnt;
				ret /= 10;
			}
			System.out.println(ret + "e" + cnt);
			return;
		}
		++n;
		double v = Math.log10(Math.sqrt(2 * Math.PI)) + n * Math.log10(n / Math.E) - 0.5 * Math.log10(n)
				  - c * Math.log10(m);
		System.out.println(Math.pow(10, v - (long) v) + "e" + (long) v);
	}

	ArrayList<long[]> flis(long n) {
		ArrayList<long[]> ret = new ArrayList<>();
		for (long i = 2; i * i <= n; ++i) {
			if (n % i == 0) {
				int c = 0;
				while (n % i == 0) {
					n /= i;
					++c;
				}
				ret.add(new long[] { i, c });
			}
		}
		if (n > 1)
			ret.add(new long[] { n, 1 });
		return ret;
	}

	long pow(long a, long n) {
		long ret = 1;
		for (; n > 0; n >>= 1, a *= a) {
			if (n % 2 == 1) {
				ret *= a;
			}
		}
		return ret;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0