結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー kcvlexkcvlex
提出日時 2020-02-14 22:24:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,834 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 184,584 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2024-04-27 19:48:35
合計ジャッジ時間 3,828 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 43 ms
7,680 KB
testcase_11 AC 19 ms
6,940 KB
testcase_12 WA -
testcase_13 AC 14 ms
6,940 KB
testcase_14 AC 32 ms
6,944 KB
testcase_15 AC 17 ms
6,944 KB
testcase_16 AC 53 ms
8,832 KB
testcase_17 AC 16 ms
6,940 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 169 ms
17,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define DEBUGGING
#include <bits/stdc++.h>
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
using ll = std::int64_t;
using ull = std::uint64_t;
using PLL = std::pair<ll, ll>;
using TLL = std::tuple<ll, ll, ll>;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); }
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
#define mv_rec make_v(init, tail...)
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, size_t s, Tail... tail) { return V<decltype(mv_rec)>(s, mv_rec); }
#undef mv_rec
using namespace std;

#ifdef DEBUGGING
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

map<ll, ll> memo;
set<ll> calced;
V<ll> primes;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll dfs(ll cur, map<ll, ll> &cur_m, map<ll, ll> &max_m, map<ll, ll> &gcd_v) {
    if (calced.find(cur) != calced.end()) return memo[cur];
    calced.insert(cur);
    DEBUG(cur);
    ll l = 0;
    ll ret = gcd_v[cur];
    for (ll i = 0; i < primes.size(); i++) {
        ll p = primes[i];
        ll c = cur_m[p];
        ll m = max_m[p];
        if (c < m) {
            cur_m[p]++;
            ret += dfs(cur * p, cur_m, max_m, gcd_v);
            cur_m[p]--;
            if (l == 0) {
                l = cur * p;
            } else {
                l *= p;
                ret -= memo[l];
            }
        }
    }
    memo[cur] = ret;
    return ret;
}

ll solve_p(ll N, ll M, ll K, const V<ll> &A, const V<ll> &B) {
    map<ll, ll> cnt;
    for (ll e : A) cnt[e % K]++;
    ll ret = 0;
    for (ll e : B) {
        ll mod = e % K;
        ll key = (K - mod) % K;
        ret += cnt[key];
    }
    return ret;
}

ll solve_m(ll N, ll M, ll K, const V<ll> &A, const V<ll> &B) {
    map<ll, ll> gcd_v;
    for (ll e : B) gcd_v[gcd(e, K)]++;
    for (auto &&e : gcd_v) DEBUG(e);
    ll cpy = K;
    map<ll, ll> max_m;
    
    auto add_p = [&](ll key, ll val) {
        primes.push_back(key);
        max_m[key] += val;
    };
    
    for (ll i = 2; i * i <= K; i++) {
        ll c = 0;
        while (cpy % i == 0) {
            cpy /= i;
            c++;
        }
        if (c) add_p(i, c);
    }
    if (1 < cpy) add_p(cpy, 1);
    map<ll, ll> cur_m;
    dfs(1, cur_m, max_m, gcd_v);
    ll ret = 0;
    for (auto &&e : memo) DEBUG(e);
    for (ll e : A) {
        ll g = gcd(e, K);
        DEBUG(K/g);
        ret += memo[K / g];
    }
    return ret;
}

int main() {
    ll N, M, K;
    cin >> N >> M >> K;
    char op;
    cin >> op;
    V<ll> B(M), A(N);
    for (ll &e : B) cin >> e;
    for (ll &e : A) cin >> e;
    ll ans = (op == '+' ?  solve_p(N, M, K, A, B) : solve_m(N, M, K, A, B));
    cout << ans << endl;
    return 0;
}
0