結果

問題 No.3516 Very Large Range Mod
コンテスト
ユーザー T1610
提出日時 2026-05-01 02:07:45
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 2,980 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,089 ms
コンパイル使用メモリ 380,100 KB
実行使用メモリ 14,464 KB
最終ジャッジ日時 2026-05-01 02:07:57
合計ジャッジ時間 10,844 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

template <typename T, typename U>
T chmax(T& a, const U& b) {
    if (a >= b) return false;
    a = b;
    return true;
}
template <typename T, typename U>
T chmin(T& a, const U& b) {
    if (a <= b) return false;
    a = b;
    return true;
}

void yes_no(bool f, string yes = "Yes", string no = "No") { cout << (f ? yes : no) << "\n"; }

ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll g = a;
    x = 1LL;
    y = 0LL;
    if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
    return g;
}

// ax ≡ b (mod m) の 0 <= x < D における解の個数を求める
ll count_solutions(ll a, ll b, ll m, ll D) {
    a = (a % m + m) % m;
    b = (b % m + m) % m;
    if (a == 0) return (b == 0 ? D : 0);

    ll x, y;
    ll g = extgcd(a, m, x, y);
    if (b % g != 0) return 0;

    ll m_prime = m / g;
    // 最小の非負整数解 x0 を求める
    ll x0 = (x % m_prime + m_prime) % m_prime;
    x0 = (__int128)x0 * (b / g) % m_prime;

    if (x0 >= D) return 0;
    return (D - 1 - x0) / m_prime + 1;
}

void solve() {
    ll n, k, m;
    cin >> n >> k >> m;
    vl b(n), c(n);
    rep(i, n) cin >> b[i];
    rep(i, n) cin >> c[i];

    vl sb(n + 1);
    rep(i, n) sb[i + 1] = sb[i] + b[i];

    ll len = sb.back();
    if (len < k) {
        cout << 0 << '\n';
        return;
    }

    ll lim = len - k;
    vl ev;
    ev.reserve(2 * n);
    ev.emplace_back(0);
    ev.emplace_back(lim + 1);
    for (auto&& s : sb) {
        if (s > 0 && s <= lim) ev.emplace_back(s);
        if (s - k > 0 && s - k <= lim) ev.emplace_back(s - k);
    }
    sort(all(ev));
    ev.erase(unique(all(ev)), ev.end());

    using lint = __int128_t;
    lint s = 0;
    rep(i, n) {
        ll l = max(sb[i], 0LL);
        ll r = min(sb[i + 1], k);
        if (l < r) {
            s += (lint)(r - l) * c[i];
            s %= m;
        }
    }

    ll ans = 0;
    rep(i, ev.size() - 1) {
        ll x = ev[i], d = ev[i + 1] - ev[i];
        int outid = upper_bound(all(sb), x) - sb.begin() - 1;
        ll out = c[outid];
        ll in = 0;
        if (x + k < len) {
            int inid = upper_bound(all(sb), x + k) - sb.begin() - 1;
            in = c[inid];
        }

        ans += count_solutions(in - out, -s, m, d);

        if (i + 1 < ev.size() - 1) {
            lint y = ((lint)(in - out) * d) % m;
            s += y;
            s %= m;
            if (s < 0) s += m;
        }
    }
    cout << ans << '\n';
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t = 1;
    // cin >> t;
    rep(ti, t) solve();
    return 0;
}
0