結果

問題 No.982 Add
ユーザー silviasetitech
提出日時 2020-03-05 11:14:06
言語 Java
(openjdk 23)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 1,710 bytes
コンパイル時間 2,290 ms
コンパイル使用メモリ 77,156 KB
実行使用メモリ 41,616 KB
最終ジャッジ日時 2024-10-14 00:53:50
合計ジャッジ時間 6,812 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author silviase
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        add solver = new add();
        solver.solve(1, in, out);
        out.close();
    }

    static class add {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int a = in.nextInt();
            int b = in.nextInt();
            int res = 0;
            if (Arith.gcd(a, b) != 1) {
                if (a == 1 || b == 1) {
                    out.println(0);
                    return;
                } else {
                    out.println(-1);
                    return;
                }
            }

            for (int i = 1; i < a * b; i++) {
                if (!canMake(a, b, i)) {
                    res++;
                }
            }
            out.println(res);

        }

        private boolean canMake(int a, int b, int key) {
            for (int i = 0; i <= b; i++) {
                for (int j = 0; j <= a; j++) {
                    if (a * i + b * j == key) {
                        return true;
                    }
                }
            }
            return false;
        }

    }

    static class Arith {
        public static long gcd(long a, long b) {
            return a % b == 0 ? b : gcd(b, a % b);
        }

    }
}

0