結果

問題 No.1660 Matrix Exponentiation
ユーザー yamakeyamake
提出日時 2021-08-27 22:05:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,843 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 180,096 KB
実行使用メモリ 813,824 KB
最終ジャッジ日時 2024-05-01 02:39:35
合計ジャッジ時間 4,725 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 379 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 MLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define debug(output) if(debugFlag)cout<<#output<<"= "<<output<<endl
using lint = long long;
typedef pair<int,int> P;
const bool debugFlag=true;
const lint linf=1e18+7;
const lint inf=1e9+7;
const int MOD=1000000007;
template<typename T>
class Matrix{
    public:
    int size;
    vector<vector<T>> mat;
    Matrix(int n){
        size=n;
        mat.resize(n,vector<T>(n,0));
    }
    void in(T value,int i,int j){
        mat[i][j]+=value;
    }
    T out(int i,int j){
        return mat[i][j];
    }
    Matrix<T> mul(Matrix<T> A,Matrix<T> B){
        int n=A.size;
        Matrix<T> res(n);
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                T sum=0;
                for(int k=0;k<n;++k){
                    sum+=A.out(i,k)*B.out(k,j);
                    sum%=MOD;
                }
                res.in(sum,i,j);
            }
        }
        return res;
    }
    Matrix<T> pow(long long k){
        Matrix<T> res(size);
        Matrix<T> A(*this);
        for(int i=0;i<size;++i)res.in(1,i,i);
        while(k>0){
            if(k&1)res=mul(res,A);
            k/=2;
            A=mul(A,A);
        }
        return res;
    }
    bool judge(){
      int n=size;
      rep(i,n)rep(j,n){
        if(mat[i][j])return false;
      }
      return true;
    }
};
signed main(){
  int n,k;cin>>n>>k;
  Matrix<lint> mat(n);
  rep(i,k){
    int a,b;cin>>a>>b;
    --a;--b;
    mat.in(1,a,b);
  }
  lint u=linf;
  auto a=mat.pow(u);
  if(!a.judge()){
    cout<<-1<<"\n";
    return 0;
  }
  if(k==0){
    cout<<0<<"\n";
    return 0;
  }
  lint d=0;
  while(u-d>1){
    lint mid=(u+d)/2;
    a=mat.pow(mid);
    if(a.judge())u=mid;
    else d=mid;
  }
  cout<<u<<"\n";
  return 0;
}
0