結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー htensai
提出日時 2020-02-21 15:26:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,210 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 79,720 KB
実行使用メモリ 72,296 KB
最終ジャッジ日時 2024-11-16 03:20:24
合計ジャッジ時間 14,322 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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