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>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
#define MOD 10000

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int dp[105][5][5], d[105];
bool used[105];

int main() {
	int n, k;
	cin >> n >> k;

	memset(used, 0, sizeof(used));
	memset(d, 0, sizeof(d));

	rep(i, k) {
		int a, b;
		cin >> a >> b;

		used[a] = true;
		d[a] = b;
	}

	memset(dp, 0, sizeof(dp));

	if(used[1]) {
		REP(j, 1, 4) {
			if(d[1] == j) dp[1][j][0] = 1;
		}
	} else {
		REP(j, 1, 4) {
			dp[1][j][0] = 1;
		}
	}

	REP(i, 2, n + 1) {
		if(used[i]) {
			if(d[i] == 1) {
				dp[i][1][1] = dp[i-1][1][0];
				dp[i][1][0] = (dp[i-1][2][0] + dp[i-1][2][1]) + (dp[i-1][3][0] + dp[i-1][3][1]);
				dp[i][1][1] %= MOD;
				dp[i][1][0] %= MOD;
			} else if(d[i] == 2) {
				dp[i][2][1] = dp[i-1][2][0];
				dp[i][2][0] = (dp[i-1][1][0] + dp[i-1][1][1]) + (dp[i-1][3][0] + dp[i-1][3][1]);
				dp[i][2][1] %= MOD;
				dp[i][2][0] %= MOD;
			} else {
				dp[i][3][1] = dp[i-1][3][0];
				dp[i][3][0] = (dp[i-1][1][0] + dp[i-1][1][1]) + (dp[i-1][2][0] + dp[i-1][2][1]);
				dp[i][3][1] %= MOD;
				dp[i][3][0] %= MOD;
			}
		} else {
			dp[i][1][1] = dp[i-1][1][0];
			dp[i][1][0] = (dp[i-1][2][0] + dp[i-1][2][1]) + (dp[i-1][3][0] + dp[i-1][3][1]);
			dp[i][2][1] = dp[i-1][2][0];
			dp[i][2][0] = (dp[i-1][1][0] + dp[i-1][1][1]) + (dp[i-1][3][0] + dp[i-1][3][1]);
			dp[i][3][1] = dp[i-1][3][0];
			dp[i][3][0] = (dp[i-1][1][0] + dp[i-1][1][1]) + (dp[i-1][2][0] + dp[i-1][2][1]);

			dp[i][1][1] %= MOD;
			dp[i][1][0] %= MOD;
			dp[i][2][1] %= MOD;
			dp[i][2][0] %= MOD;
			dp[i][3][1] %= MOD;
			dp[i][3][0] %= MOD;
		}
	}


	// rep(i, n + 1) {
	// 	REP(j, 1, 4) {
	// 		cout << "(" << dp[i][j][0] << "," << dp[i][j][1] << ") ";
	// 	}
	// 	cout << endl;
	// }

	int sum = 0;
	REP(j, 1, 4) {
		rep(k, 2) {
			sum += dp[n][j][k];
			sum %= MOD;
		}
	}

	cout << sum << endl;

	return 0;
}

C++: #include<stdio.h>
int dp[101][4][4];
int main(void)
{
	int n,d;
	int a,b;
	int pa[101];
	int s=0;
	int i,j,k,l;
	
	scanf("%d %d",&n,&d);
	for(i=1;i<=n;i++){
		pa[i]=0;
		for(j=1;j<=3;j++){
			for(k=1;k<=3;k++){
				dp[i][j][k]=0;
			}
		}
	}
	for(i=1;i<=d;i++){
		scanf("%d %d",&a,&b);
		pa[a]=b;
	}
	if(pa[1]==0){
		for(i=1;i<=3;i++){
			dp[1][i][1]=1;
		}
	}
	else {
		dp[1][pa[1]][1]=1;
	}
	for(i=1;i<=3;i++){
		if(pa[1]==i || pa[1]==0){
			for(j=1;j<=3;j++){
				if(pa[2]==j || pa[2]==0){
					dp[2][j][i]+=1;
				}
			}
		}
	}
	for(i=2;i<n;i++){
		for(j=1;j<=3;j++){
			if(pa[i]==j || pa[i]==0){
				for(k=1;k<=3;k++){
					for(l=1;l<=3;l++){
						if(pa[i+1]==l || pa[i+1]==0){
							if(!(j==k && l==j)){
								dp[i+1][l][j]+=dp[i][j][k];
								dp[i+1][l][j]=dp[i+1][l][j]%10000;
							}
						}
					}
				}
			}
		}
	}
	
/*	for(i=1;i<=n;i++){
		for(j=1;j<=3;j++){
			for(k=1;k<=3;k++){
				printf(" %d",dp[i][j][k]);
			}
			printf(" |");
		}
		printf("\n");
	}*/
	for(i=1;i<=3;i++){
		for(j=1;j<=3;j++){
			s+=dp[n][i][j];
			s=s%10000;
		}
	}
	printf("%d\n",s);
	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 - 1] = 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;
}