結果

問題 No.141 魔法少女コバ
ユーザー jp_ste
提出日時 2015-03-22 13:44:16
言語 Java
(openjdk 23)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 832 bytes
コンパイル時間 3,060 ms
コンパイル使用メモリ 77,016 KB
実行使用メモリ 41,620 KB
最終ジャッジ日時 2024-06-29 00:04:45
合計ジャッジ時間 16,395 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int M = sc.nextInt();
		int N = sc.nextInt();
		
		if(M == N)  { 
			System.out.println(0);
			return;
		}
		
		int div = getCommonDivisor(M, N);
		int m = M/=div;
		int n = N/=div;
		int ans = 0;
	
		while(true) {			
			if(m==1 || n==1) break;
			
			if(m>n) {
				ans += (m/n);
				m %= n; 
			} else {
				int tmp=m;
				m=n;
				n=tmp;
				ans++;
			}
		}
		
		if(m==1) {
			ans++; 
			ans += (n-1);
			
		} else if(n==1) {
			ans += (m-1);
		}
		System.out.println(ans);
	}
	
	static int getCommonDivisor(final int a, final int b) {
		
		int nowA = a;
		int nowB = b;
		
		while(true) {
			int tmp = nowB % nowA;
			if(tmp == 0) break;
			
			nowB = nowA;
			nowA = tmp;
		}
		return nowA;
	}
}
0