結果

問題 No.3516 Very Large Range Mod
コンテスト
ユーザー GOTKAKO
提出日時 2026-04-24 22:59:51
言語 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
結果
MLE  
実行時間 -
コード長 5,699 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,373 ms
コンパイル使用メモリ 218,736 KB
実行使用メモリ 1,307,832 KB
最終ジャッジ日時 2026-04-24 23:00:11
合計ジャッジ時間 6,779 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 MLE * 2
other -- * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

template<typename T>
bool chmin(T &a,T b){ //a>bならa=bに更新してtrue.
    if(a > b){a = b; return true;}
    else return false;
}
template<typename T> 
bool chmax(T &a,T b){ //a<bならa=bに更新してtrue.
    if(a < b){a = b; return true;}
    else return false;
}
template<typename T> 
T safemod(T a,T m){a %= m,a += m;return a>=m?a-m:a;} //return x = a mod m. 
template<typename T> 
T floor(T a,T b){ //return a/b切り下げ.
    if(b < 0) a *= -1,b *= -1;
    return a<0?(a+1)/b-1:a/b; 
   
}
template<typename T> 
T ceil(T a,T b){ //return a/b切り上げ.
    if(b < 0) a *= -1,b *= -1;
    return a>0?(a-1)/b+1:a/b; 
}
template<typename T> 
pair<T,T> invgcd(T a,T b){
    //return {gcd(a,b),x} (xa≡g(mod b))
    a = safemod(a,b);
    if(a == 0) return {b,0};
    T x = 0,y = 1,memob = b;
    while(a){
        T q = b/a;
        b -= a*q;
        swap(x,y); y -= q*x;
        swap(a,b);
    }
    if(x < 0) x += memob/b;
    return {b,x};
}
template<typename T>
bool isABmoreC(T a,T b,T c){ //a*b=cはfalse
    if(c%b) return a>=ceil(c,b);
    else return a>ceil(c,b);
}
template<typename T>
bool isABmoreC2(T a,T b,T c){return a>=ceil(c,b);} //a*b=cはtrue.
template<typename T>
bool isABlessC(T a,T b,T c){ //a*b=cはfalse.
    if(c%b) return a<=floor(c,b);
    else return a<floor(c,b);
}
template<typename T>
bool isABlessC2(T a,T b,T c){return a<=floor(c,b);} //a*b=cはtrue.
template<typename T>
T Kthpower(T a,int k){ //return a^k オーバーフローは考慮しない.
    T ret = 1;
    while(k){
        if(k&1) ret *= a;
        a *= a; k >>= 1;
    }
    return ret;
}
template<typename T>
pair<T,bool> Kthpower2(T a,int k){ //return {a^k,オーバーした?} オーバーフローは考慮する.
    T ret = 1,maxv = numeric_limits<T>::max();
    while(k){
        if(k&1){
            if(isABmoreC(ret,a,maxv)) return {-1,true};
            ret *= a;
        }
        if(k == 1) break;
        if(isABmoreC(a,a,maxv)) return {-1,true};
        a *= a; k >>= 1;
    }
    return {ret,false};
}
template<typename T>
T Kthroot(T a,int k){ //return floor(a^(1/k));
    assert(k > 0 && a >= 0);
    if(k == 1 || a <= 1) return a;
    T ret = pow(a,1.0/k);
    while(true){
        auto [check,over] = Kthpower2(ret+1,k);
        if(over || check > a) break;
        ret++;
    }    
    while(true){
        auto [check,over] = Kthpower2(ret,k);
        if(!over && check <= a) break;
        ret--;
    }    
    return ret;
}
template<typename T>
T powmod(T a,T b,T m){//a^b(mod m)を返す.
    assert(b >= 0);
    __int128_t ret = 1,p = a;
    while(b){
        if(b&1) ret = ret*p%m;
        p = p*p%m; b >>= 1;
    }
    return T(ret);
}
template<typename T>
T divmod(T a,T b,T m){//a/b(mod m)を返す 素数mod必須.
    return (T)((__int128_t)a*powmod(b,m-2,m)%m); 
}

long long solve(vector<long long> V,long long M,long long K){
    long long ret = 0,sum = 0;
    for(int i=0; i<K-1; i++) sum += V.at(i);
    for(int i=K-1; i<V.size(); i++){
        sum += V.at(i);
        ret += sum%M==0;
        sum -= V.at(i+1-K);
    }
    return ret;
}

random_device rnd;
mt19937 mt(rnd());

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    bool submit = true;
while(true){

    long long N,K,M;
    if(submit) cin >> N >> K >> M;
    else N = K = 5,M = 10;
    vector<long long> B(N),C(N);
    if(!submit){for(auto &b : B) b = mt()%5+1;}
    else for(auto &b : B) cin >> b;
    if(!submit){for(auto &c : C) c = mt()%M;}
    else for(auto &c : C) cin >> c;

    vector<long long> V;
    for(int i=0; i<N; i++) for(int k=0; k<B.at(i); k++) V.push_back(C.at(i));
    long long jury = solve(V,M,K);
    
    long long answer = 0;
    long long posl = 0,posr = 0,c = 0,s = 0;
    while(posl < N){
        long long bl = B.at(posl),cl = C.at(posl);
        while(posr < N){
            long long br = B.at(posr),cr = C.at(posr);
            if(c+br >= K){
                if(posl == posr){if(cl*K%M == 0) answer += bl+1-K;}
                else{
                    c -= bl,s -= bl*cl,s = (s%M+M)%M;

                    long long n = K-c,goal = (M-s)%M,x,y;
                    long long move = min(n,br-max(0LL,n-bl)),diff = (cr-cl+M)%M,now = 0;
                    if(bl < n) move = min(bl,br-max(0LL,n-bl));
                    now += min(n,bl)*cl;
                    now += max(0LL,n-bl)*cr;
                    goal = (goal-now%M+M)%M;
                    if(diff == 0){
                        if(goal == 0) answer += move+(bl<n)-(br>=n);
                    }
                    else{
                        long long g = gcd(diff,M);
                        if(goal%g == 0){
                            long long d = diff/g,m = M/g; goal /= g;
                            auto [ign,x] = invgcd(d,m);
                            x *= goal,x %= m;
                            if(x == 0 && bl >= n) x = m;
                            answer += (move+m-x)/m;
                            if((move+m-x)%m == 0 && br >= n) answer--;
                        }
                    }

                    c += bl,s += bl*cl,s %= M;
                }
            }
            if(c+br-bl >= K) break;
            posr++;
            c += br,s += br*cr,s %= M;
        }
        posl++;
        c -= bl,s -= bl*cl,s = (s%M+M)%M;
    }

    if(answer != jury && !submit){
        cout << "Wrong" << endl;
        cout << N << " " << K << " " << M << endl;
        for(auto b : B) cout << b << " "; cout << endl;
        for(auto b : C) cout << b << " "; cout << endl;
        cout << answer << " " << jury << endl;
        return 0;
    }

    cout << answer << endl;
    if(submit) break;
}
}
0