結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー tentententen
提出日時 2022-08-30 11:50:53
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 4,247 bytes
コンパイル時間 2,780 ms
コンパイル使用メモリ 79,824 KB
実行使用メモリ 77,368 KB
最終ジャッジ日時 2024-04-24 21:59:27
合計ジャッジ時間 8,307 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
42,280 KB
testcase_01 AC 54 ms
36,860 KB
testcase_02 AC 57 ms
37,052 KB
testcase_03 AC 55 ms
36,972 KB
testcase_04 AC 55 ms
36,840 KB
testcase_05 AC 55 ms
36,960 KB
testcase_06 AC 54 ms
36,836 KB
testcase_07 AC 57 ms
37,096 KB
testcase_08 AC 54 ms
37,004 KB
testcase_09 AC 53 ms
36,820 KB
testcase_10 AC 326 ms
49,632 KB
testcase_11 AC 361 ms
46,584 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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();
        if (sc.next().charAt(0) == '+') {
            HashMap<Integer, Long> bCounts = new HashMap<>();
            for (int i = 0; i < m; i++) {
                int x = sc.nextInt() % k;
                bCounts.put(x, bCounts.getOrDefault(x, 0L) + 1);
            }
            HashMap<Integer, Long> aCounts = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int x = sc.nextInt() % k;
                aCounts.put(x, aCounts.getOrDefault(x, 0L) + 1);
            }
            long ans = 0;
            for (int x : bCounts.keySet()) {
                if (x == 0) {
                    ans += aCounts.getOrDefault(x, 0L) * bCounts.get(x);
                } else {
                    ans += aCounts.getOrDefault(k - x, 0L) * bCounts.get(x);
                }
            }
            System.out.println(ans);
        } else {
            HashMap<Integer, Integer> base = new HashMap<>();
            int kk = k;
            for (int i = 2; i <= Math.sqrt(kk); i++) {
                while (kk % i == 0) {
                    base.put(i, base.getOrDefault(i, 0) + 1);
                    kk /= i;
                }
            }
            if (kk > 1) {
                base.put(kk, base.getOrDefault(kk, 0) + 1);
            }
            Count.base = base;
            HashMap<Count, Long> bCounts = new HashMap<>();
            for (int i = 0; i < m; i++) {
                Count x = new Count(sc.nextInt());
                bCounts.put(x, bCounts.getOrDefault(x, 0L) + 1);
            }
            HashMap<Count, Long> aCounts = new HashMap<>();
            for (int i = 0; i < n; i++) {
                Count x = new Count(sc.nextInt());
                aCounts.put(x, aCounts.getOrDefault(x, 0L) + 1);
            }
            long ans = 0;
            for (Count x : bCounts.keySet()) {
                for (Count y : aCounts.keySet()) {
                    if (x.hasK(y)) {
                        ans += bCounts.get(x) * aCounts.get(y);
                    }
                }
            }
            System.out.println(ans);
        }
    }
    
    static class Count {
        static HashMap<Integer, Integer> base;
        HashMap<Integer, Integer> counts = new HashMap<>();
        
        public Count(int x) {
            for (int y : base.keySet()) {
                int c = 0;
                while (x % y == 0) {
                    c++;
                    x /= y;
                }
                counts.put(y, Math.min(base.get(y), c));
            }
        }
        
        public boolean hasK(Count another) {
            for (int x : base.keySet()) {
                if (counts.get(x) + another.counts.get(x) < base.get(x)) {
                    return false;
                }
            }
            return true;
        }
        
        public int hashCode() {
            int sum = 0;
            for (int x : counts.values()) {
                sum += x;
            }
            return sum;
        }
        
        public boolean equals(Object o) {
            Count c = (Count)o;
            for (int x : base.keySet()) {
                if (counts.get(x) != c.counts.get(x)) {
                    return false;
                }
            }
            return true;
        }
    }
}
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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0