結果
問題 |
No.3243 Multiplication 8 1
|
ユーザー |
|
提出日時 | 2025-08-22 21:47:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 181 ms / 2,000 ms |
コード長 | 3,665 bytes |
コンパイル時間 | 3,884 ms |
コンパイル使用メモリ | 255,708 KB |
実行使用メモリ | 7,716 KB |
最終ジャッジ日時 | 2025-08-22 21:47:21 |
合計ジャッジ時間 | 4,801 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 4 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using ll = long long; using mint = atcoder::modint998244353; template <class T, size_t N> struct Matrix { std::array<std::array<T, N>, N> A{}; Matrix() {} Matrix(const std::array<std::array<T, N>, N> &M) : A(M){} Matrix(const std::vector<std::vector<T>> &M) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] = M[i][j]; } } } const std::array<T, N>& operator[](int i) const { return A[i]; } std::array<T, N>& operator[](int i) { return A[i]; } Matrix& operator+=(const Matrix& rhs) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] += rhs[i][j]; } } return *this; } Matrix& operator-=(const Matrix& rhs) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] -= rhs[i][j]; } } return *this; } Matrix& operator*=(const Matrix& rhs) { std::array<std::array<T, N>, N> res{}; for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ for(size_t k = 0; k < N; k++){ res[i][j] += A[i][k] * rhs[k][j]; } } } swap(A, res); return *this; } Matrix& operator+() const { return *this; } Matrix& operator-() const { return Matrix() - *this; } friend Matrix operator+(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) += rhs; } friend Matrix operator-(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) -= rhs; } friend Matrix operator*(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) *= rhs; } friend bool operator==(const Matrix& lhs, const Matrix& rhs) { return (lhs.A == rhs.A); } friend bool operator!=(const Matrix& lhs, const Matrix& rhs) { return (lhs.A != rhs.A); } Matrix pow(long long v){ Matrix res, temp = A; for(size_t i = 0; i < N; i++)res[i][i] = 1; while(v){ if(v & 1)res *= temp; temp *= temp; v >>= 1; } return res; } friend std::ostream& operator << (std::ostream &os, const Matrix& rhs) noexcept { for(size_t i = 0; i < N; i++){ if(i) os << '\n'; for(size_t j = 0; j < N; j++){ os << (j ? " " : "") << rhs[i][j]; } } return os; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; vector<int> a = {-8, -4, -2, -1, 1, 2, 4, 8}; vector<int> b = {-2, -1, 1, 2}; Matrix<mint, 10> Mt; // 2 2 2 1 1 1 1 2 2 2 のときに困る for(int i = 0; i < a.size(); i++){ for(auto v : b){ if(a[i] == 8 && (v == 1 || v == -1))continue; v *= a[i]; if(binary_search(a.begin(), a.end(), v)){ int p = lower_bound(a.begin(), a.end(), v) - a.begin(); Mt[i][p]++; } } // 8ができた後の1や-1は特別扱いする if(a[i] == 8){ Mt[i][8]++; Mt[i][9]++; Mt[8][8]++; Mt[9][9]++; Mt[8][9]++; Mt[9][8]++; Mt[i][2]++; Mt[i][3]++; Mt[i][4]++; Mt[i][5]++; } } while(T--){ ll n; cin >> n; auto B = Mt.pow(n); cout << (B[4][7] + B[4][9]).val() << '\n'; } }