結果

問題 No.578 3 x N グリッド上のサイクルのサイズ(easy)
ユーザー mamekinmamekin
提出日時 2017-10-14 00:00:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 3,267 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 124,120 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 00:37:38
合計ジャッジ時間 3,977 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 7 ms
4,376 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 10 ms
4,376 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 12 ms
4,376 KB
testcase_24 AC 13 ms
4,376 KB
testcase_25 AC 14 ms
4,376 KB
testcase_26 AC 15 ms
4,376 KB
testcase_27 AC 17 ms
4,380 KB
testcase_28 AC 17 ms
4,376 KB
testcase_29 AC 18 ms
4,380 KB
testcase_30 AC 20 ms
4,380 KB
testcase_31 AC 20 ms
4,380 KB
testcase_32 AC 22 ms
4,376 KB
testcase_33 AC 23 ms
4,380 KB
testcase_34 AC 25 ms
4,376 KB
testcase_35 AC 26 ms
4,380 KB
testcase_36 AC 27 ms
4,376 KB
testcase_37 AC 28 ms
4,376 KB
testcase_38 AC 31 ms
4,380 KB
testcase_39 AC 32 ms
4,380 KB
testcase_40 AC 33 ms
4,376 KB
testcase_41 AC 35 ms
4,376 KB
testcase_42 AC 38 ms
4,376 KB
testcase_43 AC 39 ms
4,376 KB
testcase_44 AC 41 ms
4,376 KB
testcase_45 AC 43 ms
4,376 KB
testcase_46 AC 44 ms
4,380 KB
testcase_47 AC 47 ms
4,376 KB
testcase_48 AC 47 ms
4,380 KB
testcase_49 AC 50 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;
const char EMPTY = '_';

string normalizeState(const string& s)
{
    char newChar = 'A';
    map<int, int> to;
    to[EMPTY] = EMPTY;

    string t;
    for(char c : s){
        if(to.find(c) == to.end()){
            to[c] = newChar;
            ++ newChar;
        }
        t.push_back(to[c]);
    }
    return t;
}

int main()
{
    int n;
    cin >> n;

    map<pair<string, int>, long long> dp;
    dp[make_pair("____", 0)] = 1;
    long long ans = 0;
    for(int y=0; y<=n; ++y){
        map<pair<string, int>, long long> nextDp;
        for(const auto& p : dp){
            string s = p.first.first;
            bitset<4> upper;
            for(int i=0; i<4; ++i)
                upper[i] = (s[i] != EMPTY);

            for(int i=0; i<(1<<3); ++i){
                bitset<5> side(i << 1);
                bool ng = false;
                int cnt = p.first.second;
                for(int j=0; j<4; ++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<4; ++j){
                    if(!side[j]){
                        int k = j;
                        while(side[k+1])
                            ++ k;

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

                if(t == "____" && cnt > 0){
                    ans += cnt * p.second;
                    ans %= MOD;
                }
                else{
                    t = normalizeState(t);
                    nextDp[make_pair(t, cnt)] += p.second;
                    nextDp[make_pair(t, cnt)] %= MOD;
                }
            }
        }
        dp.swap(nextDp);
    }
    cout << ans << endl;

    return 0;
}
0