結果

問題 No.1218 Something Like a Theorem
ユーザー ks2mks2m
提出日時 2020-09-04 21:54:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,019 bytes
コンパイル時間 2,049 ms
コンパイル使用メモリ 77,684 KB
実行使用メモリ 41,888 KB
最終ジャッジ日時 2024-05-04 22:13:24
合計ジャッジ時間 4,783 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
41,888 KB
testcase_01 AC 113 ms
41,596 KB
testcase_02 AC 115 ms
41,444 KB
testcase_03 AC 100 ms
40,264 KB
testcase_04 AC 111 ms
41,652 KB
testcase_05 AC 107 ms
41,092 KB
testcase_06 AC 114 ms
41,368 KB
testcase_07 AC 102 ms
41,684 KB
testcase_08 AC 123 ms
41,816 KB
testcase_09 AC 115 ms
41,696 KB
testcase_10 AC 108 ms
41,136 KB
testcase_11 AC 114 ms
41,764 KB
testcase_12 AC 106 ms
41,644 KB
testcase_13 AC 117 ms
41,420 KB
testcase_14 AC 102 ms
41,460 KB
testcase_15 AC 109 ms
41,548 KB
testcase_16 AC 103 ms
41,588 KB
testcase_17 AC 109 ms
41,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

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

		long zn = power(z, n);
		for (int x = 1; x < z; x++) {
			long xn = power(x, n);
			long yn = zn - xn;
			long y = root(yn, n);
			if (power(y, n) == yn) {
				System.out.println("Yes");
				return;
			}
		}
		System.out.println("No");
	}

	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;
	}

	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;
	}
}
0