提出 #4060246
ソースコード 拡げる
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(kbrni,n)cout<<" "<<a[kbrni];cout<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define svecp(v) cout<<#v<<":";each(kbrni,v)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
using namespace std;
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
template<typename T> class mat : public vector<vector<T> > {
private:
int r,c; //行,列
public:
int row() const {
return r;
}
int column() const {
return c;
}
mat(int n,int m,T val = 0){
this->r = n,this->c = m;
rep(i,n){
this->push_back(vector<T>(m,val));
}
}
mat operator+(const mat& another){
if(this->r != another.r && this->c != another.c){
cout << "足し算失敗(サイズ不一致)" << endl;
exit(1);
}
mat<T> X(this->r,this->c);
rep(i,this->r){
rep(j,this->c){
X[i][j] = (*this)[i][j] + another[i][j];
}
}
return X;
}
mat operator+(const T val){
mat<T> X(this->r,this->c);
rep(i,this->r){
rep(j,this->c){
X[i][j] = (*this)[i][j] + val;
}
}
return X;
}
mat operator-(const mat& another){
if(this->r != another.r && this->c != another.c){
cout << "引き算失敗(サイズ不一致)" << endl;
exit(1);
}
mat<T> X(this->r,this->c);
rep(i,this->r){
rep(j,this->c){
X[i][j] = (*this)[i][j] - another[i][j];
}
}
return X;
}
mat operator-(const T val){
mat<T> X(this->r,this->c);
rep(i,this->r){
rep(j,this->c){
X[i][j] = (*this)[i][j] - val;
}
}
return X;
}
vector<T> operator*(const vector<T>& another){
if(this->c != (int)another.size()){
cout << "掛け算失敗(サイズ不一致)" << endl;
exit(1);
}
vector<T> vec(this->r,0);
rep(i,this->r){
rep(j,this->c){
vec[i] += (*this)[i][j] * another[j];
}
}
return vec;
}
mat operator*(const mat& another){
if(this->c != another.r){
cout << "掛け算失敗(サイズ不一致)" << endl;
exit(1);
}
mat<T> X(this->r,another.c);
rep(i,this->r){
rep(k,this->c){
rep(j,another.c){
X[i][j] += (*this)[i][k]*another[k][j];
}
}
}
return X;
}
mat operator-(){
mat<T> X(this->r,this->c);
rep(i,this->r){
rep(j,this->c){
X[i][j] = -(*this)[i][j];
}
}
return X;
}
void print(){
rep(i,this->r){
rep(j,(this->c)-1){
cout << (*this)[i][j] << ",";
}
cout << (*this)[i][(this->c)-1] << endl;
}
}
};
template<typename T> mat<T> pow(mat<T> A,ll cnt)
{
if(A.row() != A.column()){
cout << "累乗不可" << endl;
}
int n = A.row();
mat<T> B(n,n);
rep(i,n){
B[i][i] = 1;
}
while(cnt>0){
if(cnt & 1){
B = B*A;
}
A = A*A;
cnt >>= 1;
}
return B;
}
template<typename T> mat<T> mod_mul(mat<T>& A,mat<T>& B)
{
if(A.column() != B.row()){
cout << "掛け算失敗(サイズ不一致)" << endl;
exit(1);
}
mat<T> X(A.row(),B.column());
rep(i,A.row()){
rep(k,A.column()){
rep(j,B.column()){
X[i][j] = (X[i][j] + (ll)A[i][k] * B[k][j] % MOD) % MOD;
}
}
}
return X;
}
template<typename T> mat<T> mod_pow(mat<T> A,ll cnt)
{
if(A.row() != A.column()){
cout << "累乗不可" << endl;
}
int n = A.row();
mat<T> B(n,n);
rep(i,n){
B[i][i] = 1;
}
while(cnt>0){
if(cnt & 1){
B = mod_mul(B,A);
}
A = mod_mul(A,A);
cnt >>= 1;
}
return B;
}
inline int add(int x,int y)
{
return (x + y)%MOD;
}
inline int sub(int x,int y)
{
return (x+MOD-y)%MOD;
}
inline int mul(int x,int y)
{
return (ll)x*y%MOD;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n;
ll K;
cin >> n >> K;
mat<int> a(n,n);
rep(i,n){
rep(j,n){
cin >> a[i][j];
}
}
mat<int> b = mod_pow(a,K);
int ans = 0;
rep(i,n){
rep(j,n){
ans = add(ans, b[i][j]);
}
}
cout << ans << "\n";
}
提出情報
| 提出日時 |
|
| 問題 |
R - Walk |
| ユーザ |
kopricky |
| 言語 |
C++14 (GCC 5.4.1) |
| 得点 |
100 |
| コード長 |
5856 Byte |
| 結果 |
AC |
| 実行時間 |
56 ms |
| メモリ |
256 KiB |
ジャッジ結果
| セット名 |
All |
| 得点 / 配点 |
100 / 100 |
| 結果 |
|
| セット名 |
テストケース |
| All |
0_00, 0_01, 0_02, 0_03, 0_04, 1_00, 1_01, 1_02, 1_03, 1_04, 1_05, 1_06, 1_07, 1_08, 1_09, 1_10, 1_11, 1_12, 1_13, 1_14, 1_15, 1_16, 1_17 |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 0_00 |
AC |
1 ms |
256 KiB |
| 0_01 |
AC |
1 ms |
256 KiB |
| 0_02 |
AC |
1 ms |
256 KiB |
| 0_03 |
AC |
1 ms |
256 KiB |
| 0_04 |
AC |
2 ms |
256 KiB |
| 1_00 |
AC |
1 ms |
256 KiB |
| 1_01 |
AC |
1 ms |
256 KiB |
| 1_02 |
AC |
2 ms |
256 KiB |
| 1_03 |
AC |
41 ms |
256 KiB |
| 1_04 |
AC |
2 ms |
256 KiB |
| 1_05 |
AC |
41 ms |
256 KiB |
| 1_06 |
AC |
2 ms |
256 KiB |
| 1_07 |
AC |
56 ms |
256 KiB |
| 1_08 |
AC |
30 ms |
256 KiB |
| 1_09 |
AC |
41 ms |
256 KiB |
| 1_10 |
AC |
41 ms |
256 KiB |
| 1_11 |
AC |
38 ms |
256 KiB |
| 1_12 |
AC |
37 ms |
256 KiB |
| 1_13 |
AC |
46 ms |
256 KiB |
| 1_14 |
AC |
42 ms |
256 KiB |
| 1_15 |
AC |
43 ms |
256 KiB |
| 1_16 |
AC |
44 ms |
256 KiB |
| 1_17 |
AC |
43 ms |
256 KiB |