結果

問題 No.621 3 x N グリッド上のドミノの置き方の数
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-21 23:34:03
言語 Java19
(openjdk 21)
結果
AC  
実行時間 102 ms / 3,000 ms
コード長 3,745 bytes
コンパイル時間 2,396 ms
コンパイル使用メモリ 74,784 KB
実行使用メモリ 51,944 KB
最終ジャッジ日時 2023-08-22 21:05:05
合計ジャッジ時間 11,291 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
51,528 KB
testcase_01 AC 86 ms
51,456 KB
testcase_02 AC 86 ms
51,472 KB
testcase_03 AC 84 ms
51,536 KB
testcase_04 AC 86 ms
51,416 KB
testcase_05 AC 84 ms
51,564 KB
testcase_06 AC 85 ms
51,904 KB
testcase_07 AC 85 ms
51,936 KB
testcase_08 AC 86 ms
51,488 KB
testcase_09 AC 85 ms
51,604 KB
testcase_10 AC 89 ms
51,584 KB
testcase_11 AC 87 ms
51,576 KB
testcase_12 AC 90 ms
51,376 KB
testcase_13 AC 86 ms
51,320 KB
testcase_14 AC 88 ms
51,336 KB
testcase_15 AC 88 ms
51,384 KB
testcase_16 AC 85 ms
51,472 KB
testcase_17 AC 91 ms
51,568 KB
testcase_18 AC 89 ms
51,860 KB
testcase_19 AC 85 ms
51,664 KB
testcase_20 AC 87 ms
51,424 KB
testcase_21 AC 86 ms
51,872 KB
testcase_22 AC 89 ms
51,628 KB
testcase_23 AC 87 ms
51,476 KB
testcase_24 AC 86 ms
51,428 KB
testcase_25 AC 87 ms
51,456 KB
testcase_26 AC 86 ms
51,392 KB
testcase_27 AC 88 ms
51,416 KB
testcase_28 AC 86 ms
51,340 KB
testcase_29 AC 85 ms
51,472 KB
testcase_30 AC 86 ms
51,544 KB
testcase_31 AC 89 ms
51,624 KB
testcase_32 AC 90 ms
51,724 KB
testcase_33 AC 86 ms
51,420 KB
testcase_34 AC 88 ms
51,568 KB
testcase_35 AC 85 ms
51,428 KB
testcase_36 AC 89 ms
51,480 KB
testcase_37 AC 86 ms
51,324 KB
testcase_38 AC 89 ms
51,756 KB
testcase_39 AC 87 ms
51,704 KB
testcase_40 AC 90 ms
51,396 KB
testcase_41 AC 88 ms
51,288 KB
testcase_42 AC 88 ms
51,944 KB
testcase_43 AC 88 ms
51,476 KB
testcase_44 AC 87 ms
51,472 KB
testcase_45 AC 87 ms
51,460 KB
testcase_46 AC 88 ms
51,588 KB
testcase_47 AC 102 ms
51,804 KB
testcase_48 AC 99 ms
51,504 KB
testcase_49 AC 87 ms
51,560 KB
testcase_50 AC 89 ms
51,464 KB
testcase_51 AC 85 ms
51,420 KB
testcase_52 AC 85 ms
51,428 KB
testcase_53 AC 88 ms
51,428 KB
testcase_54 AC 86 ms
51,444 KB
testcase_55 AC 88 ms
51,656 KB
testcase_56 AC 89 ms
51,324 KB
testcase_57 AC 89 ms
51,392 KB
testcase_58 AC 87 ms
51,592 KB
testcase_59 AC 87 ms
51,336 KB
testcase_60 AC 88 ms
51,344 KB
testcase_61 AC 88 ms
51,468 KB
testcase_62 AC 93 ms
51,500 KB
testcase_63 AC 88 ms
51,324 KB
testcase_64 AC 90 ms
51,416 KB
testcase_65 AC 85 ms
51,488 KB
testcase_66 AC 86 ms
51,280 KB
testcase_67 AC 87 ms
51,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    static final long MOD=1000000007;
    static long[][]matMul(long[][]a,long[][]b){
        int n=a.length;
        int m=b.length;
        int l=b[0].length;
        long[][]ret=new long[n][l];
        for(int i=0;i<n;++i)
            for(int j=0;j<m;++j)
                for(int k=0;k<l;++k)
                    ret[i][k]=(ret[i][k]+a[i][j]*b[j][k])%MOD;
        return ret;
    }
    static long[][] powerMat(long[][]x, long exponent) {
        int n=x.length;
	long[][] prod = new long[n][n];
        for(int i=0;i<n;++i)prod[i][i]=1;
	for (int i = 63; i >= 0; --i) {
	    prod = matMul(prod,prod);
	    if ((exponent & 1L << i) != 0) {
		prod =matMul(prod,x);
	    }
	}
	return prod;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        long n=sc.nextLong();
        long[][]trans=new long[27][27];
        int valid=0x60b;
        for(int i=0;i<27;++i){
            int[]a=new int[3];
            a[0]=i%3;
            a[1]=i/3%3;
            a[2]=i/9;
            for(int j=0;j<8;++j){
                boolean ok=true;
                for(int k=0;k<3;++k){
                    if((j&1<<k)!=0)
                        ok&=a[k]>=1;
                    else
                        ok&=a[k]<=1;
                }
                if(!ok)continue;
                int pb=j;
                for(int k=0;k<3;++k)
                    if(a[k]==0){
                        if((pb&(1<<k))!=0)throw new Error();
                        pb|=1<<k;
                    }
                if((pb&3)==0||(pb&6)==0)continue;
                int cb=j;
                int nxt=0;
                int[]pw={1,3,9};
                for(int k=0;k<3;++k)
                    if((cb&1<<k)==0){
                        if(a[k]>=2)throw new Error();
                        nxt+=(a[k]+1)*pw[k];
                    }
                trans[i][nxt]+=1;
                if((j&3)==0){
                    int pn=(j&1<<2)==0?(a[2]+1)*9:0;
                    trans[i][pn]+=1;
                }
                if((j&6)==0){
                    int pn=(j&1<<0)==0?(a[0]+1)*1:0;
                    trans[i][pn]+=1;
                }
            }
        }
        long[][]pw=powerMat(trans,n);
        long ans=0;
        for(int i=0;i<27;++i)
            if((valid&1<<i)!=0)
                ans=(ans+pw[0][i])%MOD;
        out.println(ans);
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
0