結果
| 問題 | 
                            No.3118 Increment or Multiply
                             | 
                    
| コンテスト | |
| ユーザー | 
                             Yu_212
                         | 
                    
| 提出日時 | 2025-04-19 09:57:45 | 
| 言語 | C++23  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 65 ms / 2,000 ms | 
| コード長 | 3,498 bytes | 
| コンパイル時間 | 3,731 ms | 
| コンパイル使用メモリ | 274,828 KB | 
| 実行使用メモリ | 7,844 KB | 
| 最終ジャッジ日時 | 2025-04-19 09:57:52 | 
| 合計ジャッジ時間 | 5,594 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 35 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int iinf = 1e9;
const ll inf = 1e18;
template<ll mod>
struct Mint {
    using M=Mint; ll v;
    M& put(ll x) { v=(x<mod)?x:x-mod; return *this; }
    Mint(ll x=0) { put(x%mod+mod); }
    M operator+(M m) {return M().put(v+m.v);}
    M operator-(M m) {return M().put(v+mod-m.v);}
    M operator*(M m) {return M().put(v*m.v%mod);}
    M operator/(M m) {return M().put(v*m.inv().v%mod);}
    M operator+=(M m) { return put(v+m.v); }
    M operator-=(M m) { return put(v+mod-m.v); }
    M operator*=(M m) { return put(v*m.v%mod); }
    M operator/=(M m) { return put(v*m.inv().v%mod); }
    bool operator==(M m) { return v==m.v; }
    M pow(ll m) const {
        M x=v, res=1;
        while (m) {
            if (m&1) res=res*x;
            x=x*x; m>>=1;
        }
        return res;
    }
    M inv() { return pow(mod-2); }
};
template<ll mod>
ostream&operator<<(ostream&o,Mint<mod>v){return o<<v.v;}
template<typename T>
ostream& operator<<(ostream &o, vector<T> v) {
    for (int i = 0; i < v.size(); i++)
        o << v[i] << (i+1<v.size()?" ":"");
    return o;
}
template <typename T>
struct SegTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T ti;
    vector<T> dat;
    SegTree() {}
    SegTree(F f, T ti,int num) : f(f), ti(ti) {
        n = max(__bit_ceil(num), 1);
        dat.assign(n << 1, ti);
    }
    SegTree(F f,T ti,vector<T>&v):SegTree(f,ti,v.size()){
        for (int i = 0; i < v.size(); i++)
            dat[n + i] = v[i];
        for(int i=n-1;i;i--) dat[i]=f(dat[i*2], dat[i*2+1]);
    }
    void set_val(int k, T x) {
        dat[k += n] = x;
        while(k >>= 1) dat[k] = f(dat[k*2], dat[k*2+1]);
    }
    T query(int a, int b) {
        if (a >= b) return ti;
        T vl = ti, vr = ti;
        for (int l=a+n, r=b+n; l<r; l>>=1, r>>=1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};
struct Edge { int to; ll cost; };
const int MOD = 998244353;
// a^e mod
ll modpow(ll a, ll e=MOD-2){
    ll r=1;
    while(e){
        if(e&1) r=r*a%MOD;
        a=a*a%MOD;
        e>>=1;
    }
    return r;
}
using mint = Mint<MOD>;
mint sm(mint n) {
    return n * (n+1) / 2;
}
mint sm(mint l, mint r) {
    return sm(r) - sm(l);
}
ll naive(ll n, ll a) {
    ll ans = 0;
    for (ll i = 1; i <= n; i++) {
        ll x = n;
        while (x > i) {
            if (x % a == 0  && x / a >= i) {
                x /= a;
                ans++;
            } else {
                x--;
                ans++;
            }
        }
    }
    return ans;
}
int main() {
    cin.tie(0)->sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        ll n, a;
        // n = rand() % 100 + 1;
        // a = rand() % 100 +1 ;
        cin >> n >> a;
        if (a == 1) {
            cout << sm(n-1) << endl;
            continue;
        }
        // cout << n << " " << a << endl;
        ll m = n;
        mint ans = 0;
        mint sub = 0;
        while (true) {
            ll l = m / a + 1;
            ll r = m;
            if (l > r) break;
            // cout << l << " " << r << " "  << sub <<" " <<(sm(mint(r-l)) + mint(r-l+1)*sub)<< endl;
            ans += sm(mint(r-l)) + mint(r-l+1)*sub;
            sub += m % a + 1;
            m /= a;
        }
        cout << ans << endl;
        // cout << naive(n, a) << endl;
        // assert (naive(n,a)==ans);
    }
    return 0;
}
            
            
            
        
            
Yu_212