結果

問題 No.3516 Very Large Range Mod
コンテスト
ユーザー Yoyoyo8128
提出日時 2026-04-17 01:34:10
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 3,882 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,191 ms
コンパイル使用メモリ 221,444 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2026-04-24 20:55:27
合計ジャッジ時間 10,202 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#pragma region Yoyoyo

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using i128t=__int128_t;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
const long long inf=1ll<<60;
const string Yes="Yes";
const string No="No";
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define faster ios::sync_with_stdio(false);cin.tie(nullptr);
#define print(s) cout<<s<<endl;

template<typename T>
inline bool chmax(T &a,T b){return ((a<b)?(a=b,true):(false));}
template<typename T>
inline bool chmin(T &a,T b){return ((a>b)?(a=b,true):(false));}
template<typename T>
ll sum(const T&a){return accumulate(all(a),0LL);}

template<typename T,typename U>
istream &operator>>(istream &is,pair<T,U>&p){
    is>>p.first>>p.second;
    return is;
}

template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U>&p){
    os<<p.first<<' '<<p.second;
    return os;
}

template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(auto &x:v)is>>x;
    return is;
}

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    int s=v.size();
    for(int i=0;i<s;i++){
        os<<(i?" ":"")<<v[i];
    }
    return os;
}

template<typename T>
ostream &operator<<(ostream &os,const vector<vector<T>>&v){
    int s=v.size();
    for(int i=0;i<s;i++){
        os<<v[i]<<endl;
    }
    return os;
}

template<typename T>
ostream &operator<<(ostream &os,const vector<vector<vector<T>>>&v){
    int s=v.size();
    for(int i=0;i<s;i++){
        os<<"i = "<<i<<endl;
        os<<v[i];
    }
    return os;
}

#ifdef LOCAL
template<class... Args>
void debug_out(Args... args){
    int _i=0;
    ((cerr<<(_i++?", ":" ")<<args), ...);
    cerr<<"\n";
}
#define debug(...){\
    cerr<<"["<<#__VA_ARGS__<<"]:";\
    debug_out(__VA_ARGS__);\
}
#else
#define debug(...)
#endif

#pragma endregion Yoyoyo


long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}


int main(){
    faster;
    ll N,K,M;
    cin>>N>>K>>M;
    vector<ll>B(N),C(N);
    cin>>B>>C;
    {
        assert(1<=N && N<=200000);
        assert(1<=K && K<=1000000000 && 1<=M && M<=1000000000);
        for(int i=0;i<N;i++){
            assert(1<=B[i] && B[i]<=1000000000 && 1<=C[i] && C[i]<=1000000000);
        }
    }
    vector<ll>rui(N+1,0);
    for(int i=0;i<N;i++)rui[i+1]=rui[i]+B[i];
    ll now=0;
    {
        ll K2=K;
        for(int i=0;i<N;i++){
            now+=C[i]*min(K,B[i]);
            K-=min(K,B[i]);
            now%=M;
        }
        K=K2;
    }
    ll idx=0;
    ll ans=0;
    if(!now)ans++;
    debug(rui,now);
    while(idx+K<rui[N]){
        auto itr1=upper_bound(all(rui),idx);
        auto itr2=upper_bound(all(rui),idx+K);
        ll nex=min(((*itr1)-(idx)),((*itr2)-(idx+K)));
        ll sa=0;
        debug(nex);
        {
            sa-=C[distance(rui.begin(),itr1)-1];
            sa+=C[distance(rui.begin(),itr2)-1];
            sa%=M;
            while(sa<0)sa+=M;
        }
        debug(sa);
        if(sa==0){
            if(now==0)ans+=nex;
            idx+=nex;
            now+=sa*nex;
            now%=M;
            continue;
        }
        ll g=gcd(M,sa);
        ll per=M/g;
        if(gcd(g,now)!=g){
            idx+=nex;
            now+=sa*nex;
            now%=M;
            continue;
        }
        ll x,y;
        extGCD(g,M,x,y);
        x%=per;
        while(x<0)x+=per;
        ll ko=max(0ll,((nex-x)+(per-1))/per);
        ans+=ko;
        idx+=nex;
        now+=sa*nex;
        now%=M;
    }
    print(ans);
}
0