結果

問題 No.2703 FizzBuzz Letter Counting
ユーザー 👑 hos.lyrichos.lyric
提出日時 2024-03-29 23:06:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 983 ms / 3,000 ms
コード長 7,682 bytes
コンパイル時間 1,750 ms
コンパイル使用メモリ 130,720 KB
実行使用メモリ 72,832 KB
最終ジャッジ日時 2024-03-29 23:07:48
合計ジャッジ時間 48,076 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 455 ms
71,936 KB
testcase_01 AC 453 ms
71,936 KB
testcase_02 AC 451 ms
71,936 KB
testcase_03 AC 921 ms
72,704 KB
testcase_04 AC 552 ms
72,064 KB
testcase_05 AC 528 ms
72,064 KB
testcase_06 AC 671 ms
72,192 KB
testcase_07 AC 800 ms
72,448 KB
testcase_08 AC 475 ms
72,064 KB
testcase_09 AC 869 ms
72,576 KB
testcase_10 AC 659 ms
72,192 KB
testcase_11 AC 472 ms
72,064 KB
testcase_12 AC 742 ms
72,320 KB
testcase_13 AC 570 ms
72,192 KB
testcase_14 AC 925 ms
72,832 KB
testcase_15 AC 891 ms
72,704 KB
testcase_16 AC 899 ms
72,704 KB
testcase_17 AC 512 ms
72,064 KB
testcase_18 AC 815 ms
72,448 KB
testcase_19 AC 698 ms
72,320 KB
testcase_20 AC 751 ms
72,448 KB
testcase_21 AC 661 ms
72,192 KB
testcase_22 AC 567 ms
72,192 KB
testcase_23 AC 948 ms
72,832 KB
testcase_24 AC 679 ms
72,320 KB
testcase_25 AC 702 ms
72,320 KB
testcase_26 AC 921 ms
72,704 KB
testcase_27 AC 839 ms
72,576 KB
testcase_28 AC 978 ms
72,832 KB
testcase_29 AC 971 ms
72,832 KB
testcase_30 AC 971 ms
72,832 KB
testcase_31 AC 960 ms
72,832 KB
testcase_32 AC 975 ms
72,832 KB
testcase_33 AC 942 ms
72,832 KB
testcase_34 AC 967 ms
72,832 KB
testcase_35 AC 957 ms
72,832 KB
testcase_36 AC 983 ms
72,832 KB
testcase_37 AC 952 ms
72,832 KB
testcase_38 AC 974 ms
72,832 KB
testcase_39 AC 954 ms
72,832 KB
testcase_40 AC 974 ms
72,832 KB
testcase_41 AC 950 ms
72,832 KB
testcase_42 AC 972 ms
72,832 KB
testcase_43 AC 448 ms
71,936 KB
testcase_44 AC 450 ms
71,936 KB
testcase_45 AC 450 ms
71,936 KB
testcase_46 AC 449 ms
71,936 KB
testcase_47 AC 458 ms
71,936 KB
testcase_48 AC 453 ms
71,936 KB
testcase_49 AC 451 ms
71,936 KB
testcase_50 AC 448 ms
71,936 KB
testcase_51 AC 458 ms
71,936 KB
testcase_52 AC 450 ms
71,936 KB
testcase_53 AC 451 ms
71,936 KB
testcase_54 AC 450 ms
71,936 KB
testcase_55 AC 457 ms
71,936 KB
testcase_56 AC 451 ms
71,936 KB
testcase_57 AC 452 ms
71,936 KB
testcase_58 AC 456 ms
71,936 KB
testcase_59 AC 454 ms
71,936 KB
testcase_60 AC 451 ms
71,936 KB
testcase_61 AC 450 ms
71,936 KB
testcase_62 AC 450 ms
71,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;

// square matrix
using Mat = vector<vector<Mint>>;
Mat zero(int n) {
  return Mat(n, vector<Mint>(n, 0));
}
Mat identity(int n) {
  Mat a(n, vector<Mint>(n, 0));
  for (int i = 0; i < n; ++i) a[i][i] = 1;
  return a;
}
Mat &operator+=(Mat &a, const Mat &b) {
  const int n = a.size();
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) a[i][j] += b[i][j];
  return a;
}
Mat &operator-=(Mat &a, const Mat &b) {
  const int n = a.size();
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) a[i][j] -= b[i][j];
  return a;
}
Mat operator+(const Mat &a, const Mat &b) {
  const int n = a.size();
  Mat c(n, vector<Mint>(n));
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) c[i][j] = a[i][j] + b[i][j];
  return c;
}
Mat operator-(const Mat &a, const Mat &b) {
  const int n = a.size();
  Mat c(n, vector<Mint>(n));
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) c[i][j] = a[i][j] - b[i][j];
  return c;
}
Mat operator*(Mint k, const Mat &a) {
  const int n = a.size();
  Mat b(n, vector<Mint>(n));
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) b[i][j] = k * a[i][j];
  return b;
}
Mat operator*(const Mat &a, const Mat &b) {
  const int n = a.size();
  Mat c(n, vector<Mint>(n, 0));
  for (int i = 0; i < n; ++i) for (int k = 0; k < n; ++k) for (int j = 0; j < n; ++j) {
    c[i][j] += a[i][k] * b[k][j];
  }
  return c;
}
Mat power(const Mat &a, long e) {
  const int n = a.size();
  Mat b = a, c(n, vector<Mint>(n, 0));
  for (int i = 0; i < n; ++i) c[i][i] = 1;
  for (; ; b = b * b) {
    if (e & 1) c = c * b;
    if (!(e >>= 1)) return c;
  }
}


constexpr int N = 2 * 4 * 2;
constexpr int A = 10;
int id(int s, int r, int u) {
  return (s * 4 + r) * 2 + u;
}
int mod(int r) {
  return r ? ((r - 1) % 3 + 1) : 0;
}

int M;
vector<int> V;
vector<Int> L;

constexpr int BASE = 1 << 10;
Mat trans[A];
Mat pw[A][4][BASE + 1];

int main() {
  for (int a = 0; a < A; ++a) {
    trans[a] = zero(N);
    for (int s = 0; s < 2; ++s) for (int r = 0; r < 4; ++r) for (int u = 0; u < 2; ++u) {
      for (int b = 0; b < A; ++b) if (s || b <= a) {
        const int rr = mod(r * A + b);
        for (int du = 0; u + du < 2; ++du) {
          if (!rr && du) continue;
          trans[a][id(s, r, u)][id((s || b < a) ? 1 : 0, rr, u + du)] += 1;
        }
      }
    }
    for (int e = 0; e < 4; ++e) {
      pw[a][e][0] = identity(N);
      pw[a][e][1] = e ? pw[a][e - 1][BASE] : trans[a];
      for (int k = 2; k <= BASE; ++k) {
        pw[a][e][k] = pw[a][e][k - 1] * pw[a][e][1];
      }
    }
  }
  
  for (; ~scanf("%d", &M); ) {
    V.resize(M);
    L.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%lld", &V[i], &L[i]);
    }
    
    vector<Mint> crt(N, 0);
    crt[id(0, 0, 0)] = 1;
    for (int i = 0; i < M; ++i) {
      Int l = L[i] - ((i == M - 1) ? 1 : 0);
      for (int e = 0; e < 4; ++e) {
        const int k = l % BASE;
        l /= BASE;
// cerr<<"*= pw["<<V[i]<<"]["<<e<<"]["<<k<<"]"<<endl;
        vector<Mint> nxt(N, 0);
        for (int x = 0; x < N; ++x) for (int y = 0; y < N; ++y) {
          nxt[y] += crt[x] * pw[V[i]][e][k][x][y];
        }
        crt.swap(nxt);
      }
    }
    Mint ans = 0;
    for (int s = 0; s < 2; ++s) for (int r = 0; r < 4; ++r) for (int u = 0; u < 2; ++u) {
      const Mint way = crt[id(s, r, u)];
// cerr<<s<<" "<<r<<" "<<u<<": "<<way<<endl;
      for (int b = 0; b < A; ++b) if (s || b <= V[M - 1]) {
        // const int ss = (s || b < V[M - 1]) ? 1 : 0;
        const int rr = mod(r * A + b);
        if (!rr) continue;
        for (int du = 0; u + du < 2; ++du) {
          const int uu = u + du;
          if (rr % 3 == 0 && b % 5 == 0) {
            if (uu == 0) ans += way * 8;
          } else if (rr % 3 == 0 || b % 5 == 0) {
            if (uu == 0) ans += way * 4;
          } else {
            if (uu == 1) ans += way;
          }
        }
      }
    }
    printf("%u\n", ans.x);
  }
  return 0;
}
0