結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー TatsuyaaaaTatsuyaaaa
提出日時 2020-02-14 23:53:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,001 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 80,668 KB
実行使用メモリ 67,716 KB
最終ジャッジ日時 2024-04-27 20:38:23
合計ジャッジ時間 7,900 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
50,248 KB
testcase_01 AC 45 ms
50,360 KB
testcase_02 AC 46 ms
50,256 KB
testcase_03 AC 46 ms
50,436 KB
testcase_04 AC 46 ms
50,544 KB
testcase_05 AC 47 ms
50,156 KB
testcase_06 AC 45 ms
50,248 KB
testcase_07 AC 48 ms
50,448 KB
testcase_08 AC 48 ms
50,472 KB
testcase_09 AC 47 ms
50,464 KB
testcase_10 AC 308 ms
61,244 KB
testcase_11 AC 317 ms
61,040 KB
testcase_12 AC 695 ms
67,716 KB
testcase_13 AC 263 ms
48,928 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 393 ms
51,980 KB
testcase_17 WA -
testcase_18 AC 707 ms
57,408 KB
testcase_19 AC 425 ms
62,032 KB
testcase_20 AC 605 ms
66,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

import java.util.*;

public class Main {

    static List<Integer> divisor(int n) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i=1;i<=Math.sqrt(n);i++) {
            if (n%i==0) {
                list.add(i);
                if ((long)i*i!=n) {
                    list.add(n/i);
                }
            }
        }
        Collections.sort(list);
        return list;
    }

    static long gcd(long a,long b) {
        long temp;
        while ((temp=a%b)!=0) {
            a=b;
            b=temp;
        }
        return b;
    }

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        int N = in.nextInt();
        int M = in.nextInt();
        int K = in.nextInt();
        char op = in.next().charAt(0);
        long[] B = new long[M];
        long[] A = new long[N];
        // long B_sum = 0L;
        // long A_sum = 0L;
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        HashMap<Integer, Integer> mul_map = new HashMap<Integer, Integer>();
        for (int i=0;i<M;i++) {
            B[i] = in.nextLong();
            int remainder = (int)B[i]%K;
            if (!map.containsKey(remainder)) {
                map.put(remainder, 1);
            } else {
                map.put(remainder, map.get(remainder)+1);
            }

            int val = K/(int)gcd(K, B[i]);
            if (!mul_map.containsKey(val)) {
                mul_map.put(val, 1);
            } else {
                mul_map.put(val, mul_map.get(val)+1);
            }
            // B_sum += B[i];
        }

        for (int i=0;i<N;i++) {
            A[i] = in.nextLong();
        }

        HashMap<Integer, Integer> mul_map_A = new HashMap<Integer, Integer>();
        for (int i=0;i<N;i++) {
            int val = K/(int)gcd(K, A[i]);
            if (!mul_map_A.containsKey(val)) {
                mul_map_A.put(val, 1);
            } else {
                mul_map_A.put(val, mul_map_A.get(val)+1);
            }
        }

        long ans_plus = 0L;
        long ans_mul = 0L;
        ArrayList<Integer> keys = new ArrayList<Integer>(mul_map.keySet());
        ArrayList<Integer> vals = new ArrayList<Integer>(mul_map.values());


        ArrayList<Integer> keys_A = new ArrayList<Integer>(mul_map_A.keySet());
        ArrayList<Integer> vals_A = new ArrayList<Integer>(mul_map_A.values());
        long ans_final = 0;
        for (int i=0;i<keys.size();i++) {
            for (int j=0;j<keys_A.size();j++) {
                if (K%((long)keys_A.get(j)*keys.get(i))==0) {
                    ans_final += (long)vals_A.get(j)*vals.get(i);
                }
            }
        }

        ArrayList<Integer> diviser = new ArrayList<Integer>(divisor(K));
        // out.println(diviser);
        HashMap<Integer, Long> map_map = new HashMap<Integer, Long>();
        for (int i=0;i<diviser.size();i++) {
            for (int j=0;j<keys.size();j++) {
                if (diviser.get(i)%keys.get(j)==0) {
                    if (!map_map.containsKey(diviser.get(i))) {
                        map_map.put(diviser.get(i), (long)vals.get(j));
                    } else {
                        map_map.put(diviser.get(i), (Long)map_map.get(diviser.get(i))+(Integer)vals.get(j));
                    }
                }
            }
        }
        // out.println(map_map);

        for (int i=0;i<N;i++) {
            // A_sum += A[i];
            int val = K-(int)A[i]%K;
            if (map.containsKey(val)) {
                ans_plus += map.get(val);
            }

            // if (A[i]%K==0) ans_mul+=M;
            // else {
                // int mul_tmp = 0;
                // for (int j=0;j<keys.size();j++) {
                //     if (A[i]%keys.get(j)==0) mul_tmp+=vals.get(j);
                // }
                // ans_mul+=mul_tmp;


                int val_2 = (int)gcd(K, A[i]);
                // out.println("val_2: "+val_2);
                if (map_map.containsKey(val_2)) {
                    ans_mul += map_map.get(val_2);
                }
            // }
        }

        // out.println("K: "+K);
        // out.println(mul_map);
        if (op=='+') out.println(ans_plus);
        // if (op=='*') out.println(ans_mul);
        if (op=='*') out.println(ans_final);
        // out.println(map);
        // Arrays.sort(B);

        // long ans = 0L;
        // for (int i=0;i<N;i++) {
        //     long tmp = 0;
        //     if (op=='+') tmp = K-A[i];
        //     if (op=='*') tmp = (K+A[i]-1)/A[i];
        //     // ans += binary_search(tmp, B, op);
        //     int index = ~Arrays.binarySearch(B, tmp, (o1, o2)->o1>=o2?1:-1);
        //     ans += M-index;
        // }
        // out.println(ans);

        out.close();
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}
0