結果

問題 No.3118 Increment or Multiply
ユーザー ponjuice
提出日時 2025-04-17 19:16:45
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,052 bytes
コンパイル時間 3,497 ms
コンパイル使用メモリ 274,780 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-04-17 19:16:52
合計ジャッジ時間 6,535 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353;

ll sums(ll n) {
    return (n % mod) * ((n + 1) % mod) / 2 % mod;
}

// 各ケースを解く関数
void solve() {
    ll n, a;
    cin >> n >> a;

    if(a == 1) {
        cout << sums(n-1) << endl;
        return;
    }
    else{
        ll now = n;
        ll ans = 0;
        // nowからnにするためのコストを持つ
        ll cost = 0;
        
        while(now > 0) {
            ll next = now / a;
            // [next + 1, now] までの数字をnowにするためのコストの総和
            ans += sums(now - next - 1) % mod;
            // nowになった数字をnにするためのコストの総和
            ans += (now - next) % mod * cost % mod;

            // nextからnにするためのコストを更新
            cost += now % a + 1;
            cost %= mod;

            now = next;
        }
        ans %= mod;
        cout << ans << endl;
    }
}

int main(){
    int t;
    cin >> t;
    while(t--) solve();
}
0