結果

問題 No.415 ぴょん
ユーザー 37zigen37zigen
提出日時 2016-08-24 17:18:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 141 ms / 1,000 ms
コード長 838 bytes
コンパイル時間 3,870 ms
コンパイル使用メモリ 84,604 KB
実行使用メモリ 41,652 KB
最終ジャッジ日時 2024-04-29 11:28:40
合計ジャッジ時間 8,566 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,316 KB
testcase_01 AC 134 ms
41,476 KB
testcase_02 AC 137 ms
41,616 KB
testcase_03 AC 135 ms
41,368 KB
testcase_04 AC 135 ms
41,124 KB
testcase_05 AC 125 ms
40,156 KB
testcase_06 AC 138 ms
41,236 KB
testcase_07 AC 135 ms
41,616 KB
testcase_08 AC 139 ms
41,496 KB
testcase_09 AC 139 ms
41,236 KB
testcase_10 AC 135 ms
41,212 KB
testcase_11 AC 132 ms
41,456 KB
testcase_12 AC 137 ms
41,340 KB
testcase_13 AC 139 ms
41,336 KB
testcase_14 AC 138 ms
41,272 KB
testcase_15 AC 139 ms
41,244 KB
testcase_16 AC 136 ms
41,412 KB
testcase_17 AC 141 ms
41,448 KB
testcase_18 AC 133 ms
41,052 KB
testcase_19 AC 141 ms
41,652 KB
testcase_20 AC 136 ms
41,136 KB
testcase_21 AC 136 ms
41,284 KB
testcase_22 AC 139 ms
41,180 KB
testcase_23 AC 137 ms
41,260 KB
testcase_24 AC 137 ms
41,316 KB
testcase_25 AC 121 ms
39,976 KB
testcase_26 AC 121 ms
39,844 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