結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー htensaihtensai
提出日時 2020-02-21 15:26:22
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,071 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 2,344 ms
コンパイル使用メモリ 76,128 KB
実行使用メモリ 71,196 KB
最終ジャッジ日時 2023-08-10 03:57:18
合計ジャッジ時間 13,321 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,688 KB
testcase_01 AC 119 ms
55,744 KB
testcase_02 AC 132 ms
56,132 KB
testcase_03 AC 134 ms
55,724 KB
testcase_04 AC 128 ms
55,744 KB
testcase_05 AC 135 ms
56,104 KB
testcase_06 AC 131 ms
55,960 KB
testcase_07 AC 137 ms
55,964 KB
testcase_08 AC 130 ms
55,992 KB
testcase_09 AC 130 ms
55,492 KB
testcase_10 AC 579 ms
63,224 KB
testcase_11 AC 644 ms
61,856 KB
testcase_12 AC 1,047 ms
64,992 KB
testcase_13 AC 521 ms
60,992 KB
testcase_14 AC 655 ms
64,744 KB
testcase_15 AC 557 ms
61,712 KB
testcase_16 AC 639 ms
64,280 KB
testcase_17 AC 507 ms
60,988 KB
testcase_18 AC 1,071 ms
63,004 KB
testcase_19 AC 659 ms
62,580 KB
testcase_20 AC 980 ms
71,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        char op = sc.next().charAt(0);
        if (op == '+') {
            HashMap<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < m; i ++ ){
                int mod = sc.nextInt() % k;
                if (map.containsKey(mod)) {
                    map.put(mod, map.get(mod) + 1);
                } else {
                    map.put(mod, 1);
                }
            }
            long count = 0;
            for (int i = 0; i < n; i++) {
                int mod = sc.nextInt() % k;
                if (map.containsKey((k - mod) % k)) {
                    count += map.get((k - mod) % k);
                }
            }
            System.out.println(count);
        } else {
            HashMap<Integer, Integer> mapB = new HashMap<>();
            for (int i = 0; i < m; i ++ ){
                int gd = gcd(sc.nextInt(), k);
                if (mapB.containsKey(gd)) {
                    mapB.put(gd, mapB.get(gd) + 1);
                } else {
                    mapB.put(gd, 1);
                }
            }
            HashMap<Integer, Integer> mapA = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int gd = gcd(sc.nextInt(), k);
                if (mapA.containsKey(gd)) {
                    mapA.put(gd, mapA.get(gd) + 1);
                } else {
                    mapA.put(gd, 1);
                }
            }
            long count = 0;
            for (Map.Entry<Integer, Integer> entryA : mapA.entrySet()) {
                for (Map.Entry<Integer, Integer> entryB : mapB.entrySet()) {
                    if ((long)entryA.getKey() * entryB.getKey() % k == 0) {
                        count += (long)entryA.getValue() * entryB.getValue();
                    }
                }
            }
            System.out.println(count);
        }
    }
    
    static int gcd(int x, int y) {
        if (x % y == 0) {
            return y;
        } else {
            return gcd(y, x % y);
        }
    }
}
0