結果

問題 No.579 3 x N グリッド上のサイクルのサイズ(hard)
ユーザー mamekinmamekin
提出日時 2017-10-14 13:51:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 5,216 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 132,864 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 02:12:13
合計ジャッジ時間 5,209 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

const int MOD = 1000000007;

template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
    int a = x.size();
    int b = x[0].size();
    int c = y[0].size();
    vector<vector<T> > z(a, vector<T>(c, 0));
    for(int i=0; i<a; ++i){
        for(int j=0; j<c; ++j){
            for(int k=0; k<b; ++k){
                z[i][j] += x[i][k] * y[k][j];
                z[i][j] %= MOD;
            }
        }
    }
    return z;
}

template <class T>
vector<vector<T> > matrixPower(const vector<vector<T> >& x, long long k)
{
    int n = x.size();
    vector<vector<T> > y(n, vector<T>(n, 0));
    for(int i=0; i<n; ++i)
        y[i][i] = 1; // 積の単位元

    vector<vector<T> > z = x;
    while(k > 0){
        if(k & 1)
            y = matrixProduct(y, z);
        z = matrixProduct(z, z);
        k >>= 1;
    }
    return y;
}

string normalizeState(string s)
{
    char newChar = 'A';
    map<int, int> to;
    to['.'] = '.';
    
    int n = s.size();
    string ans(n, '.');
    for(int i=0; i<2; ++i){
        string t(n, '.');
        for(int j=0; j<n; ++j){
            if(to.find(s[j]) == to.end()){
                to[s[j]] = newChar;
                ++ newChar;
            }
            t[j] = to[s[j]];
        }
        ans = max(ans, t);
        reverse(s.begin(), s.end());
    }
    return ans;
}

void makeState(int len, vector<string>& state)
{
    vector<set<string> > nonEmptyState(len+1);
    nonEmptyState[0].insert("");
    for(int i=0; i<len; i+=2){
        for(const string s : nonEmptyState[i]){
            string t1 = normalizeState(s + "ZZ");
            string t2 = normalizeState('Z' + s + 'Z');
            nonEmptyState[i+2].insert(t1);
            nonEmptyState[i+2].insert(t2);
        }
    }

    set<string> preState;
    for(int i=0; i<(1<<len); ++i){
        bitset<32> bs(i);
        for(const string s : nonEmptyState[bs.count()]){
            int k = 0;
            string t(len, '.');
            for(int j=0; j<len; ++j){
                if(bs[j]){
                    t[j] = s[k];
                    ++ k;
                }
            }
            preState.insert(normalizeState(t));
        }
    }

    state = vector<string>(preState.begin(), preState.end());
}

long long solve(long long n, int len)
{
    vector<string> state;
    makeState(len, state);
    int stateNum = state.size();

    vector<vector<long long> > mat(stateNum*2+1, vector<long long>(stateNum*2+1, 0));
    mat[stateNum*2][stateNum*2] = 1;
    for(int a=0; a<stateNum; ++a){
        const string& s = state[a];
        bitset<32> upper;
        for(int i=0; i<len; ++i)
            upper[i] = (s[i] != '.');

        for(int i=0; i<(1<<(len-1)); ++i){
            bitset<32> side(i << 1);
            bool ng = false;
            int cnt = 0;
            for(int j=0; j<len; ++j){
                if(upper[j] && side[j] && side[j+1])
                    ng = true;
                if(upper[j] || side[j] || side[j+1])
                    ++ cnt;
            }
            if(ng)
                continue;

            string t = s;
            int sameCnt = 0;
            char newChar = 'Z';
            for(int j=0; j<len; ++j){
                if(!side[j]){
                    int k = j;
                    while(side[k+1])
                        ++ k;

                    if(j < k){
                        if(s[j] == '.' && s[k] == '.'){
                            t[j] = t[k] = newChar;
                            -- newChar;
                        }
                        else if(s[j] == '.' || s[k] == '.'){
                            swap(t[j], t[k]);
                        }
                        else if(s[j] == s[k]){
                            t[j] = t[k] = '.';
                            ++ sameCnt;
                        }
                        else{
                            replace(t.begin(), t.end(), s[j], s[k]);
                            t[j] = t[k] = '.';
                        }
                    }
                }
            }
            if(sameCnt >= 2 || (sameCnt == 1 && t != string(len, '.')))
                continue;

            if(t == string(len, '.') && cnt > 0){
                ++ mat[stateNum*2][a+stateNum];
                mat[stateNum*2][a] += cnt;
            }
            else{
                t = normalizeState(t);
                int b = find(state.begin(), state.end(), t) - state.begin();
                ++ mat[b][a];
                ++ mat[b+stateNum][a+stateNum];
                mat[b+stateNum][a] += cnt;
            }
        }
    }

    mat = matrixPower(mat, n+1);
    return mat[stateNum*2][0];
}

int main()
{
    long long n;
    cin >> n;
    cout << solve(n, 4) << endl;

    return 0;
}
0