結果

問題 No.572 妖精の演奏
ユーザー beetbeet
提出日時 2018-12-02 11:03:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,878 bytes
コンパイル時間 3,174 ms
コンパイル使用メモリ 205,776 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 22:39:50
合計ジャッジ時間 3,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 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 K>
struct Matrix{
  typedef vector<K> arr;
  typedef vector<arr> mat;
  mat dat;

  Matrix(size_t r,size_t c):dat(r,arr(c,K())){}
  Matrix(mat dat):dat(dat){}

  size_t size() const{return dat.size();};
  bool empty() const{return size()==0;};
  arr& operator[](size_t k){return dat[k];};
  const arr& operator[](size_t k) const {return dat[k];};
  
  static Matrix cross(const Matrix &A,const Matrix &B){
    Matrix res(A.size(),B[0].size());    
    for(Int i=0;i<(Int)A.size();i++)
      for(Int j=0;j<(Int)B[0].size();j++)
        for(Int k=0;k<(Int)B.size();k++)
          res[i][j]+=A[i][k]*B[k][j];
    return res;
  }

  static Matrix identity(size_t n){
    Matrix res(n,n);
    for(Int i=0;i<(Int)n;i++) res[i][i]=K(1);
    return res;
  }
  
  Matrix pow(long long n) const{
    Matrix a(dat),res=identity(size());
    while(n){
      if(n&1) res=cross(res,a);
      a=cross(a,a);      
      n>>=1;
    }
    return res;
  }
};

const Int INF = 1e15;
struct R{
  Int v;
  R():v(-INF){}
  R(Int x){if(x==1) v=0;}
  R operator+=(const R &a){
    v=max(v,a.v);
    return *this;
  }
  R operator*=(const R &a){
    v=v+a.v;
    return *this;
  }
  R operator+(const R &a) const{
    R b(*this);
    return b+=a;
  }
  R operator*(const R &a) const{
    R b(*this);
    return b*=a;
  }
};

//INSERT ABOVE HERE
signed main(){
  using M = Matrix<R>;
  Int n,m;
  cin>>n>>m;
  M A(m,m);
  for(Int i=0;i<m;i++)
    for(Int j=0;j<m;j++)
      cin>>A[i][j].v;
  M B(m,1);
  for(Int i=0;i<m;i++) B[i][0].v=0;

  M C=M::cross(A.pow(n-1),B);

  Int res=0;
  for(Int i=0;i<m;i++) chmax(res,C[i][0].v);
  cout<<res<<endl;
  return 0;
}
0