結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー tentententen
提出日時 2023-02-01 09:19:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 597 ms / 2,000 ms
コード長 3,284 bytes
コンパイル時間 3,524 ms
コンパイル使用メモリ 100,144 KB
実行使用メモリ 72,792 KB
最終ジャッジ日時 2023-09-13 22:26:57
合計ジャッジ時間 10,100 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,428 KB
testcase_01 AC 45 ms
49,436 KB
testcase_02 AC 47 ms
49,632 KB
testcase_03 AC 47 ms
49,424 KB
testcase_04 AC 45 ms
47,732 KB
testcase_05 AC 47 ms
49,792 KB
testcase_06 AC 46 ms
49,488 KB
testcase_07 AC 47 ms
50,012 KB
testcase_08 AC 47 ms
49,380 KB
testcase_09 AC 47 ms
49,592 KB
testcase_10 AC 315 ms
59,408 KB
testcase_11 AC 304 ms
56,528 KB
testcase_12 AC 593 ms
58,512 KB
testcase_13 AC 247 ms
56,232 KB
testcase_14 AC 316 ms
56,824 KB
testcase_15 AC 248 ms
56,528 KB
testcase_16 AC 357 ms
60,392 KB
testcase_17 AC 233 ms
56,896 KB
testcase_18 AC 597 ms
59,008 KB
testcase_19 AC 337 ms
57,976 KB
testcase_20 AC 595 ms
72,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        boolean isPlus = (sc.next().charAt(0) == '+');
        long ans = 0;
        if (isPlus) {
            HashMap<Integer, Long> countsA = new HashMap<>();
            for (int i = 0; i < m; i++) {
                int x = sc.nextInt() % k;
                countsA.put(x, countsA.getOrDefault(x, 0L) + 1);
            }
            HashMap<Integer, Long> countsB = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int x = sc.nextInt() % k;
                countsB.put(x, countsB.getOrDefault(x, 0L) + 1);
            }
            for (int x : countsA.keySet()) {
                if (x == 0) {
                    ans += countsA.get(x) * countsB.getOrDefault(x, 0L);
                } else {
                    ans += countsA.get(x) * countsB.getOrDefault(k - x, 0L);
                }
            }
        } else {
            HashMap<Long, Long> countsA = new HashMap<>();
            for (int i = 0; i < m; i++) {
                long gcd = getGCD(sc.nextInt(), k);
                countsA.put(gcd, countsA.getOrDefault(gcd, 0L) + 1);
            }
            HashMap<Long, Long> countsB = new HashMap<>();
            for (int i = 0; i < n; i++) {
                long gcd = getGCD(sc.nextInt(), k);
                countsB.put(gcd, countsB.getOrDefault(gcd, 0L) + 1);
            }
            for (long x : countsA.keySet()) {
                for (long y : countsB.keySet()) {
                    if (x * y % k == 0) {
                        ans += countsA.get(x) * countsB.get(y);
                    }
                }
            }
        }
        System.out.println(ans);
    }
    
    static long getGCD(long x, long y) {
        if (y == 0) {
            return x;
        } else {
            return getGCD(y, x % y);
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0