結果

問題 No.3119 A Little Cheat
ユーザー GOTKAKO
提出日時 2025-04-19 08:24:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 5,219 bytes
コンパイル時間 2,638 ms
コンパイル使用メモリ 210,184 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-04-19 08:24:22
合計ジャッジ時間 7,954 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long mod = 998244353;
//入力が必ず-mod<a<modの時.
struct mint{
    long long v = 0;
    mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;}
    mint(unsigned long long a){v = a;}
    long long val(){return v;}
    void modu(){v %= mod;}

    mint &operator=(const mint &b) = default;
    mint operator-() const {return mint(0)-(*this);}
    mint operator+(const mint b){return mint(v)+=b;}
    mint operator-(const mint b){return mint(v)-=b;}
    mint operator*(const mint b){return mint(v)*=b;}
    mint operator/(const mint b){return mint(v)/=b;}

    mint operator+=(const mint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    mint operator-=(const mint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    mint operator*=(const mint b){v = v*b.v%mod; return *this;}
    mint operator/=(mint b){
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }

    mint operator++(){*this += 1; return *this;}
    mint operator--(){*this -= 1; return *this;}
    mint operator++(int){*this += 1; return *this;}
    mint operator--(int){*this -= 1; return *this;}
    bool operator==(const mint b){return v == b.v;}
    bool operator!=(const mint b){return v != b.v;}
    bool operator>(const mint b){return v > b.v;}
    bool operator>=(const mint b){return v >= b.v;}
    bool operator<(const mint b){return v < b.v;}
    bool operator<=(const mint b){return v <= b.v;}

    mint pow(long long n){
        mint ret = 1,p = v;
        if(n < 0) p = p.inv(),n = -n;
        while(n){
            if(n&1) ret *= p;
            p *= p; n >>= 1;
        }
        return ret;
    }
    mint inv(){return mint(1)/v;}
};

mint jury(int N,int M,vector<int> A){
    vector<int> B(N);
    mint ret = 0;
    auto dfs = [&](auto dfs,int pos) -> void {
        if(pos == N){
            for(int i=0; i<N; i++) ret += (int)(B.at(i)>A.at(i));
            for(int i=0; i<N-1; i++){
                int one = B.at(i)>A.at(i);
                one += B.at(i+1)>A.at(i+1);
                int two = B.at(i)>A.at(i+1);
                two += B.at(i+1)>A.at(i);
                if(one < two){ret++; break;}
            }
            return;
        }

        for(int i=1; i<=M; i++){
            B.at(pos) = i;
            dfs(dfs,pos+1);
        }
    };
    dfs(dfs,0);
    return ret;
}

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

    int N,M; cin >> N >> M;
    vector<int> A(N);
    for(auto &a : A) cin >> a;

    //cout << jury(N,M,A).v << endl;

    vector<mint> pm(N+1,1),S(N);
    for(int i=1; i<=N; i++) pm.at(i) = pm.at(i-1)*M;
    for(int i=N-1; i>=0; i--){
        S.at(i) = (M-A.at(i))%mod;
        if(i < N-1) S.at(i) += S.at(i+1);
    }

    mint answer = pm.at(N-1)*(M-A.at(0));
    vector<pair<int,mint>> imos = {{1,1}};
    int a2 = A.at(0);
    for(int i=1; i<N; i++){
        vector<pair<int,mint>> next;
        int a = A.at(i);
        if(a2 <= a){
            mint now = 0;
            {
                int back = 0; mint sum = 0;
                for(auto [p,v] : imos){ 
                    now += sum*(p-back);
                    sum += v; back = p;
                }
                now += sum*(M+1-back);
            }
            answer += now*(M-A.at(i))*pm.at(N-1-i);
            next.push_back({1,now});
            mint end = 0;
            {
                int back = 0; mint sum = 0;
                for(auto [p,v] : imos){
                    end += sum*max(0,min(p,a+1)-max(back,a2+1));
                    sum += v; back = p;
                }
                end += sum*max(0,min(M+1,a+1)-max(back,a2+1));
            }
            mint plus1 = (now-end)*(a-a2);
            if(i < N-1) plus1 = plus1*pm.at(N-1-i)+plus1*pm.at(N-2-i)*S.at(i+1);
            else plus1 = plus1*pm.at(N-1-i);
            answer += plus1;
            next.push_back({a2+1,-now+end});
            next.push_back({a+1,now-end});
        }
        else{
            swap(a,a2);
            mint now = 0;
            {
                int back = 0; mint sum = 0;
                for(auto [p,v] : imos){ 
                    now += sum*(p-back);
                    sum += v; back = p;
                }
                now += sum*(M+1-back);
            }
            answer += now*(M-A.at(i))*pm.at(N-1-i);
            next.push_back({1,now});
            mint end = 0;
            {
                int back = 0; mint sum = 0;
                for(auto [p,v] : imos){
                    end += sum*max(0,min(p,a+1)-max(back,a2+1));
                    sum += v; back = p;
                }
                end += sum*max(0,min(M+1,a+1)-max(back,a2+1));
            }
            mint plus1 = end*(M-(a-a2));
            if(i < N-1) plus1 = plus1*pm.at(N-1-i)+plus1*pm.at(N-2-i)*S.at(i+1);
            else plus1 = plus1*pm.at(N-1-i);
            answer += plus1;
            next.push_back({1,-end});
            next.push_back({a2+1,end});
            next.push_back({a+1,-end});
        }
        a2 = A.at(i); imos = next;
    }
    cout << answer.v << endl;
}
0