```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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 ``` ```#include #include #include #include #include #include #include #include #include using namespace std; #define MAXN 100500 #define pi pair #define y first #define x second using namespace std; int N, _n, cs, tr, u; int chk[MAXN][128], pre[MAXN], suf[MAXN]; long long A[128], B[128], F[128]; string s; int bS(long long n, int lo = 1, int hi = 100) // first i s.t. F[i] >= n { if(lo == hi) { if(F[lo] >= n) { return lo; } else { return lo+1; } } if(lo + 1 == hi) { if(F[lo] >= n) { return lo; } if(F[hi] >= n) { return hi; } return hi+1; } int mid = (hi+lo)/2; if(F[mid] >= n) { return bS(n,lo,mid); } return bS(n,mid+1,hi); } int check(int i, int n) { if(chk[i][n] != -1) { return chk[i][n]; } if(F[n] > N-i) { return chk[i][n] = 0; } if(n == 0) { return chk[i][0] = (s[i] == '0'); } if(n == 1) { return chk[i][1] = (s[i] == '1'); } chk[i][n] = 0; if(check(i,n-1)) { if(check(i+F[n-1],n-2)) { chk[i][n] = 1; } } return chk[i][n]; } int Pre(int i, int n) { if(n == 0) { return (i == N-1 && s[i] == '0'); } return pre[i] <= n; } int Suf(int i, int n) { if(n == 0) { return (i == 0 && s[i] == '0'); } return suf[i] <= n && (suf[i]%2 == n%2); } long long find(int n) { if(A[n] != -1) { return A[n]; } if(n == 0) { A[0] = (s == "0"); return A[0]; } if(n == 1) { A[1] = (s == "1"); return A[1]; } A[n] = find(n-1)+find(n-2); B[n] = 0; if(n >= 4 && N <= F[n-4]) { B[n] = B[n-2]; } else for(int i = 0; i < N-1; i += 1) { B[n] += (long long)(Suf(i,n-1)&Pre(i+1,n-2)); } A[n] += B[n]; return A[n]; } int main() { F[0] = 1; F[1] = 1; for(int n = 2; n < 128; n += 1) { F[n] = min(F[n-1]+F[n-2],1000000000LL); } while(cin >> _n >> s) { N = s.size(); for(tr = 0; F[tr] < (long long)N; tr += 1); tr += tr&1; tr = 10; memset(chk,-1,sizeof(chk)); suf[0] = s[0]-'0'; for(int i = 1; i < N; i += 1) { u = bS(i+1); if(u < 2) { suf[i] = (s[0] == '1')? u : 1000; } else { suf[i] = (Suf(i-F[u-2],u-1) && check(i-F[u-2]+1,u-2))?u:1000; } u++; if(u < 2) { suf[i] = min(suf[i], (s[0] == char(u+'0'))? u : 1000); } else { suf[i] = min(suf[i], (Suf(i-F[u-2],u-1) && check(i-F[u-2]+1,u-2))?u:1000); } } for(int i = N-1; i >= 0; i -= 1) { u = bS(N-i); if(u < 2) { pre[i] = (s[N-1] == char(u+'0'))? u : 1000; } else { pre[i] = (Pre(i+F[u-1],u-2) && check(i,u-1))?u:1000; } } for(int i = 0; i <= _n; i += 1) { A[i] = -1; } cout << "Case " << ++cs << ": " << find(_n) << '\n'; } return 0; } ```