結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー betrue12betrue12
提出日時 2020-02-14 21:51:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 980 bytes
コンパイル時間 2,361 ms
コンパイル使用メモリ 210,924 KB
実行使用メモリ 12,968 KB
最終ジャッジ日時 2023-08-10 01:50:10
合計ジャッジ時間 4,458 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 52 ms
6,064 KB
testcase_11 AC 38 ms
4,380 KB
testcase_12 AC 117 ms
4,380 KB
testcase_13 AC 26 ms
4,380 KB
testcase_14 AC 55 ms
4,384 KB
testcase_15 AC 31 ms
4,380 KB
testcase_16 AC 67 ms
6,856 KB
testcase_17 AC 25 ms
4,384 KB
testcase_18 AC 117 ms
4,380 KB
testcase_19 AC 49 ms
4,380 KB
testcase_20 AC 213 ms
12,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b){
    return b==0 ? a : gcd(b, a%b);
}

int main(){
    int N, M, K;
    char op;
    cin >> N >> M >> K >> op;
    vector<int> A(N), B(M);
    for(int i=0; i<M; i++) cin >> B[i];
    for(int i=0; i<N; i++) cin >> A[i];

    int64_t ans = 0;
    if(op == '+'){
        map<int, int> num;
        for(int b : B) num[b%K]++;
        for(int a : A) ans += num[(K-a%K)%K];
    }else{
        map<int, int> mp;
        for(int i=1; i*i <= K; i++) if(K%i == 0){
            mp[i] = mp[K/i] = 0;
        }
        int sz = 0;
        vector<int> V;
        for(auto& p : mp){
            V.push_back(p.first);
            p.second = sz++;
        }
        vector<int> num(sz);
        for(int b : B) num[mp[gcd(K, b)]]++;
        for(int i=0; i<sz; i++) for(int j=i+1; j<sz; j++) if(V[j] % V[i] == 0) num[i] += num[j];
        for(int a : A) ans += num[mp[K/gcd(a, K)]];
    }
    cout << ans << endl;
    return 0;
}
0