結果

問題 No.3516 Very Large Range Mod
コンテスト
ユーザー abc864197532
提出日時 2026-04-24 22:00:07
言語 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,659 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,414 ms
コンパイル使用メモリ 340,484 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2026-04-24 22:00:20
合計ジャッジ時間 9,292 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#ifdef Doludu
template <typename T>
ostream& operator << (ostream &o, vector <T> vec) {
    o << "{"; int f = 0;
    for (T i : vec) o << (f++ ? " " : "") << i;
    return o << "}";
}
void bug__(int c, auto ...a) {
    cerr << "\e[1;" << c << "m";
    (..., (cerr << a << " "));
    cerr << "\e[0m" << endl;
}
#define bug_(c, x...) bug__(c, __LINE__, "[" + string(#x) + "]", x)
#define bug(x...) bug_(32, x)
#define bugv(x...) bug_(36, vector(x))
#define safe bug_(33, "safe")
#else
#define bug(x...) void(0)
#define bugv(x...) void(0)
#define safe void(0)
#endif
const int mod = 998244353, N = 2000006;

//a * p.first + b * p.second = gcd(a, b)
pair<ll, ll> extgcd(ll a, ll b) {
  if (b == 0) return {1, 0};
  auto [y, x] = extgcd(b, a % b);
  return pair<ll, ll>(x, y - (a / b) * x);
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n; ll k; int m;
    cin >> n >> k >> m;
    vector <pii> a(n);
    vector <int> pre(n + 1);
    for (int i = 0; i < n; ++i) cin >> a[i].first;
    for (int i = 0; i < n; ++i) cin >> a[i].second;
    for (int i = 0; i < n; ++i) {
        pre[i + 1] = (pre[i] + 1ll * a[i].first * a[i].second) % m;
    }
    int x1 = 0, y1 = 0;
    int x2 = 0, y2 = k;
    {
        ll z = k;
        while (x2 < n && z >= a[x2].first) {
            z -= a[x2].first;
            x2++;
        }
        y2 = z;
    }

    ll ans = 0;
    while (x2 < n) {
        int nxt = min(a[x1].first - y1, a[x2].first - y2);

        int z1 = (pre[x1] + 1ll * y1 * a[x1].second) % m;
        int z2 = (pre[x2] + 1ll * y2 * a[x2].second) % m;

        // z1 + a[x1].second * i = z2 + a[x2].second * i
        int z = (z2 - z1 + m) % m;
        int x = (a[x1].second - a[x2].second + m) % m;
        // xi = z (mod m)
        bug(x, z);
        if (x == 0) {
            if (z == 0) ans += nxt;
        } else {
            int g = gcd(x, m);
            if (z % g == 0) {
                auto [r1, r2] = extgcd(x, m);
                ll fr = 1ll * r1 * (z / g);
                fr %= m;
                if (fr < 0) fr += m;
                bug(fr, x, z, m);
                if (fr < nxt) {
                    ans += (nxt - fr - 1) / (m / g) + 1;
                }
            }
        }

        y1 += nxt;
        y2 += nxt;
        if (y1 == a[x1].first) y1 = 0, x1++;
        if (y2 == a[x2].first) y2 = 0, x2++;
    }

    int z1 = (pre[x1] + 1ll * y1 * a[x1].second) % m;
    int z2 = pre[n];
    if (z1 == z2) ans++;

    cout << ans << "\n";
}
0