結果

問題 No.3242 Count 8 Included Numbers (Hard)
ユーザー tnakao0123
提出日時 2025-08-25 11:41:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 2,085 bytes
コンパイル時間 466 ms
コンパイル使用メモリ 44,476 KB
実行使用メモリ 9,448 KB
最終ジャッジ日時 2025-08-25 11:41:13
合計ジャッジ時間 1,860 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:68:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |   scanf("%s", s);
      |   ~~~~~^~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3242.cc:  No.3242 Count 8 Included Numbers (Hard) - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 888888 + 4;
const int MOD = 998244353;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; }
  MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; }

  explicit operator int() const { return v; }
  
  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator-() const { return MI(MOD - v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

using mi = MI<MOD>;

/* global variables */

char s[MAX_N];
int ds[MAX_N];
mi es[MAX_N];

/* subroutines */

/* main */

int main() {
  scanf("%s", s);
  int n = strlen(s);

  for (int i = 0; i < n; i++) ds[i] = (s[n - 1 - i] - '0');

  int co = 1;
  for (int i = 0; co && i < n; i++) {
    int di = ds[i] + co;
    ds[i] = di % 10;
    co = di / 10;
  }
  if (co) ds[n++] = 1;
  
  es[0] = 1;
  for (int i = 0; i < n; i++) es[i + 1] = es[i] * 9;

  mi s0 = 0;
  for (int i = n - 1; i >= 0; i--) s0 = s0 * 10 + ds[i];

  mi s1 = 0;
  for (int i = n - 1; i >= 0; i--) {
    s1 += es[i] * min(8, ds[i]);
    if (ds[i] == 8) break;
  }

  //printf(" s0=%d, s1=%d\n", (int)s0, (int)s1);
  printf("%d\n", (int)(s0 - s1));
  
  return 0;
}
0