結果

問題 No.2699 Simple Math (Returned)
ユーザー tobbietobbie
提出日時 2024-05-01 23:37:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,294 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 166,448 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-01 23:37:29
合計ジャッジ時間 5,478 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long int;
#define MOD 998244353

int mod_pow(int x, int n) {
  int ret = 1;
  while (n > 0) {
    if ((n & 0x01) != 0)
      ret = (long long)ret * x % MOD;
    x = (long long)x * x % MOD;
    n >>= 1;
  }
  return ret;
}

int main() {
  int T;
  cin >> T;
  rep(i, T) {
    int N, M;
    cin >> N >> M;
    // 10^N - 1 = (10^M + 1)10^(N-M) - 10^(N-M) - 1
    //          = (10^M + 1)10^(N-M) - (10^(N-M) + 1)
    // (10^N - 1) % (10^M +1) = - (10^(N-M) + 1) / (10^M + 1)
    // 10^(N-M) + 1 = (10^M + 1)10^(N-2・M) - 10^(N-2・M) + 1
    //              = (10^M + 1)10^(N-2・M) - (10^(N-2・M) - 1)
    // (10^(N-M) + 1) % (10^M + 1) = - (10^(N-2・M) - 1) / (10^M + 1)
    // (10^N - 1) % (10^M + 1) = (10^(N-2・M) - 1) / (10^M + 1)
    //                         = (10^(N-2・M・k) - 1) / (10^M + 1)
    // N-2・M・k >= M ... ((10^(N-2・M・k) - 1) - (10^M + 1)) % MOD
    // N-2・M・k <  M ... (10^(N-2・M・k) - 1) % MOD
    while (N >= 2*M) N -= 2*M;
    int ans;
    if (N >= M) {
      ans = (mod_pow(10, N) - mod_pow(10, N) - 2) % MOD;
    } else {
      ans = mod_pow(10, N) - 1;
      if (ans < 0) ans += MOD;
    }
    cout << ans << endl;
  }
  return 0;
}
0