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 <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#define MOD	10000
#define INF 123456789

long long int N, K, A[112], B[112];
long long int pasta[112], dp[112][3][2], ans = 0;

int main(void) {
	scanf("%lld%lld", &N, &K);
	for (int i = 0; i < K; ++i) { scanf("%lld%lld", &A[i], &B[i]); }

	for (int i = 0; i < N; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { dp[i][j][k] = INF; } } }
	for (int i = 0; i < K; ++i) { pasta[A[i] - 1] = B[i]; }
	if (pasta[0] == 0) {
		for (int j = 0; j < 3; ++j) {
			dp[0][j][0] = 1;
			dp[0][j][1] = 0;
		}
	}
	else {
		for (int j = 0; j < 3; ++j) { dp[0][j][0] = dp[0][j][1] = 0; }
		dp[0][pasta[0] - 1][0] = 1;
	}

	// DP
	for (int i = 1; i<N; i++) {
		if (pasta[i] == 0) {
			dp[i][0][0] = (dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][2][0] + dp[i - 1][2][1]) % MOD;
			dp[i][1][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][2][0] + dp[i - 1][2][1]) % MOD;
			dp[i][2][0] = (dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][0][0] + dp[i - 1][0][1]) % MOD;
			dp[i][0][1] = dp[i - 1][0][0] % MOD;
			dp[i][1][1] = dp[i - 1][1][0] % MOD;
			dp[i][2][1] = dp[i - 1][2][0] % MOD;
		}
		else {
			for (int j = 0; j<3; j++) {
				if (j == pasta[i] - 1) {
					dp[i][j][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][2][0] + dp[i - 1][2][1] - dp[i - 1][j][0] - dp[i - 1][j][1]) % MOD;
					dp[i][j][1] = dp[i - 1][j][0];
				}
				else {
					dp[i][j][0] = dp[i][j][1] = 0;
				}
			}
		}
	}

	for (int j = 0; j < 3; ++j) {
		for (int k = 0; k < 2; ++k) {
			ans += dp[N - 1][j][k];
		}
	}
	printf("%lld\n", ans % MOD);
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<functional>
using namespace std;
long long int dp[101][4][4];
int main(void)
{
	int i,j,k,n,m,a,b[101],c;
	long long int sum,x;
	cin>>n>>m;
	for(i=1;i<=n;i++){
		for(j=1;j<=3;j++){
			for(k=0;k<=3;k++) dp[i][j][k]=0;
		}
	}
	for(i=1;i<=n;i++) b[i]=0;
	for(i=1;i<=m;i++){
		cin>>a;
		cin>>b[a];
	}
//	for(i=1;i<=n;i++)	printf("b=%d\n",b[i]);
	if(b[1]>0) dp[1][b[1]][0]=1;
	else{
		for(i=1;i<=3;i++) dp[1][i][0]=1;
	}
	for(i=1;i<n;i++){
//		printf("i=%d\n",i);
		for(j=1;j<=3;j++){
			if(dp[i][j][0]+dp[i][j][1]+dp[i][j][2]+dp[i][j][3]>0){
				for(k=0;k<=3;k++){
					if(b[i+1]>0){
						if(b[i+1]!=k||b[i+1]!=j){
							dp[i+1][b[i+1]][j]+=dp[i][j][k];
						}
					}
					else{
						if(k!=1||j!=1) dp[i+1][1][j]+=dp[i][j][k];
						if(k!=2||j!=2) dp[i+1][2][j]+=dp[i][j][k];
						if(k!=3||j!=3) dp[i+1][3][j]+=dp[i][j][k];
					}
				}
			}
		}
/*		for(j=1;j<=3;j++)	{
			for(k=1;k<=3;k++)	{			
				printf("dp=%d ",dp[i][j][k]);
			}
			printf("\n");
		}*/
	}
/*	for(i=1;i<=n;i++){
		for(j=1;j<=3;j++){
			sum=0;
			for(k=0;k<=3;k++) sum+=dp[i][j][k];
			printf("%lld ",sum);
		}
		printf("\n");
	}*/
	sum=0;
	for(i=1;i<=3;i++){
		for(j=0;j<=3;j++) sum+=dp[n][i][j];
	}
	x=sum%10000;
	printf("%d\n",x);
	return 0;
}

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

using namespace std;

int n,k;
int dp[102][4][2];
int yt[102];

void debug(){
    for(int i=1;i<=3;i++){
        for(int d=0;d<=n;d++){
            printf("%d(%3d,%3d) ",d,dp[d][i][0],dp[d][i][1]);
        }
        cout << endl;
    }
}

int solve(){
    if(yt[1] == 0){
        dp[1][1][0] = dp[1][2][0] = dp[1][3][0] = 1;
    }else{
        dp[1][yt[1]][0] = 1;
    }
    for(int d=2;d<=n;d++){
        for(int i=1;i<=3;i++){
            if(yt[d] != 0 && yt[d] != i){ dp[d][i][0] = dp[d][i][1] = 0; continue; }
            dp[d][i][1] = dp[d-1][i][0] % 10000;
            dp[d][i][0] = 0;
            for(int j=1;j<=3;j++){
                if(i==j)continue;
                dp[d][i][0] += (dp[d-1][j][0] + dp[d-1][j][1]) % 10000;
            }
        }
    }
    int ans = 0;
    for(int i=1;i<=3;i++){
        ans += (dp[n][i][0] + dp[n][i][1]) % 10000;
    }
    return ans;
}

int main(){
    cin >> n >> k;
    int a,b;
    for(int i=0;i<k;i++){
        cin >> a >> b;
        yt[a] = b;
    }
    cout << solve() << endl;
}