結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー t98slidert98slider
提出日時 2023-07-27 02:59:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,500 bytes
コンパイル時間 1,801 ms
コンパイル使用メモリ 172,032 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 16:19:35
合計ジャッジ時間 3,295 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 18 ms
6,944 KB
testcase_11 AC 17 ms
6,940 KB
testcase_12 AC 70 ms
6,940 KB
testcase_13 AC 13 ms
6,944 KB
testcase_14 AC 34 ms
6,944 KB
testcase_15 AC 17 ms
6,940 KB
testcase_16 AC 22 ms
6,944 KB
testcase_17 AC 15 ms
6,944 KB
testcase_18 AC 70 ms
6,944 KB
testcase_19 AC 34 ms
6,940 KB
testcase_20 AC 49 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, k;
    char c;
    cin >> n >> m >> k >> c;
    vector<int> a(n), b(m);
    for(auto &&v : b) cin >> v;
    for(auto &&v : a) cin >> v;
    
    if(c == '+'){
        ll ans = 0;
        for(int i = 0; i < n; i++) a[i] %= k;
        sort(a.begin(), a.end());
        for(int i = 0; i < m; i++){
            b[i] %= k;
            auto pa = equal_range(a.begin(), a.end(), k - b[i] == k ? 0 : k - b[i]);
            ans += pa.second - pa.first;
        }
        cout << ans << '\n';
    }else{
        vector<int> divs;
        for(int i = 1; i * i <= k; i++){
            if(k % i == 0){
                divs.emplace_back(i);
                if(k != i * i) divs.emplace_back(k / i);
            }
        }
        sort(divs.begin(), divs.end());
        int cn = divs.size();
        vector<int> cnt(cn);
        for(int i = 0; i < n; i++){
            int gcdv = __gcd(a[i], k);
            cnt[lower_bound(divs.begin(), divs.end(), gcdv) - divs.begin()]++;
        }
        for(int i = 1; i < cn; i++){
            for(int j = 0; j < i; j++){
                if(divs[i] % divs[j] == 0) cnt[j] += cnt[i];
            }
        }
        ll ans = 0;
        for(int i = 0; i < m; i++){
            int v = k / __gcd(b[i], k);
            ans += cnt[lower_bound(divs.begin(), divs.end(), v) - divs.begin()];
        }
        cout << ans << '\n';
    }
}
0