結果

問題 No.1664 Unstable f(n)
ユーザー ks2mks2m
提出日時 2021-09-03 21:34:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 1,032 bytes
コンパイル時間 7,094 ms
コンパイル使用メモリ 77,200 KB
実行使用メモリ 41,912 KB
最終ジャッジ日時 2024-05-09 02:11:34
合計ジャッジ時間 9,191 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
41,544 KB
testcase_01 AC 124 ms
41,496 KB
testcase_02 AC 126 ms
41,496 KB
testcase_03 AC 124 ms
41,768 KB
testcase_04 AC 126 ms
41,876 KB
testcase_05 AC 126 ms
41,592 KB
testcase_06 AC 142 ms
41,728 KB
testcase_07 AC 128 ms
41,488 KB
testcase_08 AC 127 ms
41,732 KB
testcase_09 AC 128 ms
41,332 KB
testcase_10 AC 116 ms
41,204 KB
testcase_11 AC 114 ms
40,268 KB
testcase_12 AC 127 ms
41,464 KB
testcase_13 AC 123 ms
41,796 KB
testcase_14 AC 123 ms
41,456 KB
testcase_15 AC 132 ms
41,312 KB
testcase_16 AC 124 ms
41,348 KB
testcase_17 AC 118 ms
40,204 KB
testcase_18 AC 133 ms
41,484 KB
testcase_19 AC 133 ms
41,712 KB
testcase_20 AC 130 ms
41,756 KB
testcase_21 AC 126 ms
41,192 KB
testcase_22 AC 129 ms
41,912 KB
testcase_23 AC 130 ms
41,172 KB
testcase_24 AC 126 ms
41,560 KB
testcase_25 AC 129 ms
41,312 KB
testcase_26 AC 126 ms
41,456 KB
testcase_27 AC 129 ms
41,800 KB
testcase_28 AC 114 ms
41,480 KB
testcase_29 AC 126 ms
41,672 KB
testcase_30 AC 127 ms
41,576 KB
testcase_31 AC 126 ms
41,304 KB
testcase_32 AC 112 ms
41,752 KB
testcase_33 AC 114 ms
41,764 KB
testcase_34 AC 120 ms
40,508 KB
testcase_35 AC 131 ms
41,616 KB
testcase_36 AC 124 ms
41,684 KB
testcase_37 AC 114 ms
41,708 KB
testcase_38 AC 123 ms
40,252 KB
testcase_39 AC 114 ms
41,400 KB
testcase_40 AC 124 ms
41,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		sc.close();

		System.out.println(solve(n));
	}

	static long solve(long n) {
		long ans = n;
		for (int j = 1; j < 60; j++) {
			int i = root(n, j);
			long k = n - power(i, j);
			if (k < 0) {
				throw new RuntimeException();
			}
			ans = Math.min(ans, i + j + k);
		}
		return ans;
	}

	static int root(long x, int n) {
		int ng = 1000000001;
		int ok = 1;
		while (ng - ok > 1) {
			int mid = (ok + ng) / 2;
			if (Math.pow(mid, n) < Long.MAX_VALUE) {
				ok = mid;
			} else {
				ng = mid;
			}
		}
		ng = ok + 1;
		ok = 1;
		while (ng - ok > 1) {
			int mid = (ok + ng) / 2;
			if (power(mid, n) <= x) {
				ok = mid;
			} else {
				ng = mid;
			}
		}
		return ok;
	}

	static long power(long x, long n) {
		if (n == 0) {
			return 1;
		}
		long val = power(x, n / 2);
		val = val * val;
		if (n % 2 == 1) {
			val = val * x;
		}
		return val;
	}
}
0