結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー sahiyasahiya
提出日時 2020-12-21 16:22:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 2,121 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 119,576 KB
実行使用メモリ 18,720 KB
最終ジャッジ日時 2023-10-21 11:43:39
合計ジャッジ時間 3,291 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 24 ms
7,164 KB
testcase_11 AC 17 ms
4,776 KB
testcase_12 AC 76 ms
5,332 KB
testcase_13 AC 12 ms
4,348 KB
testcase_14 AC 18 ms
5,032 KB
testcase_15 AC 12 ms
4,440 KB
testcase_16 AC 33 ms
8,584 KB
testcase_17 AC 9 ms
4,348 KB
testcase_18 AC 79 ms
5,332 KB
testcase_19 AC 32 ms
4,572 KB
testcase_20 AC 86 ms
18,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef bitset<16> BS;
struct edge {
    int to, cost, id;
};
const ll MOD = 1E+09 + 7; // =998244353;
const ll INF = 1E18;
const int MAX_N = 1E+05;

ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

int N, M;
ll K;

ll A[MAX_N + 1], B[MAX_N + 1];

ll gcd(ll a, ll b);

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> M >> K;
    char op;
    cin >> op;

    for (int i = 1; i <= M; i++) {
        cin >> B[i];
    }
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
    }
    ll ans = 0;
    if (op == '+') {
        unordered_map<ll, ll> AM, BM;
        for (int i = 1; i <= N; i++) {
            AM[A[i] % K]++;
        }
        for (int j = 1; j <= M; j++) {
            BM[B[j] % K]++;
        }
        for (auto p : AM) {
            ans += BM[(K - p.first) % K] * p.second;
        }
    } else {
        unordered_map<ll, ll> AD, BD;
        for (int i = 1; i <= N; i++) {
            AD[gcd(A[i], K)]++;
        }
        for (int j = 1; j <= M; j++) {
            BD[gcd(B[j], K)]++;
        }
        for (auto pa : AD) {
            for (auto pb : BD) {
                ll da = pa.first, db = pb.first;
                if ((da * db) % K == 0) {
                    ans += pa.second * pb.second;
                }
            }
        }
    }

    /*
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
        }
    }
    */

    cout << ans << "\n";

    return 0;
}

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;

    return gcd(b, a % b);
}
0