結果

問題 No.840 ほむほむほむら
ユーザー beetbeet
提出日時 2019-06-14 22:01:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 4,000 ms
コード長 3,803 bytes
コンパイル時間 1,975 ms
コンパイル使用メモリ 201,208 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-09 00:34:01
合計ジャッジ時間 5,939 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
4,380 KB
testcase_01 AC 31 ms
4,376 KB
testcase_02 AC 27 ms
4,380 KB
testcase_03 AC 27 ms
4,380 KB
testcase_04 AC 31 ms
4,380 KB
testcase_05 AC 56 ms
4,380 KB
testcase_06 AC 60 ms
4,376 KB
testcase_07 AC 60 ms
4,376 KB
testcase_08 AC 48 ms
4,380 KB
testcase_09 AC 60 ms
4,384 KB
testcase_10 AC 98 ms
4,380 KB
testcase_11 AC 118 ms
4,380 KB
testcase_12 AC 93 ms
4,380 KB
testcase_13 AC 122 ms
4,376 KB
testcase_14 AC 115 ms
4,380 KB
testcase_15 AC 180 ms
4,376 KB
testcase_16 AC 181 ms
4,376 KB
testcase_17 AC 210 ms
4,376 KB
testcase_18 AC 156 ms
4,380 KB
testcase_19 AC 189 ms
4,376 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 14 ms
4,376 KB
testcase_22 AC 18 ms
4,376 KB
testcase_23 AC 180 ms
4,380 KB
testcase_24 AC 214 ms
4,380 KB
testcase_25 AC 19 ms
4,380 KB
testcase_26 AC 27 ms
4,384 KB
testcase_27 AC 180 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


template<size_t N,typename R>
struct SquareMatrix{
  typedef array<R, N> arr;
  typedef array<arr, N> mat;
  mat dat;

  SquareMatrix(){
    for(size_t i=0;i<N;i++)
      for(size_t j=0;j<N;j++)
        dat[i][j]=R::add_identity();
  }
  SquareMatrix& operator=(const SquareMatrix& a){
    dat=a.dat;
    return (*this);
  }
  bool operator==(const SquareMatrix& a) const{
    return dat==a.dat;
  }

  size_t size() const{return N;};
  arr& operator[](size_t k){return dat[k];};
  const arr& operator[](size_t k) const {return dat[k];};

  static SquareMatrix add_identity(){return SquareMatrix();}
  static SquareMatrix mul_identity(){
    SquareMatrix res;
    for(size_t i=0;i<N;i++) res[i][i]=R::mul_identity();
    return res;
  }

  SquareMatrix operator*(const SquareMatrix &B) const{
    SquareMatrix 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]=res[i][j]+(dat[i][k]*B[k][j]);
    return res;
  }

  SquareMatrix operator+(const SquareMatrix &B) const{
    SquareMatrix res;
    for(size_t i=0;i<N;i++)
      for(size_t j=0;j<N;j++)
        res[i][j]=dat[i][j]+B[i][j];
    return res;
  }

  SquareMatrix pow(long long n) const{
    SquareMatrix a=*this,res=mul_identity();
    while(n){
      if(n&1) res=res*a;
      a=a*a;
      n>>=1;
    }
    return res;
  }
};

//INSERT ABOVE HERE
signed main(){
  using M = Mint<int, 998244353>;
  using SM = SquareMatrix<125, M>;
  int n,d;
  cin>>n>>d;
  auto idx=
    [&](int i,int j,int k){
      i%=d;j%=d;k%=d;
      return (i*d+j)*d+k;
    };
  SM A;
  for(int i=0;i<d;i++){
    for(int j=0;j<d;j++){
      for(int k=0;k<d;k++){
        // ho
        {
          int ni=i+1,nj=j,nk=k;
          A[idx(ni,nj,nk)][idx(i,j,k)]+=M(1);
          //A[idx(i,j,k)][idx(ni,nj,nk)]+=M(1);
        }
        // mu
        {
          int ni=i,nj=j+i,nk=k;
          A[idx(ni,nj,nk)][idx(i,j,k)]+=M(1);
          //A[idx(i,j,k)][idx(ni,nj,nk)]+=M(1);
        }
        // ra
        {
          int ni=i,nj=j,nk=k+j;
          A[idx(ni,nj,nk)][idx(i,j,k)]+=M(1);
          //A[idx(i,j,k)][idx(ni,nj,nk)]+=M(1);
        }
      }
    }
  }

  SM B=A.pow(n);
  M ans{0};
  for(int i=0;i<d;i++)
    for(int j=0;j<d;j++)
      ans+=B[idx(i,j,0)][0];

  cout<<ans<<endl;
  return 0;
}
0