結果

問題 No.2699 Simple Math (Returned)
ユーザー Magentor
提出日時 2025-02-25 12:20:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,109 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 79,088 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-02-25 12:21:02
合計ジャッジ時間 4,264 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
using ll = long long;
const ll MOD = 998244353;

// 高速冪乗法: base^exp mod mod を計算する関数
ll modpow(ll base, ll exp, ll mod) {
    ll res = 1;
    base %= mod;
    while(exp > 0) {
        if(exp & 1) res = (res * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T;
    cin >> T;
    while(T--){
        ll N, M;
        cin >> N >> M;
        
        // 2M を周期として N を縮約
        ll period = 2 * M;
        ll r = N % period;
        
        ll ans = 0;
        if(r < M){
            // R = 10^r - 1
            ans = (modpow(10, r, MOD) - 1) % MOD;
            if(ans < 0) ans += MOD;
        } else {
            // r >= M のとき、r = M + k, ここで k = r - M
            ll k = r - M;
            // R = 10^M - 10^k
            ans = (modpow(10, M, MOD) - modpow(10, k, MOD)) % MOD;
            if(ans < 0) ans += MOD;
        }
        
        cout << ans % MOD << "\n";
    }
    return 0;
}
0