結果

問題 No.1516 simple 門松列 problem Re:MASTER
ユーザー beetbeet
提出日時 2021-05-21 21:43:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 547 ms / 6,000 ms
コード長 4,214 bytes
コンパイル時間 2,068 ms
コンパイル使用メモリ 203,552 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-31 14:58:42
合計ジャッジ時間 7,801 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
4,380 KB
testcase_01 AC 39 ms
4,380 KB
testcase_02 AC 237 ms
4,380 KB
testcase_03 AC 115 ms
4,380 KB
testcase_04 AC 87 ms
4,380 KB
testcase_05 AC 115 ms
4,380 KB
testcase_06 AC 105 ms
4,380 KB
testcase_07 AC 78 ms
4,376 KB
testcase_08 AC 115 ms
4,380 KB
testcase_09 AC 30 ms
4,380 KB
testcase_10 AC 49 ms
4,380 KB
testcase_11 AC 39 ms
4,380 KB
testcase_12 AC 40 ms
4,380 KB
testcase_13 AC 59 ms
4,376 KB
testcase_14 AC 473 ms
4,380 KB
testcase_15 AC 444 ms
4,380 KB
testcase_16 AC 474 ms
4,380 KB
testcase_17 AC 416 ms
4,376 KB
testcase_18 AC 303 ms
4,380 KB
testcase_19 AC 397 ms
4,380 KB
testcase_20 AC 547 ms
4,380 KB
testcase_21 AC 538 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

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> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T, T MOD = 1000000007>
struct Mint{
  inline 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 *this;}
  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;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


template<typename R, size_t N>
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();
  }

  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;
  }
};


template<typename T>
bool is_kado(T a,T b,T c){
  if(a==b or b==c or c==a) return 0;
  if(a<b and b>c) return 1;
  if(a>b and b<c) return 1;
  return 0;
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  const int T = 9;
  using M = Mint<int, 998244353>;
  using SM = SquareMatrix<M, T*T*2>;
  SM A=SM::add_identity();

  int n,k;
  cin>>n>>k;
  auto idx=[&](int sum,int x,int y){return sum*T*T+x*T+y;};
  for(int x=0;x<k;x++){
    for(int y=0;y<k;y++){
      for(int z=0;z<k;z++){
        if(is_kado(x,y,z)){
          A[idx(0,y,z)][idx(0,x,y)]+=M(1);
          A[idx(1,y,z)][idx(0,x,y)]+=M(z);
          A[idx(1,y,z)][idx(1,x,y)]+=M(1);
        }
      }
    }
  }

  vector<M> B(T*T*2,0);
  for(int x=0;x<k;x++)
    for(int y=0;y<k;y++)
      if(x!=y) B[idx(0,x,y)]+=M(1),B[idx(1,x,y)]+=M(x+y);

  // A^(n-2) B
  A=A.pow(n-2);
  {
    M ans{0};
    for(int i=0;i<T*T;i++)
      for(int j=0;j<T*T*2;j++)
        ans+=A[i][j]*B[j];
    cout<<ans<<' ';
  }
  {
    M ans{0};
    for(int i=0;i<T*T;i++)
      for(int j=0;j<T*T*2;j++)
        ans+=A[T*T+i][j]*B[j];
    cout<<ans<<newl;
  }
  return 0;
}
0