結果

問題 No.663 セルオートマトンの逆操作
ユーザー はまやんはまやん
提出日時 2018-03-10 21:42:57
言語 C++14
(gcc 9.3.0)
結果
AC  
実行時間 3 ms
コード長 3,957 Byte
コンパイル時間 1,659 ms
使用メモリ 6,148 KB
最終ジャッジ日時 2020-03-07 16:21:29

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
small1.txt AC 3 ms
5,380 KB
small2.txt AC 3 ms
5,384 KB
small3.txt AC 3 ms
5,380 KB
small4.txt AC 2 ms
5,384 KB
small5.txt AC 3 ms
5,384 KB
small6.txt AC 3 ms
5,376 KB
small7.txt AC 3 ms
5,380 KB
small8.txt AC 3 ms
5,380 KB
small9.txt AC 3 ms
5,380 KB
small10.txt AC 2 ms
5,376 KB
small11.txt AC 2 ms
5,380 KB
small12.txt AC 2 ms
5,380 KB
small13.txt AC 3 ms
5,380 KB
small14.txt AC 3 ms
5,380 KB
small15.txt AC 3 ms
5,376 KB
small16.txt AC 3 ms
5,376 KB
test1.txt AC 2 ms
5,380 KB
test2.txt AC 2 ms
5,380 KB
test3.txt AC 2 ms
5,380 KB
test4.txt AC 3 ms
5,380 KB
test5.txt AC 3 ms
5,380 KB
test6.txt AC 3 ms
6,148 KB
test7.txt AC 3 ms
5,380 KB
test8.txt AC 3 ms
5,380 KB
test9.txt AC 2 ms
5,380 KB
test10.txt AC 2 ms
5,376 KB
test11.txt AC 3 ms
5,376 KB
test12.txt AC 2 ms
5,376 KB
test13.txt AC 2 ms
5,380 KB
テストケース一括ダウンロード

ソースコード

diff #
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/





int N, E[2020];
//---------------------------------------------------------------------------------------------------
int g(int a, int b, int c) {
    if (a == 0 and b == 0 and c == 0) return 0;
    if (a == 1 and b == 0 and c == 0) return 0;
    if (a == 1 and b == 1 and c == 1) return 0;
    return 1;
}
//---------------------------------------------------------------------------------------------------
mint dp[2020][2][2];
mint f(int v0, int v1) {
    rep(i, 0, N + 1) rep(a, 0, 2) rep(b, 0, 2) dp[i][a][b] = 0;
    dp[2][v0][v1] = 1;
    rep(i, 2, N) rep(a, 0, 2) rep(b, 0, 2) rep(c, 0, 2) if (g(a, b, c) == E[i - 1]) {
        dp[i + 1][b][c] += dp[i][a][b];
    }

    mint ans = 0;
    rep(a, 0, 2) rep(b, 0, 2) {
        if (g(a, b, v0) == E[N - 1] and g(b, v0, v1) == E[0]) ans += dp[N][a][b];
    }
    return ans;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N) cin >> E[i];

    mint ans = 0;
    rep(v0, 0, 2) rep(v1, 0, 2) ans += f(v0, v1);
    cout << ans << endl;
}
0