結果

問題 No.982 Add
ユーザー silviasetitechsilviasetitech
提出日時 2020-03-05 11:14:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,710 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 74,000 KB
実行使用メモリ 56,764 KB
最終ジャッジ日時 2023-08-04 03:48:59
合計ジャッジ時間 6,914 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,020 KB
testcase_01 AC 123 ms
56,104 KB
testcase_02 AC 127 ms
56,748 KB
testcase_03 AC 122 ms
55,932 KB
testcase_04 AC 122 ms
55,780 KB
testcase_05 AC 134 ms
55,928 KB
testcase_06 AC 129 ms
55,852 KB
testcase_07 AC 129 ms
55,936 KB
testcase_08 AC 127 ms
56,024 KB
testcase_09 AC 162 ms
56,216 KB
testcase_10 AC 147 ms
56,264 KB
testcase_11 AC 129 ms
56,132 KB
testcase_12 AC 122 ms
56,360 KB
testcase_13 AC 123 ms
56,520 KB
testcase_14 AC 124 ms
56,064 KB
testcase_15 AC 129 ms
56,108 KB
testcase_16 AC 135 ms
55,804 KB
testcase_17 AC 128 ms
56,088 KB
testcase_18 AC 123 ms
56,464 KB
testcase_19 AC 122 ms
55,664 KB
testcase_20 AC 124 ms
56,708 KB
testcase_21 AC 123 ms
56,508 KB
testcase_22 AC 124 ms
55,436 KB
testcase_23 AC 124 ms
56,764 KB
testcase_24 AC 181 ms
54,604 KB
testcase_25 AC 182 ms
55,896 KB
権限があれば一括ダウンロードができます

ソースコード

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