結果

問題 No.1593 Perfect Distance
ユーザー ks2m
提出日時 2021-07-09 21:23:37
言語 Java
(openjdk 23)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 967 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 77,176 KB
実行使用メモリ 41,596 KB
最終ジャッジ日時 2024-07-01 14:57:06
合計ジャッジ時間 5,926 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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