結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー chocoruskchocorusk
提出日時 2020-02-16 06:55:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 3,000 ms
コード長 3,207 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 129,908 KB
実行使用メモリ 6,780 KB
最終ジャッジ日時 2024-04-16 03:29:03
合計ジャッジ時間 5,534 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
6,732 KB
testcase_01 AC 21 ms
6,480 KB
testcase_02 AC 20 ms
6,728 KB
testcase_03 AC 20 ms
6,732 KB
testcase_04 AC 20 ms
6,604 KB
testcase_05 AC 20 ms
6,604 KB
testcase_06 AC 20 ms
6,480 KB
testcase_07 AC 21 ms
6,476 KB
testcase_08 AC 20 ms
6,728 KB
testcase_09 AC 21 ms
6,600 KB
testcase_10 AC 21 ms
6,732 KB
testcase_11 AC 21 ms
6,480 KB
testcase_12 AC 21 ms
6,736 KB
testcase_13 AC 21 ms
6,608 KB
testcase_14 AC 22 ms
6,608 KB
testcase_15 AC 22 ms
6,480 KB
testcase_16 AC 23 ms
6,736 KB
testcase_17 AC 23 ms
6,604 KB
testcase_18 AC 24 ms
6,736 KB
testcase_19 AC 25 ms
6,732 KB
testcase_20 AC 25 ms
6,736 KB
testcase_21 AC 25 ms
6,728 KB
testcase_22 AC 26 ms
6,728 KB
testcase_23 AC 26 ms
6,600 KB
testcase_24 AC 26 ms
6,604 KB
testcase_25 AC 28 ms
6,476 KB
testcase_26 AC 28 ms
6,604 KB
testcase_27 AC 29 ms
6,476 KB
testcase_28 AC 28 ms
6,476 KB
testcase_29 AC 29 ms
6,608 KB
testcase_30 AC 29 ms
6,604 KB
testcase_31 AC 29 ms
6,608 KB
testcase_32 AC 28 ms
6,728 KB
testcase_33 AC 29 ms
6,476 KB
testcase_34 AC 29 ms
6,480 KB
testcase_35 AC 28 ms
6,604 KB
testcase_36 AC 29 ms
6,476 KB
testcase_37 AC 29 ms
6,732 KB
testcase_38 AC 30 ms
6,480 KB
testcase_39 AC 30 ms
6,604 KB
testcase_40 AC 30 ms
6,736 KB
testcase_41 AC 30 ms
6,732 KB
testcase_42 AC 30 ms
6,736 KB
testcase_43 AC 30 ms
6,732 KB
testcase_44 AC 29 ms
6,732 KB
testcase_45 AC 30 ms
6,476 KB
testcase_46 AC 30 ms
6,608 KB
testcase_47 AC 30 ms
6,604 KB
testcase_48 AC 30 ms
6,732 KB
testcase_49 AC 30 ms
6,728 KB
testcase_50 AC 31 ms
6,472 KB
testcase_51 AC 30 ms
6,480 KB
testcase_52 AC 30 ms
6,608 KB
testcase_53 AC 31 ms
6,476 KB
testcase_54 AC 30 ms
6,732 KB
testcase_55 AC 30 ms
6,600 KB
testcase_56 AC 30 ms
6,476 KB
testcase_57 AC 29 ms
6,604 KB
testcase_58 AC 30 ms
6,604 KB
testcase_59 AC 31 ms
6,476 KB
testcase_60 AC 31 ms
6,480 KB
testcase_61 AC 31 ms
6,480 KB
testcase_62 AC 31 ms
6,604 KB
testcase_63 AC 32 ms
6,476 KB
testcase_64 AC 26 ms
6,732 KB
testcase_65 AC 26 ms
6,780 KB
testcase_66 AC 26 ms
6,728 KB
testcase_67 AC 26 ms
6,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int a[3][4];
vector<vector<int>> v;
void dfs(int k){
    bool myon=0;
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            if(!a[i][j] && !a[i][j+1]){
                a[i][j]=a[i][j+1]=k;
                dfs(k+1);
                a[i][j]=a[i][j+1]=0;
                myon=1;
            }
        }
    }
    for(int i=0; i<4; i++){
        for(int j=0; j<2; j++){
            if(!a[j][i] && !a[j+1][i]){
                a[j][i]=a[j+1][i]=k;
                dfs(k+1);
                a[j][i]=a[j+1][i]=0;
                myon=1;
            }
        }
    }
    if(myon) return;
    vector<int> w(3);
    for(int j=0; j<4; j++){
        vector<int> w(3);
        if(a[0][j]==a[1][j] && a[1][j]!=0) w[0]=w[1]=3;
        else if(a[1][j]==a[2][j] && a[1][j]!=0) w[1]=w[2]=3;
        for(int i=0; i<3; i++){
            if(a[i][j]!=0 && w[i]!=3){
                if(j>0 && a[i][j-1]==a[i][j]) w[i]=1;
                else w[i]=2;
            }
        }
        v.push_back(w);
    }
}
const ll MOD=1e9+7;
vector<vector<ll>> matrixmul(int l, int m, int n, vector<vector<ll>> a, vector<vector<ll>> b){
	vector<vector<ll>> c(l, vector<ll>(n));
	for(int i=0; i<l; i++){
		for(int j=0; j<n; j++){
			for(int k=0; k<m; k++){
				(c[i][j]+=a[i][k]*b[k][j])%=MOD;
			}
		}
	}
	return c;
}
vector<vector<ll>> matrixpow(int n, vector<vector<ll>> a, ll k){
	vector<vector<ll>> ap=a, ans(n, vector<ll>(n));
	for(int i=0; i<n; i++) ans[i][i]=1;
	while(k){
		if(k&1) ans=matrixmul(n, n, n, ap, ans);
		ap=matrixmul(n, n, n, ap, ap);
		k>>=1;
	}
	return ans;
}
int main()
{
    ll n;
    cin>>n;
    dfs(1);
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    int m=v.size();
    vector<vector<ll>> mat(m, vector<ll>(m));
    for(int i=0; i<m; i++){
        for(int j=0; j<m; j++){
            bool dame=0;
            for(int k=0; k<3; k++){
                if(v[i][k]==2 && v[j][k]!=1){
                    dame=1;
                }
                if(v[j][k]==1 && v[i][k]!=2){
                    dame=1;
                }
                if(!v[i][k] && !v[j][k]) dame=1;
            }
            if(dame) continue;
            mat[i][j]++;
        }
    }
    auto matp=matrixpow(m, mat, n-1);
    ll ans=0;
    for(int i=0; i<m; i++){
        bool dame=0;
        for(int k=0; k<3; k++){
            if(v[i][k]==1) dame=1;
        }
        if(dame) continue;
        for(int j=0; j<m; j++){
            bool dame1=0;
            for(int k=0; k<3; k++){
                if(v[j][k]==2) dame1=1;
            }
            if(dame1) continue;
            (ans+=matp[i][j])%=MOD;
        }
    }
    cout<<ans<<endl;
	return 0;
}
0