結果

問題 No.141 魔法少女コバ
ユーザー YamaKasa
提出日時 2018-07-17 19:15:48
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 951 bytes
コンパイル時間 1,976 ms
コンパイル使用メモリ 76,616 KB
実行使用メモリ 41,592 KB
最終ジャッジ日時 2024-11-26 07:09:54
合計ジャッジ時間 14,637 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 WA * 89
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int M = scan.nextInt();
		int min = Math.min(N, M);
		int max = Math.max(N, M);
		scan.close();
		if(N == M) {
			System.out.println(1);
			System.exit(0);
		}
		// m <= n
		// ユークリッドの互除法
		int n = max;
		int m = min;
		int r = -1;
	    while (r != 0) {
	        r = n % m;
	        n = m;
	        m = r;
	    }
	    int a = N / n;
	    int b = M / n;
	    // M / N = a / b
	    int cnt = 0;
	    int d1, n1;
	    if(a > b) {
	    	d1 = b;
	    	n1 = a;
	    }else {
	    	d1 = a;
	    	n1 = b;
	    }

	    if(n1 == 1) {
	    	cnt += d1;
	    	System.out.println(cnt);
	    	System.exit(0);
	    }
	    int d2, n2;
	    while(n1 != 1) {
	    	cnt += n1 / d1;
	    	d2 = n1 % d1;
	    	n2 = d1;
	    	n1 = n2;
	    	d1 = d2;
	    }
	    System.out.println(cnt + 1);


	}
}
0