結果

問題 No.575 n! / m / m / m...
ユーザー 37zigen37zigen
提出日時 2017-10-07 05:52:15
言語 Java19
(openjdk 21)
結果
AC  
実行時間 187 ms / 2,000 ms
コード長 1,608 bytes
コンパイル時間 5,115 ms
コンパイル使用メモリ 76,860 KB
実行使用メモリ 59,272 KB
最終ジャッジ日時 2023-08-10 18:57:39
合計ジャッジ時間 8,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
57,320 KB
testcase_01 AC 177 ms
56,988 KB
testcase_02 AC 174 ms
57,076 KB
testcase_03 AC 169 ms
57,096 KB
testcase_04 AC 174 ms
57,412 KB
testcase_05 AC 172 ms
57,044 KB
testcase_06 AC 160 ms
57,196 KB
testcase_07 AC 161 ms
54,612 KB
testcase_08 AC 161 ms
56,960 KB
testcase_09 AC 159 ms
57,188 KB
testcase_10 AC 163 ms
57,180 KB
testcase_11 AC 175 ms
57,180 KB
testcase_12 AC 176 ms
57,384 KB
testcase_13 AC 174 ms
59,272 KB
testcase_14 AC 181 ms
55,236 KB
testcase_15 AC 163 ms
56,988 KB
testcase_16 AC 162 ms
56,940 KB
testcase_17 AC 168 ms
56,840 KB
testcase_18 AC 176 ms
57,000 KB
testcase_19 AC 163 ms
57,100 KB
testcase_20 AC 172 ms
57,104 KB
testcase_21 AC 164 ms
57,648 KB
testcase_22 AC 162 ms
57,176 KB
testcase_23 AC 187 ms
57,240 KB
testcase_24 AC 164 ms
57,496 KB
testcase_25 AC 184 ms
57,136 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)
				+ Math.log10(1 + 1 / (12 * n) + 1 / (288 * n * n) - 139 / (51840 * n * n * 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