1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5,P = 1e9 + 7; inline void Plus(int &x,const int &y) { x += y;if(x >= P) x -= P;} int n; char s[N]; int a[N]; bool Can[5][4096];
struct State{ int t0,t1,t2; bool v0,v1,v2,v3; State(){} State(const int _t0,const int _t1,const int _t2 ,const int _v0,const int _v1,const int _v2,const int _v3): t0(_t0),t1(_t1),t2(_t2),v0(_v0),v1(_v1),v2(_v2),v3(_v3){} bool operator == (const State &rhs) const { return t0 == rhs.t0 and t1 == rhs.t1 and t2 == rhs.t2 and v0 == rhs.v0 and v1 == rhs.v1 and v2 == rhs.v2 and v3 == rhs.v3; } }; struct Hash{ static const int base = 131; typedef unsigned int uint; uint operator ()(const State &x) const { uint res = 0; res = res * base + x.t0;res = res * base + x.t1; res = res * base + x.t2;res = res * base + x.v0; res = res * base + x.v1;res = res * base + x.v2; res = res * base + x.v3;return res; } }; inline State Reduce(int id,const State &x) { State res = x; if(res.v3 == 0) res.t2 = 10; if(res.v3 == 0 && res.v2 == 0) res.t1 = 10; if(res.v3 == 0 && res.v2 == 0 && res.v1 == 0) res.t0 = 10; int bk[10];memset(bk,0,sizeof bk); if(res.v3 == 1) { if(id > 2) bk[a[id - 2]] = 1; if(id > 1) bk[a[id - 1]] = 1; bk[a[id]] = 1; if(id < n) bk[a[id + 1]] = 1; if(!Can[3][(1 << res.t0) | (1 << res.t1) | (1 << res.t2)]) res.v3 = 0; if(!bk[res.t0] || !bk[res.t1] || !bk[res.t2]) res.v3 = 0; } if(res.v2 == 1) { memset(bk,0,sizeof bk); if(id > 1) bk[a[id - 1]] = 1; bk[a[id]] = 1; if(id < n) bk[a[id + 1]] = 1; if(id < n - 1) bk[a[id + 2]] = 1; if(!bk[res.t0] || !bk[res.t1]) res.v2 = 0; } if(res.v1 == 1) { memset(bk,0,sizeof bk); bk[a[id]] = 1; if(id < n) bk[a[id + 1]] = 1; if(id + 1 < n) bk[a[id + 2]] = 1; if(id + 2 < n) bk[a[id + 3]] = 1; if(!bk[res.t0]) res.v1 = 0; } return res; } inline State Next(int id,State x,int c) { State res; res.t2 = x.t1;res.t1 = x.t0;res.t0 = c; res.v3 = x.v2;res.v2 = x.v1;res.v1 = x.v0; res.v0 = 0; if(x.v0 && res.t0 == a[id + 1]) res.v0 = 1; if(x.v1) { int tv = (1 << a[id]) | (1 << a[id + 1]); if(tv == (1 << res.t0) + (1 << res.t1) && Can[2][tv]) res.v0 = 1; } if(x.v3) { int tv = (1 << a[id - 2]) | (1 << a[id - 1]) | (1 << a[id]) | (1 << a[id + 1]); if(tv == (1 << res.t0) + (1 << res.t1) + (1 << res.t2) + (1 << x.t2) && Can[4][tv]) res.v0 = 1; } return res; } unordered_map<State,int,Hash> mp1,mp2; inline void Work() { cin >> (s + 1); n = strlen(s + 1); for(int i = 1;i <= n;i++) a[i] = s[i] - '0'; mp1.clear(); mp1[State(10,10,10,1,0,0,0)] = 1; for(int i = 0;i < n;i++) { mp2.clear(); for(auto it : mp1) { State t = it.first; for(int c = 1;c <= 9;c++) { State nxt = Next(i,t,c); nxt = Reduce(i + 1,nxt); if(nxt.v3 + nxt.v2 + nxt.v1 + nxt.v0 == 0) continue; Plus(mp2[nxt],it.second); } } mp1 = mp2; } int ans = 0; for(auto it : mp1) if(it.first.v0 == 1) Plus(ans,it.second); cout << ans << endl; } int main() { for(int i = 1;i <= 9;i++) for(int j = i + 1;j <= 9;j++) if(abs(i - j) == 3 || (abs(i - j) == 1 && i != 3 && i != 6)) Can[2][(1 << i) | (1 << j)] = 1; Can[4][32 + 16 + 4 + 2] = 1; Can[4][64 + 32 + 8 + 4] = 1; Can[4][256 + 128 + 32 + 16] = 1; Can[4][512 + 256 + 64 + 32] = 1; for(int i = 1;i <= 9;i++) for(int j = i + 1;j <= 9;j++) for(int k = j + 1;k <= 9;k++) for(int p = 1;p <= 9;p++) Can[3][(1 << i) | (1 << j) | (1 << k)] |= Can[4][(1 << i) + (1 << j) + (1 << k) + (1 << p)]; int T; cin >> T; while(T--) Work(); return 0; }
|