详细解释与推导见算法竞赛训练指南26页
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int maxn = 60 + 7, INF = 0x3f3f3f3f, mod = 1e9+7;
int p, n, x;
int state[maxn], finish[maxn];
ll f(int *a, int k, int other) {
if(k == 0) return 0;
else if(a[k] == other) return f(a, k-1, other);
else return f(a, k-1, 6 - a[k] - other) + (1LL << (k-1));
}
int main() {
int kase = 1;
while(scanf("%d", &n) != EOF && n) {
for(int i = 1; i <= n; ++i)
scanf("%d", &state[i]);
for(int i = 1; i <= n; ++i)
scanf("%d", &finish[i]);
int k = n;
while(k && state[k] == finish[k]) k--;
ll ans = 0;
if(k) {
int other = (6 - state[k] - finish[k]);
ans = (f(state, k-1, other) + f(finish, k-1, other) + 1);
}
printf("Case %d: %lld\n", kase++, ans);
}
return 0;
}