結果

問題 No.982 Add
ユーザー tenten
提出日時 2021-08-06 15:56:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,454 bytes
コンパイル時間 2,331 ms
コンパイル使用メモリ 77,528 KB
実行使用メモリ 50,536 KB
最終ジャッジ日時 2024-09-17 00:30:06
合計ジャッジ時間 4,960 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int a = sc.nextInt();
        int b = sc.nextInt();
        if (getGCD(a, b) > 1) {
            System.out.println(-1);
            return;
        }
        boolean[] enables = new boolean[a * b * 2 + 1];
        for (int i = 0; i <= b; i++) {
            for (int j = 0; j <= a; j++) {
                enables[i * a + j * b] = true;
            }
        }
        int count = 0;
        for (int i = 0; i < a * b; i++) {
            if (!enables[i]) {
                count++;
            }
        }
        System.out.println(count);
    }
    
    static int getGCD(int x, int y) {
        if (x % y == 0) {
            return y;
        } else {
            return getGCD(y, x % y);
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0