結果
| 問題 |
No.1660 Matrix Exponentiation
|
| コンテスト | |
| ユーザー |
yamake
|
| 提出日時 | 2021-08-27 22:05:16 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,843 bytes |
| コンパイル時間 | 2,031 ms |
| コンパイル使用メモリ | 178,756 KB |
| 実行使用メモリ | 853,504 KB |
| 最終ジャッジ日時 | 2024-11-21 02:29:40 |
| 合計ジャッジ時間 | 34,350 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 8 TLE * 5 MLE * 14 |
ソースコード
#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;
}
yamake