結果

問題 No.569 3 x N グリッドのパスの数
ユーザー 👑 kmjpkmjp
提出日時 2017-09-09 00:51:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 2,937 bytes
コンパイル時間 1,929 ms
コンパイル使用メモリ 160,640 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 02:59:36
合計ジャッジ時間 11,198 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
5,248 KB
testcase_01 AC 12 ms
5,376 KB
testcase_02 AC 13 ms
5,376 KB
testcase_03 AC 15 ms
5,376 KB
testcase_04 AC 15 ms
5,376 KB
testcase_05 AC 17 ms
5,376 KB
testcase_06 AC 15 ms
5,376 KB
testcase_07 AC 17 ms
5,376 KB
testcase_08 AC 17 ms
5,376 KB
testcase_09 AC 19 ms
5,376 KB
testcase_10 AC 18 ms
5,376 KB
testcase_11 AC 19 ms
5,376 KB
testcase_12 AC 19 ms
5,376 KB
testcase_13 AC 22 ms
5,376 KB
testcase_14 AC 17 ms
5,376 KB
testcase_15 AC 20 ms
5,376 KB
testcase_16 AC 19 ms
5,376 KB
testcase_17 AC 21 ms
5,376 KB
testcase_18 AC 19 ms
5,376 KB
testcase_19 AC 22 ms
5,376 KB
testcase_20 AC 109 ms
5,376 KB
testcase_21 AC 112 ms
5,376 KB
testcase_22 AC 111 ms
5,376 KB
testcase_23 AC 112 ms
5,376 KB
testcase_24 AC 112 ms
5,376 KB
testcase_25 AC 115 ms
5,376 KB
testcase_26 AC 110 ms
5,376 KB
testcase_27 AC 111 ms
5,376 KB
testcase_28 AC 112 ms
5,376 KB
testcase_29 AC 115 ms
5,376 KB
testcase_30 AC 112 ms
5,376 KB
testcase_31 AC 114 ms
5,376 KB
testcase_32 AC 114 ms
5,376 KB
testcase_33 AC 117 ms
5,376 KB
testcase_34 AC 109 ms
5,376 KB
testcase_35 AC 111 ms
5,376 KB
testcase_36 AC 114 ms
5,376 KB
testcase_37 AC 115 ms
5,376 KB
testcase_38 AC 113 ms
5,376 KB
testcase_39 AC 115 ms
5,376 KB
testcase_40 AC 212 ms
5,376 KB
testcase_41 AC 209 ms
5,376 KB
testcase_42 AC 249 ms
5,376 KB
testcase_43 AC 246 ms
5,376 KB
testcase_44 AC 249 ms
5,376 KB
testcase_45 AC 246 ms
5,376 KB
testcase_46 AC 250 ms
5,376 KB
testcase_47 AC 245 ms
5,376 KB
testcase_48 AC 245 ms
5,376 KB
testcase_49 AC 241 ms
5,376 KB
testcase_50 AC 247 ms
5,376 KB
testcase_51 AC 245 ms
5,376 KB
testcase_52 AC 247 ms
5,376 KB
testcase_53 AC 244 ms
5,376 KB
testcase_54 AC 248 ms
5,376 KB
testcase_55 AC 247 ms
5,376 KB
testcase_56 AC 243 ms
5,376 KB
testcase_57 AC 240 ms
5,376 KB
testcase_58 AC 249 ms
5,376 KB
testcase_59 AC 246 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll N;
ll mo=1000000007;

const int MAT=5*5*5;
struct Mat { ll v[MAT][MAT]; };
Mat mulmat(Mat& a,Mat& b,int n=MAT) {
	ll mo2=4*mo*mo;
	int x,y,z; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(x,n) FOR(z,n) FOR(y,n) {
		r.v[x][y] += a.v[x][z]*b.v[z][y];
		if(r.v[x][y]>mo2) r.v[x][y] -= mo2;
	}
	FOR(x,n) FOR(y,n) r.v[x][y]%=mo;
	return r;
}

Mat powmat(ll p,Mat a,int n=MAT) {
	int i,x,y; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(i,n) r.v[i][i]=1;
	while(p) {
		if(p%2) r=mulmat(r,a,n);
		a=mulmat(a,a,n);
		p>>=1;
	}
	return r;
}

int ID(int a2,int a1,int a0) { return a2*5*5+a1*5+a0;}

Mat pat;
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	
	FOR(y,MAT) FOR(x,MAT) pat.v[y][x]=0;
	
	// 1 to 1
	FOR(x,4) FOR(y,4) pat.v[ID(x,4,4)][ID(y,4,4)]=1;
	// 3 to 1
	pat.v[ID(0,1,2)][ID(2,4,4)]=1;
	pat.v[ID(0,1,2)][ID(3,4,4)]=1;
	pat.v[ID(0,1,3)][ID(2,4,4)]=1;
	pat.v[ID(0,1,3)][ID(3,4,4)]=1;
	pat.v[ID(0,2,3)][ID(3,4,4)]=1;
	pat.v[ID(1,2,3)][ID(3,4,4)]=1;
	pat.v[ID(2,1,0)][ID(0,4,4)]=1;
	pat.v[ID(3,1,0)][ID(0,4,4)]=1;
	pat.v[ID(3,2,0)][ID(0,4,4)]=1;
	pat.v[ID(3,2,1)][ID(0,4,4)]=1;
	pat.v[ID(3,2,1)][ID(1,4,4)]=1;
	pat.v[ID(3,2,0)][ID(1,4,4)]=1;
	// 1 to 3
	pat.v[ID(0,4,4)][ID(0,1,2)]=1;
	pat.v[ID(0,4,4)][ID(0,1,3)]=1;
	pat.v[ID(0,4,4)][ID(0,2,3)]=1;
	pat.v[ID(0,4,4)][ID(1,2,3)]=1;
	pat.v[ID(1,4,4)][ID(0,2,3)]=1;
	pat.v[ID(1,4,4)][ID(1,2,3)]=1;
	pat.v[ID(2,4,4)][ID(3,1,0)]=1;
	pat.v[ID(2,4,4)][ID(2,1,0)]=1;
	pat.v[ID(3,4,4)][ID(3,1,0)]=1;
	pat.v[ID(3,4,4)][ID(2,1,0)]=1;
	pat.v[ID(3,4,4)][ID(3,2,0)]=1;
	pat.v[ID(3,4,4)][ID(3,2,1)]=1;
	// 3 to 3
	pat.v[ID(0,1,2)][ID(0,1,2)]=1;
	pat.v[ID(0,1,3)][ID(0,1,3)]=1;
	pat.v[ID(0,2,3)][ID(0,2,3)]=1;
	pat.v[ID(1,2,3)][ID(1,2,3)]=1;
	
	pat.v[ID(0,1,2)][ID(0,1,3)]=1;
	pat.v[ID(0,1,3)][ID(0,1,2)]=1;
	pat.v[ID(0,1,3)][ID(0,2,3)]=1;
	pat.v[ID(0,2,3)][ID(0,1,3)]=1;
	pat.v[ID(0,2,3)][ID(1,2,3)]=1;
	pat.v[ID(1,2,3)][ID(0,2,3)]=1;
	
	pat.v[ID(2,1,0)][ID(2,1,0)]=1;
	pat.v[ID(3,1,0)][ID(3,1,0)]=1;
	pat.v[ID(3,2,0)][ID(3,2,0)]=1;
	pat.v[ID(3,2,1)][ID(3,2,1)]=1;
	
	pat.v[ID(2,1,0)][ID(3,1,0)]=1;
	pat.v[ID(3,1,0)][ID(2,1,0)]=1;
	pat.v[ID(3,2,0)][ID(3,1,0)]=1;
	pat.v[ID(3,1,0)][ID(3,2,0)]=1;
	pat.v[ID(3,2,0)][ID(3,2,1)]=1;
	pat.v[ID(3,2,1)][ID(3,2,0)]=1;
	
	cin>>N;
	cout<<powmat(N+1,pat).v[ID(3,4,4)][ID(0,4,4)]<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0