d[i][j][n] 表示头颜色为i,尾颜色为j,且长度为n的排列
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <sstream>
#include <vector>
using namespace std;
#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define REPD(i,a,b) for(int i=a;i>=(int)b;i--)typedef long long LL;
LL d[4][4][50+5];
int main()
{int n;memset(d,0,sizeof(d));REP(i,0,3) REP(j,0,3) {if(i!=j) d[i][j][2]=1;d[i][j][3]=1+(i==j);}REP(k,3,55) REP(i,0,3) REP(j,0,3) {d[i][j][k]+=d[(i+1)%3][(j+1)%3][k-2];d[i][j][k]+=d[(i+2)%3][(j+2)%3][k-2];d[i][j][k]+=d[(i+1)%3][(j+2)%3][k-2];d[i][j][k]+=d[(i+2)%3][(j+1)%3][k-2];}while(scanf("%d",&n)==1){if(n<=3) {if(n==1) printf("3\n");if(n==2||n==3) printf("6\n");continue;}LL ans=0;REP(i,0,3) REP(j,0,3) if(i!=j) ans+=d[i][j][n];printf("%I64d\n", ans);}return 0;
}