結果

問題 No.415 ぴょん
ユーザー 37zigen37zigen
提出日時 2016-08-24 17:20:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 1,000 ms
コード長 838 bytes
コンパイル時間 3,732 ms
コンパイル使用メモリ 79,664 KB
実行使用メモリ 41,708 KB
最終ジャッジ日時 2024-04-29 11:29:03
合計ジャッジ時間 7,478 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
41,112 KB
testcase_01 AC 128 ms
41,028 KB
testcase_02 AC 109 ms
41,520 KB
testcase_03 AC 109 ms
40,112 KB
testcase_04 AC 126 ms
41,392 KB
testcase_05 AC 123 ms
41,540 KB
testcase_06 AC 126 ms
41,328 KB
testcase_07 AC 110 ms
41,256 KB
testcase_08 AC 122 ms
40,108 KB
testcase_09 AC 119 ms
41,352 KB
testcase_10 AC 126 ms
41,448 KB
testcase_11 AC 123 ms
41,104 KB
testcase_12 AC 122 ms
41,472 KB
testcase_13 AC 123 ms
41,280 KB
testcase_14 AC 127 ms
41,336 KB
testcase_15 AC 134 ms
41,348 KB
testcase_16 AC 125 ms
41,128 KB
testcase_17 AC 112 ms
41,556 KB
testcase_18 AC 123 ms
41,708 KB
testcase_19 AC 110 ms
41,224 KB
testcase_20 AC 119 ms
41,084 KB
testcase_21 AC 114 ms
41,300 KB
testcase_22 AC 109 ms
41,148 KB
testcase_23 AC 120 ms
41,376 KB
testcase_24 AC 112 ms
41,552 KB
testcase_25 AC 113 ms
41,248 KB
testcase_26 AC 123 ms
41,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package No400番台;

import java.util.Scanner;

public class Q415 {
	public static void main(String[] args) {
		new Q415().solver();
	}

	void solver() {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long D = sc.nextLong();
		System.out.println(N / Math.abs(gcd(D, N)) - 1);
	}

	static long gcd(long t1, long t2) {
		if (Math.abs(t1) < Math.abs(t2)) {
			long d = t2;
			t2 = t1;
			t1 = d;
		}
		if (t2 == 0)
			return t1;

		return gcd(t2, remainder(t1, t2));
	}

	static long remainder(long a, long b) {

		long r = a % b;
		if (Math.abs(r) > Math.abs(b / 2)) {
			if (r > 0 && b > 0) {
				r -= b;
			} else if (r > 0 && b < 0) {
				r += b;
			} else if (r < 0 && b > 0) {
				r += b;
			} else if (r < 0 && b < 0) {
				r -= b;
			} else {
				throw new AssertionError(a + " " + b);
			}
		}
		return r;
	}
}
0