結果

問題 No.982 Add
ユーザー silviasetitechsilviasetitech
提出日時 2020-03-05 11:14:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,710 bytes
コンパイル時間 1,951 ms
コンパイル使用メモリ 77,384 KB
実行使用メモリ 54,484 KB
最終ジャッジ日時 2024-04-22 02:12:56
合計ジャッジ時間 6,160 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
54,184 KB
testcase_01 AC 118 ms
54,008 KB
testcase_02 AC 111 ms
53,168 KB
testcase_03 AC 115 ms
54,164 KB
testcase_04 AC 112 ms
52,984 KB
testcase_05 AC 128 ms
54,316 KB
testcase_06 AC 121 ms
54,236 KB
testcase_07 AC 125 ms
54,264 KB
testcase_08 AC 127 ms
54,048 KB
testcase_09 AC 139 ms
53,324 KB
testcase_10 AC 130 ms
53,244 KB
testcase_11 AC 120 ms
52,848 KB
testcase_12 AC 128 ms
54,156 KB
testcase_13 AC 111 ms
53,004 KB
testcase_14 AC 113 ms
52,888 KB
testcase_15 AC 134 ms
54,216 KB
testcase_16 AC 139 ms
54,084 KB
testcase_17 AC 135 ms
54,216 KB
testcase_18 AC 125 ms
54,116 KB
testcase_19 AC 131 ms
54,156 KB
testcase_20 AC 108 ms
53,060 KB
testcase_21 AC 102 ms
52,820 KB
testcase_22 AC 123 ms
54,484 KB
testcase_23 AC 117 ms
54,152 KB
testcase_24 AC 161 ms
53,260 KB
testcase_25 AC 157 ms
52,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