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 <cstdio>
#include <vector>

using namespace std;

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

void debug(){
    for(int d=0;d<=n;d++){
        printf("%2d ",d);
        for(int i=1;i<=3;i++){
            printf("(%5d,%5d) ",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;
    debug();
    for(int i=1;i<=3;i++){
        ans += (dp[n][i][0] + dp[n][i][1]) % 10000;
    }
    return ans % 10000;
}

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

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

int n, k, a, b, p[100];

int fanc(int i, int x, int y, int z)
{
	int c[3][3][3];
	
	if (i == 0){
		for (int j1 = 0; j1<3; j1++){
			for (int j2 = 0; j2<3; j2++){
				for (int j3 = 0; j3<3; j3++){
					c[j1][j2][j3] = 1;
				}
			}
		}
	}

	else{
		int sum = 0;
		for (int j1 = 0; j1<3; j1++){
			for (int j2 = 0; j2<3; j2++){
				for (int j3 = 0; j3<3; j3++){
					sum += fanc(i - 1, j3, j1, j2);
				}
				for (int j3 = 0; j3 < 3; j3++){
					c[j1][j2][j3] = sum;
				}
				sum=0;
			}
		}
	}

	for (int j1 = 0; j1<3; j1++){
		c[j1][j1][j1] = 0;
	}

	if (p[i]){
		for (int j1 = 0; j1<3; j1++){
			for (int j2 = 0; j2<3; j2++){
				for (int j3 = 0; j3<3; j3++){
					if (j1 != p[i]){ c[j1][j2][j3] = 0; }
				}
			}
		}
	}

	if (p[i + 1]){
		for (int j1 = 0; j1<3; j1++){
			for (int j2 = 0; j2<3; j2++){
				for (int j3 = 0; j3<3; j3++){
					if (j2 != p[i+1]){ c[j1][j2][j3] = 0; }
				}
			}
		}
	}

	if (p[i + 2]){
		for (int j1 = 0; j1<3; j1++){
			for (int j2 = 0; j2<3; j2++){
				for (int j3 = 0; j3<3; j3++){
					if (j3 != p[i+2]){ c[j1][j2][j3] = 0; }
				}
			}
		}
	}


	for (int j1 = 0; j1<3; j1++){
		for (int j2 = 0; j2<3; j2++){
			for (int j3 = 0; j3<3; j3++){
				if (c[j1][j2][j3]>10000){ c[j1][j2][j3] %= 10000; }
			}
		}
	}

	return c[x][y][z];

}

int total()
{
	int sum = 0;
	for (int j1 = 0; j1<3; j1++){
		for (int j2 = 0; j2<3; j2++){
			for (int j3 = 0; j3<3; j3++){
				sum += fanc(n - 3, j1, j2, j3);
			}
		}
	}
	return sum;
}


int main()
{
	cin >> n >> k;
	for (int i = 0; i<k; i++){
		cin >> a >> b;
		p[a - 1] = b;
	}

	cout << total() % 10000 << endl;
	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("(%3d,%3d) ",dp[d][i][0],dp[d][i][1]);
        }
        cout << endl;
    }
}

int solve(){
    dp[0][1][0] = 1;
    for(int d=1;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];
            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];
            }
        }
    }
    int ans = 0;
    for(int i=1;i<=3;i++){
        ans += dp[n][i][0] + dp[n][i][1];
    }
    return ans;
}

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

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
typedef long long int lli;
using namespace std;

int N,K;
const int MAX_N=100;
int plan[MAX_N+1];
int memo[MAX_N+1][4][4];
int solve(int n,int b1,int b2){
	if(n==N+1) return 1;
	if(memo[n][b1][b2]!=-1) return memo[n][b1][b2];
	if(plan[n]!=0){
		if(b1==b2&&b1==plan[n])
			return 0;
		else 
			return solve(n+1,plan[n],b1)%10000;
	}
	int t=0;
	for(int i=1; i<=3; ++i){
		if(b1==b2&&b1==i) continue;
		t+=solve(n+1,i,b1);
	}
	return memo[n][b1][b2]=t%10000;
}

int main(){
	cin >> N >> K;
	for(int i=0; i<K; ++i){
		int a,b;
		cin >> a >> b;
		plan[a]=b;
	}
	for(int i=0; i<=N; ++i)
		for(int j=0; j<4; ++j)
			for(int k=0; k<4; ++k)
				memo[i][j][k]=-1;
	cout << solve(1,0,0) << "\n";
}