結果

問題 No.3516 Very Large Range Mod
コンテスト
ユーザー T1610
提出日時 2026-04-30 23:42:44
言語 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
結果
WA  
実行時間 -
コード長 2,771 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,271 ms
コンパイル使用メモリ 379,740 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2026-04-30 23:43:11
合計ジャッジ時間 20,593 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 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 (ll i = (s); i < (ll)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (ll i = (ll)(s - 1); i >= (ll)(e); i--)
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

template <typename T, typename U>
bool chmax(T& a, const U& b) {
    if (a >= b) return false;
    a = b;
    return true;
}
template <typename T, typename U>
bool 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 s(n + 1);
    rep(i, n) s[i + 1] = s[i] + b[i];

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

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

    using lint = __int128_t;
    lint sum = 0;
    ll cur = 0;
    rep(i, n) {
        lint tmp = min(b[i], k - cur);
        sum += tmp * c[i];
        sum %= m;
        cur += tmp;
        if (cur == k) break;
    }

    ll ans = 0;
    rep(i, ev.size() - 1) {
        ll x = ev[i], d = ev[i + 1] - ev[i];
        auto it1 = upper_bound(all(s), x);
        auto it2 = upper_bound(all(s), x + k);
        ll y = *it2 - *it1;
        if (it1 != s.begin() && it2 != s.begin()) {
            --it1;
            --it2;
            ans += count_solutions(y, -sum, m, d);
            sum += (lint)y * d;
            sum %= m;
            if (sum < 0) sum += m;
        }
    }
    cout << ans + 1 << "\n";
}

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