結果

問題 No.2703 FizzBuzz Letter Counting
ユーザー 👑 NachiaNachia
提出日時 2024-03-29 23:24:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,923 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 110,692 KB
実行使用メモリ 11,284 KB
最終ジャッジ日時 2024-03-29 23:24:53
合計ジャッジ時間 6,495 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,676 KB
testcase_01 AC 6 ms
6,676 KB
testcase_02 AC 7 ms
6,676 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <array>
#include <cmath>
#include <atcoder/modint>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
#define repr(i,n) for(int i=int(n)-1; i>=0; i--)
const i64 INF = 1001001001001001001;
const char* yn(bool x){ return x ? "Yes" : "No"; }
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
template<typename A> using nega_queue = std::priority_queue<A,std::vector<A>,std::greater<A>>;
using Modint = atcoder::static_modint<998244353>;
//#include "nachia/vec.hpp"
#include <cassert>

namespace nachia{

template<class Elem>
struct MatrixModulo{
private:
    int h;
    int w;
    std::vector<Elem> elems;
public:
    
    MatrixModulo(int new_h=0, int new_w=0)
        : h(new_h), w(new_w), elems(h*w, Elem(0)){}
    MatrixModulo(const MatrixModulo &) = default;
    int numRow() const { return h; }
    int numColumn() const { return w; }
    int height() const { return numRow(); }
    int width() const { return numColumn(); }
    typename std::vector<Elem>::iterator operator[](int y){ return elems.begin() + (y*w); }
    typename std::vector<Elem>::const_iterator operator[](int y) const { return elems.begin() + (y*w); }
    static MatrixModulo Identity(int n){
        auto res = MatrixModulo(n,n);
        for(int i=0; i<n; i++) res[i][i]=Elem(1);
        return res;
    }
    void swapColumns(int x1, int x2){
        assert(0 <= x1 && x1 < numColumn());
        assert(0 <= x2 && x2 < numColumn());
        for(int y=0; y<numRow(); y++) std::swap((*this)[y][x1], (*this)[y][x2]);
    }
    void swapRows(int y1, int y2){
        assert(0 <= y1 && y1 < numRow());
        assert(0 <= y2 && y2 < numRow());
        for(int x=0; x<numColumn(); x++) std::swap((*this)[y1][x], (*this)[y2][x]);
    }
    MatrixModulo operator*(const MatrixModulo& r) const {
        assert(width() == r.height());
        auto res = MatrixModulo(h, r.w);
        for(int i=0; i<h; i++) for(int j=0; j<w; j++) for(int k=0; k<r.w; k++){
            res[i][k] += (*this)[i][j] * r[j][k];
        }
        return res;
    }
    std::vector<Elem> operator*(const std::vector<Elem>& r) const {
        assert(width() == int(r.size()));
        auto res = std::vector<Elem>(h);
        for(int i=0; i<h; i++) for(int j=0; j<w; j++){
            res[i] += (*this)[i][j] * r[j];
        }
        return res;
    }
    Elem det() const {
        assert(height() == width());
        MatrixModulo g = *this;
        Elem ans = 1;
        for(int i=0; i<h; i++){
            int tg = -1;
            for(int j=i; j<h; j++){ if(g[j][i].val() != 0) tg = j; }
            if(tg == -1) return 0;
            if(tg != i) ans = -ans;
            for(int j=0; j<h; j++) std::swap(g[i][j], g[tg][j]);
            tg = i;
            ans *= g[i][i];
            Elem const_coeff = g[i][i].inv();
            for(int j=0; j<h; j++) g[i][j] *= const_coeff;
            for(int j=i+1; j<h; j++) for(int k=h-1; k>=i; k--) g[j][k] -= g[j][i] * g[i][k];
        }
        return ans;
    }
    int rank() const {
        if(height() == 0 || width() == 0) return 0;
        MatrixModulo g = *this;
        int y = 0;
        for(int d=0; d<w; d++){
            if(y == h) break;
            int tg = -1;
            for(int i=y; i<h; i++){ if(g[i][d].val() != 0){ tg = i; break; } }
            if(tg == -1) continue;
            for(int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]);
            tg = y;
            Elem const_coeff = g[y][d].inv();
            for(int j=d; j<w; j++) g[y][j] *= const_coeff;
            for(int i=y+1; i<h; i++) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
            y++;
        }
        return y;
    }
    MatrixModulo pow(unsigned long long i){
        auto a = *this;
        auto res = Identity(height());
        while(i){
            if(i%2) res = res * a;
            a = a * a; i /= 2;
        }
        return res;
    }
};


} // namespace nachia
using Mat = nachia::MatrixModulo<Modint>;
using namespace std;

struct State {
    array<Modint, 15> l00;
    array<Modint, 15> l01;
    array<Modint, 15> l11;
    array<Modint, 15> l20;
    array<Modint, 15> l21;
    array<Modint, 15> l31;
    array<Modint, 15> l40;
    array<Modint, 15> l41;
    Modint j3;
    int jtimes = 1;
    int jpos = 0;
};

State operator*(State l, State r){
    State res;
    res.jpos = (l.jpos * r.jtimes + r.jpos) % 15;
    res.j3 = l.j3 + r.j3;
    res.jtimes = l.jtimes * r.jtimes % 15;
    rep(a,15) rep(b,15) res.l00[(a*r.jtimes+b)%15] += l.l00[a] * r.l00[b];
    rep(a,15) rep(b,15) res.l01[(a*r.jtimes+b)%15] += l.l00[a] * r.l01[b] + l.l01[a] * r.l11[b];
    rep(a,15) rep(b,15) res.l11[(a*r.jtimes+b)%15] += l.l11[a] * r.l11[b];
    rep(a,15) rep(b,15) res.l20[(a*r.jtimes+b)%15] += l.l20[a] * r.l00[b];
    rep(a,15) rep(b,15) res.l21[(a*r.jtimes+b)%15] += l.l21[a] * r.l11[b] + l.l20[a] * r.l01[b];
    rep(a,15) rep(b,15) res.l31[(a*r.jtimes+b)%15] += l.l31[a] * r.l11[b];
    rep(b,15) res.l31[(l.jpos*r.jtimes+b)%15] += r.l31[b];
    rep(b,15) res.l21[(l.jpos*r.jtimes+b)%15] += l.j3 * r.l31[b];
    rep(b,15) res.l20[(l.jpos*r.jtimes+b)%15] += r.l20[b];
    rep(b,15) res.l21[(l.jpos*r.jtimes+b)%15] += r.l21[b];
    rep(b,15) res.l40[b] += r.l40[b];
    rep(b,15) res.l41[b] += r.l41[b];
    rep(a,15) rep(b,15) res.l40[(a*r.jtimes+b)%15] += l.l40[a] * r.l00[b];
    rep(a,15) rep(b,15) res.l41[(a*r.jtimes+b)%15] += l.l41[a] * r.l11[b] + l.l40[a] * r.l01[b];
    return res;
}

void testcase(){
    State base;
    base.jtimes = 10;
    base.j3 = 1;
    rep(d,10){
        base.l00[d] += 1;
        base.l01[d] += 1;
        base.l11[d] += 1;
        if(d != 0){
            base.l40[d] += 1;
            base.l41[d] += 1;
        }
    }
    vector<State> X;
    X.push_back(base);
    rep(d,10){
        State y = X.back();
        y.l20[d] += 1;
        y.l21[d] += 1;
        y.l31[d] += 1;
        // rep(i,15) cout << y.l20[i].val() << " ";
        //cout << endl;
        X.push_back(y);
    }
    rep(d,10){
        X[d].j3 = 1;
        X[d].jpos = d;
        X[d].jtimes = 10;
    }
    
    vector<vector<State>> Xp(10);
    rep(d,10){
        Xp[d].push_back(X[d]);
        rep(t,50) Xp[d].push_back(Xp[d].back() * Xp[d].back());
    }
    State v;
    int M; cin >> M;
    vector<int> V(M);
    vector<i64> C(M);
    rep(i,M) cin >> V[i] >> C[i];
    rep(d,V[0]) if(d){
        v.l40[d] += 1;
        v.l41[d] += 1;
    }
    // reverse(V.begin(), V.end());
    // reverse(C.begin(), C.end());
    v.jpos = V[0];
    v.jtimes = 10;
    v.j3 = 1;
    C[0]--;
    rep(i,M){
        rep(d,40) if((C[i] >> d) & 1) v = v * Xp[V[i]][d];
    }
    Modint ans = 0;
    array<Modint,15> ans0;
    array<Modint,15> ans1;
    rep(i,15) ans0[i] += v.l40[i] + v.l20[i];
    rep(i,15) ans1[i] += v.l41[i] + v.l21[i];
    ans0[v.jpos] += 1;
    ans1[v.jpos] += v.j3;
    rep(d,15){
        if(d==0) ans += ans0[d] * 8;
        else if(d%3 == 0) ans += ans0[d] * 4;
        else if(d%5 == 0) ans += ans0[d] * 4;
        else ans += ans1[d];
    }
    //for(auto a : v.l40) cout << a.val() << " ";
    //cout << endl;
    //for(auto a : v.l41) cout << a.val() << " ";
    //cout << endl;
    //for(auto a : v.l20) cout << a.val() << " ";
    //cout << endl;
    //for(auto a : v.l21) cout << a.val() << " ";
    //cout << endl;
    //for(auto a : ans0) cout << a.val() << " ";
    //cout << endl;
    //for(auto a : ans1) cout << a.val() << " ";
    //cout << endl;
    cout << ans.val() << endl;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    #ifdef NACHIA
    int T; cin >> T; for(int t=0; t<T; T!=++t?(cout<<'\n'),0:0)
    #endif
    testcase();
    return 0;
}
0