結果
問題 | No.2703 FizzBuzz Letter Counting |
ユーザー | 👑 hos.lyric |
提出日時 | 2024-03-29 23:06:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,089 ms / 3,000 ms |
コード長 | 7,682 bytes |
コンパイル時間 | 1,853 ms |
コンパイル使用メモリ | 129,180 KB |
実行使用メモリ | 72,704 KB |
最終ジャッジ日時 | 2024-09-30 16:50:22 |
合計ジャッジ時間 | 49,819 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 60 |
ソースコード
#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; }