結果
| 問題 |
No.663 セルオートマトンの逆操作
|
| コンテスト | |
| ユーザー |
ふっぴー
|
| 提出日時 | 2018-03-09 23:20:34 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,851 bytes |
| コンパイル時間 | 2,183 ms |
| コンパイル使用メモリ | 179,952 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-10 19:24:49 |
| 合計ジャッジ時間 | 2,955 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 29 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
const int inf = 1000000001;
const ll INF = 2e18;
const ll MOD = 1000000007;
const ll mod = 1000000009;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
ll mod_pow(ll x, ll p, ll M) {
ll a = 1;
while (p) {
if (p % 2)
a = a*x%M;
x = x*x%M;
p /= 2;
}
return a;
}
ll mod_inverse(ll a, ll m) {
return mod_pow(a, m - 2, m);
}
int main() {
int n;
cin >> n;
string s;
rep(i, n) {
char c;
cin >> c;
s.push_back(c);
}
int m;
vs first;
if (s[0] == '0') {
m = 3;
first.resize(m);
first[0] = "000"; first[1] = "100"; first[2] = "111";
}
else {
m = 5;
first.resize(m);
first[0] = "001"; first[1] = "010"; first[2] = "011";
first[3] = "101"; first[4] = "110";
}
ll res = 0;
set<string> st0, st1;
st0.insert("100");
st0.insert("000");
st0.insert("111");
st1.insert("001");
st1.insert("010");
st1.insert("011");
st1.insert("101");
st1.insert("110");
DEBUG(s);
rep(i, m) {
string ans(n, '?');
ans[0] = first[i][1];
ans[1] = first[i][2];
ans[n - 1] = first[i][0];
ll temp = 1;
for (int j = 1; j < n - 2; j++) {
if (ans[j - 1] == '0' && ans[j] == '0') {
ans[j + 1] = s[j];
}
else if (ans[j - 1] == '1' && ans[j] == '0') {
ans[j + 1] = s[j];
}
else if (ans[j - 1] == '1' && ans[j] == '1') {
ans[j + 1] = '0' + (1 - (s[j] - '0'));
}
else if (ans[j - 1] == '0' && ans[j] == '1') {
if (s[j] == '0') {
temp = 0;
break;
}
else {
ans[j + 1] = '2';
temp *= 2;
}
}
else {
ans[j + 1] = s[j];
}
}
ans.push_back(ans[0]);
for (int j = n - 2; j <= n - 1; j++) {
if (temp == 0) {
continue;
}
if (s[j] == '0') {
string a = ans.substr(j - 1, 3);
if (st0.count(a)) {
continue;
}
else if (a[0] == '2') {
if (a.substr(1, 2) != "00") {
if (a.substr(1, 2) == "11") {
(temp *= mod_inverse(2, MOD)) %= MOD;
ans[j] = '1';
}
else {
temp = 0;
}
}
}
else if (a[1] == '2') {
if (a[0] == '0' && a[2] == '1') {
temp = 0;
}
else {
a[1] = a[2];
(temp *= mod_inverse(2, mod)) %= MOD;
}
}
else {
temp = 0;
}
}
else {
string a = ans.substr(j - 1, 3);
if (st1.count(a)) {
continue;
}
else if (a[0] == '2') {
if (a.substr(1, 2) != "01" && a.substr(1, 2) != "10") {
if (a.substr(1, 2) == "11") {
(temp *= mod_inverse(2, MOD)) %= MOD;
ans[j] = '0';
}
else {
temp = 0;
}
}
}
else if (a[1] == '2') {
if (a[0] == '0' && a[2] == '0') {
ans[j + 1] = '1';
(temp *= mod_inverse(2, MOD)) %= MOD;
}
else if (a[0] == '0' && a[2] == '1') {
continue;
}
else if (a[0] == '1' && a[2] == '0') {
ans[j + 1] = '1';
(temp *= mod_inverse(2, MOD)) %= MOD;
}
else if (a[0] == '1' && a[2] == '1') {
ans[j + 1] = '0';
(temp *= mod_inverse(2, MOD)) %= MOD;
}
}
else {
temp = 0;
}
}
}
DEBUG(temp);
(res += temp) % MOD;
}
cout << res << endl;
}
ふっぴー