結果

問題 No.1593 Perfect Distance
ユーザー ks2mks2m
提出日時 2021-07-09 21:23:37
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 1,700 ms
使用メモリ 38,404 KB
最終ジャッジ日時 2023-02-01 21:47:04
合計ジャッジ時間 5,352 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 92 ms
37,416 KB
testcase_01 AC 97 ms
37,468 KB
testcase_02 AC 96 ms
37,344 KB
testcase_03 AC 91 ms
37,340 KB
testcase_04 AC 96 ms
37,768 KB
testcase_05 AC 95 ms
37,760 KB
testcase_06 AC 93 ms
37,668 KB
testcase_07 AC 96 ms
37,620 KB
testcase_08 AC 99 ms
37,712 KB
testcase_09 AC 116 ms
38,292 KB
testcase_10 AC 125 ms
38,052 KB
testcase_11 AC 130 ms
38,072 KB
testcase_12 AC 145 ms
38,032 KB
testcase_13 AC 151 ms
38,404 KB
testcase_14 AC 153 ms
38,156 KB
testcase_15 AC 174 ms
38,012 KB
testcase_16 AC 190 ms
37,992 KB
testcase_17 AC 92 ms
37,372 KB
testcase_18 AC 93 ms
37,836 KB
testcase_19 AC 101 ms
37,628 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();
		sc.close();

		long n2 = (long) n * n;
		int ans = 0;
		for (long x = 1; x < n; x++) {
			long x2 = x * x;
			long y2 = n2 - x2;
			long y = root(y2, 2);
			if (y * y == y2) {
				ans++;
			}
		}
		System.out.println(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