Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

// 文字列を1行取得
func StrStdin() (stringReturned string) {
//	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	stringInput := scanner.Text()

	stringReturned = strings.TrimSpace(stringInput)
	return
}

// 整数値1つ取得
func IntStdin() (int, error) {
	stringInput := StrStdin()
	return strconv.Atoi(strings.TrimSpace(stringInput))
}

// 空白や空文字だけの値を除去したSplit()
func SplitWithoutEmpty(stringTargeted string, delim string) (stringReturned []string) {
	stringSplited := strings.Split(stringTargeted, delim)

	for _, str := range stringSplited {
		if str != "" {
			stringReturned = append(stringReturned, str)
		}
	}

	return
}

// デリミタで分割して文字列スライスを取得
func SplitStrStdin(delim string) (stringReturned []string) {
	// 文字列で1行取得
	stringInput := StrStdin()

	// 空白で分割
	// stringSplited := strings.Split(stringInput, delim)
	stringReturned = SplitWithoutEmpty(stringInput, delim)

	return
}

// デリミタで分割して整数値スライスを取得
func SplitIntStdin(delim string) (intSlices []int, err error) {
	// 文字列スライスを取得
	stringSplited := SplitStrStdin(" ")

	// 整数スライスに保存
	for i := range stringSplited {
		var iparam int
		iparam, err = strconv.Atoi(stringSplited[i])
		if err != nil {
			return
		}
		intSlices = append(intSlices, iparam)
	}
	return
}

func main() {
	nk, _ := SplitIntStdin(" ")
	n, k := nk[0], nk[1]
	kind := make([]int, n)
	for i := 0; i < k; i++ {
		var tmp []int
		var err error
		if tmp, err = SplitIntStdin(" "); err != nil {
			panic(err)
		}
		kind[tmp[0]-1] = tmp[1]
	}
	dp := make([][3][3]int, n)
	if kind[0] > 0 {
		dp[0][0][kind[0]-1] = 1
	} else {
		for i := 0; i < 3; i++ {
			dp[0][0][i] = 1
		}
	}
	if kind[1] > 0 {
		for i := 0; i < 3; i++ {
			dp[1][i][kind[1]-1] = dp[0][0][i]
		}
	} else {
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				dp[1][i][j] = dp[0][0][i]
			}
		}
	}
	for i := 1; i < n-1; i++ {
		if kind[i+1] > 0 {
			for j := 0; j < 3; j++ {
				if j == kind[i+1]-1 {
					for k := 0; k < 3; k++ {
						if j != k {
							dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
							dp[i+1][j][kind[i+1]-1] %= 10000
						}
					}
				} else {
					for k := 0; k < 3; k++ {
						dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
						dp[i+1][j][kind[i+1]-1] %= 10000
					}
				}
			}
		} else {
			for j := 0; j < 3; j++ {
				for k := 0; k < 3; k++ {
					for l := 0; l < 3; l++ {
						if j != k || k != l {
							dp[i+1][k][j] += dp[i][l][k]
							dp[i+1][k][j] %= 10000
						}
					}
				}
			}
		}
	}
	result := 0
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			result += dp[n-1][i][j]
		}
	}
	// fmt.Println(dp)
	// fmt.Println(kind)
	fmt.Println(result % 10000)
}

C++: #include<iostream>
using namespace std;

int main(){
	int n,m,d[100],t[100],type[4][4],c;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>d[i]>>t[i];
	}
	for(int i=0;i<m;i++){
		if(d[i]==1){
			type[1][i]=1;
			goto Exit1;
		}
	}
	type[1][1]=1;
	type[2][1]=1;
	type[3][1]=1;
	
Exit1:
	for(int i=1;i<=3;i++){
		if(type[i][1]!=1){
			type[i][1]=0;
		}
	}
	type[1][2]=0;
	type[2][2]=0;
	type[3][2]=0;
	for(int i=2;i<=n;i++){
		for(int j=0;j<m;j++){

			if(d[j]==i){
				type[t[j]][2]=type[t[j]][1];
				type[t[j]][1]=0;
				for(int k=1;k<=3;k++){
					if(t[j]!=k){
						for(int l=1;l<=2;l++){
						type[t[j]][1] += type[k][l];
						type[k][l]=0;
						}
					}
				}
				goto Exit2;
			}

		}
		for(int k=1;k<=3;k++){
			type[k][2] = type[k][2]*2+type[k][1];
			type[k][1]  *= 2;
		}
Exit2:
		for(int j=1;j<=3;j++){
			for(int k=1;k<=2;k++){
				type[j][k]=type[j][k]%10000;
			}
		}
	}
	cout<<(type[1][1]+type[1][2]+type[2][1]+type[2][2]+type[3][1]+type[3][2])%10000<<endl;
	getchar();
	getchar();
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define X real()
#define Y imag()
#define value(x,y,w,h) (x >= 0 && x < w && y >= 0 && y < h)
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(Type,Arg,W,H,Init) (vector<vector<(Type)>> Arg ((W),(vector<(Type)>(H,Init))))

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

typedef long long ll ;
typedef deque<int> di ;
typedef deque<ll> dl ;
typedef map<string,int> dict;
typedef complex<double> comd;
typedef pair<int,int> pii;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

double CalcDist(comd p1, comd p2){
  return sqrt(pow(p1.X - p2.X,2.0) + pow(p1.Y -p2.Y,2.0));
}

  template <typename T>
void out(deque < T > d)
{
  for(size_t i = 0; i < d.size(); i++)
  {
    debug(d[i]);
  }
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}
int N,K,a,b;
int p[110];
// int dp[45][100];
// vary(int,dp,45,100,0);
vector<vector<int>> dp(100,vector<int>(100,0));
int ans = 0;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  rep(i,K){
    cin >> a >> b;
    --a;
    p[a] = b ;
  }
// 1:11 2:12 3:13 4:21 5:22 6:23 7:31 8:32 9:33
  int pasta[9] = {11,12,13,21,22,23,31,32,33};
  rep(i,9){
    if(p[0] == pasta[i] % 10){
      dp[0][i] = 1;
    }
    else if(p[0] == 0){
      dp[0][i] = 1;
    }
  }
  REP(i,0,N-1){
//     debug(p[i]);
//     REP(j,0,9){
//       scout(3) << pasta[j] ;
//       REP(i,0,N+1){
//         scout(5) << dp[i][j] << " ";
//       }
//       cout << endl;
//     }
    if(p[i+1] == 1 || p[i+1] == 0){
      dp[i+1][0] += dp[i][3] + dp[i][6];
      dp[i+1][1] += dp[i][0] + dp[i][3] + dp[i][6];
      dp[i+1][2] += dp[i][0] + dp[i][3] + dp[i][6];
    }
    if(p[i+1] == 2 || p[i+1] == 0){
      dp[i+1][3] += dp[i][1] + dp[i][4] + dp[i][7];
      dp[i+1][4] += dp[i][1] + dp[i][7];
      dp[i+1][5] += dp[i][1] + dp[i][4] + dp[i][7];
    }
    if(p[i+1] == 3 || p[i+1] == 0){
      dp[i+1][6] += dp[i][2] + dp[i][5] + dp[i][8];
      dp[i+1][7] += dp[i][2] + dp[i][5] + dp[i][8];
      dp[i+1][8] += dp[i][2] + dp[i][5];
    }
  }
  rep(i,9){
    ans += dp[N-2][i];
  }
  cout << ans % 10000 << endl;
  return 0;
}

C++: #include <vector>
#include <iostream>

using namespace std;

#define MOD 10000

int main()
{
	int N, K, A, B;

	cin >> N >> K;

	vector<int> P(N, 0);

	for (int i = 0; i < K; i++)
	{
		cin >> A >> B;

		P[A] = B;
	}

	vector<vector<vector<int> > > dp(N, vector<vector<int> >(3, vector<int>(2, 0)));

	if (P[0] != 0)
	{
		dp[0][P[0] - 1][0] = 1;
	}
	else
	{
		dp[0][0][0] = 1;
		dp[0][1][0] = 1;
		dp[0][2][0] = 1;
	}

	for (int i = 1; i < N; ++i)
	{
		if (P[i] == 0)
		{
			for (int j = 0; j < 3; ++j)
			{
				int j1, j2;

				if (j == 0) { j1 = 1; j2 = 2; }
				if (j == 1) { j1 = 0; j2 = 2; }
				if (j == 2) { j1 = 0; j2 = 1; }

				dp[i][j][0] = (dp[i - 1][j1][0] + dp[i - 1][j1][1] + dp[i - 1][j2][0] + dp[i - 1][j2][1]) % MOD;

				dp[i][j][1] = dp[i - 1][j][0];
			}
		}
		else
		{
			int j = P[i] - 1;

			int j1, j2;

			if (j == 0) { j1 = 1; j2 = 2; }
			if (j == 1) { j1 = 0; j2 = 2; }
			if (j == 2) { j1 = 0; j2 = 1; }

			dp[i][j][0] = (dp[i - 1][j1][0] + dp[i - 1][j1][1] + dp[i - 1][j2][0] + dp[i - 1][j2][1]) % MOD;

			dp[i][j][1] = dp[i - 1][j][0];
		}
	}

	int Ans = (dp[N - 1][0][0] + dp[N - 1][0][1] + dp[N - 1][1][0] + dp[N - 1][1][1] + dp[N - 1][2][0] + dp[N - 1][2][1]) % MOD;

	cout << Ans << endl;

	return 0;
}