結果

問題 No.1593 Perfect Distance
ユーザー ks2mks2m
提出日時 2021-07-09 21:23:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 2,030 ms
コンパイル使用メモリ 74,596 KB
実行使用メモリ 57,904 KB
最終ジャッジ日時 2023-09-14 07:30:12
合計ジャッジ時間 5,964 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,452 KB
testcase_01 AC 124 ms
56,028 KB
testcase_02 AC 123 ms
55,540 KB
testcase_03 AC 125 ms
55,812 KB
testcase_04 AC 126 ms
56,032 KB
testcase_05 AC 125 ms
55,520 KB
testcase_06 AC 126 ms
55,560 KB
testcase_07 AC 129 ms
55,516 KB
testcase_08 AC 135 ms
55,888 KB
testcase_09 AC 138 ms
56,500 KB
testcase_10 AC 147 ms
55,636 KB
testcase_11 AC 155 ms
55,796 KB
testcase_12 AC 171 ms
55,992 KB
testcase_13 AC 172 ms
55,816 KB
testcase_14 AC 176 ms
57,904 KB
testcase_15 AC 194 ms
55,940 KB
testcase_16 AC 221 ms
55,964 KB
testcase_17 AC 128 ms
53,896 KB
testcase_18 AC 126 ms
55,748 KB
testcase_19 AC 124 ms
56,124 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