結果

問題 No.569 3 x N グリッドのパスの数
ユーザー btkbtk
提出日時 2017-09-14 16:10:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,050 bytes
コンパイル時間 2,880 ms
コンパイル使用メモリ 197,620 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 09:17:41
合計ジャッジ時間 4,712 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 2 ms
5,376 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 2 ms
5,376 KB
testcase_58 AC 2 ms
5,376 KB
testcase_59 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define CIN_ONLY if(1)
struct cww{cww(){
    CIN_ONLY{
        ios::sync_with_stdio(false);cin.tie(0);
    }
}}star;
#ifdef BTK
#define DEBUG if(1)
#else
#define DEBUG if(0)
#endif

#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l,T r)
{bool a=l>r;if(a)l=r;return a;}
template <typename T>inline bool chmax(T &l,T r)
{bool a=l<r;if(a)l=r;return a;}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
    for(auto &it:v)is>>it;
    return is;
}

#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)


typedef LL D;
#define REP4(i,n) for(int i=0;i<n;i+=4)
#define S 12
int sz=S;
struct M{
    D v[S][S];
    D& operator()(int i,int j){
        return v[i][j];
    }
}mat[64];
const int mod=1e9+7;
void mat_mul(M &a,M& b,M &c){
    REP(i,sz)REP(j,sz)c(i,j)=0;

    D sum[4],tmp;
    REP(k,sz){
        REP4(i,sz){
            sum[0]=a(i,k);
            sum[1]=a(i+1,k);
            sum[2]=a(i+2,k);
            sum[3]=a(i+3,k);
            REP(j,sz){
                tmp=b(k,j);
                c(i,j)+=sum[0]*tmp%mod;
                c(i+1,j)+=sum[1]*tmp%mod;
                c(i+2,j)+=sum[2]*tmp%mod;
                c(i+3,j)+=sum[3]*tmp%mod;
            }
        }
    }
    REP(i,sz)REP(j,sz)c(i,j)%=mod;
}


map<string,int> z;
map<string,vector<string>> g;
map<string,vector<string>> rg;
inline string rev(string s){
    reverse(ALL(s));
    return s;
}
int M=0;
void init(){
    g["0123"]={"0123","1023","0001"};
    g["1023"]={"1023","0001","0123","1203"};
    g["1203"]={"1203","1023","1230","0001","0010"};
    g["1230"]={"1230","1203","0010","0001"};
    g["1000"]={"1000","0100","0010","0001","1230","1203","1023","0123"};
    g["0100"]={"1000","0100","0010","0001","0123","1023"};
    for(auto &it:g){
        vector<string> x;
        for(auto s:it.se)x.pb(rev(s));
        rg[rev(it.fi)]=x;
    }
    for(auto &it:g)z[it.fi]=M++;
    for(auto &it:rg)z[it.fi]=M++;
    for(auto &it:g){
        int v=z[it.fi];
        for(auto &jt:it.se){
            int u=z[jt];
            mat[0](v,u)=1;
        }
    }
    for(auto &it:rg){
        int v=z[it.fi];
        for(auto &jt:it.se){
            int u=z[jt];
            mat[0](v,u)=1;
        }
    }

    REP(i,61)mat_mul(mat[i],mat[i],mat[i+1]);
}

LL calc_ret(vector<LL> v){
    LL ret=0;
    ret+=v[z["0001"]];
    return ret%mod;

}

int main(){
    init();
    LL N;
    cin>>N;N++;
    LL ret=0;
    vector<LL> v(M,0);
    v[z["1000"]]=1;
    int id=0;
    while(N){
        vector<LL> nxt(M,0);
        if(N&1){
            REP(i,M)REP(j,M)
                nxt[j]+=v[i]*mat[id](i,j);
            REP(i,M)nxt[i]%=mod;
            swap(v,nxt);
        }
        id++;
        N>>=1;
    }
    
    cout<<calc_ret(v)<<endl;
    return 0;
}
0