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 dp[105][105][3] = { 0 };

int main() {
	int n, k, a[105], b[105], ans = 0;
	cin >> n >> k;
	for (int i = 0; i < k; i++) {
		cin >> a[i] >> b[i];
		b[i]--;
	}
	for (int i = 0; i < 3; i++){
		dp[1][i][1] = 1;
	}	
	for (int i = 0; i < k; i++) {
		if (a[i] == 1) {
			dp[1][(b[i] + 1) % 3][1] = 0;
			dp[1][(b[i] + 2) % 3][1] = 0;
		}
	}
	for (int i = 2; i <= n; i++) {
		bool bo = false;
		int m = 0;
		for (m = 0; m < k; m++) {
			if (a[m] == i) {
				bo = true;
				break;
			}
		}
		if (bo) {
			dp[i][b[m]][1] += dp[i - 1][(b[m] + 1) % 3][1] + dp[i - 1][(b[m] + 2) % 3][1];
			dp[i][b[m]][1] %= 10000;
			dp[i][b[m]][1] += dp[i - 1][(b[m] + 1) % 3][2] + dp[i - 1][(b[m] + 2) % 3][2];
			dp[i][b[m]][1] %= 10000;
			dp[i][b[m]][2] = dp[i - 1][b[m]][1];
			dp[i][b[m]][2] %= 10000;
		}
		else {
			for (int j = 0; j < 3; j++) {
				dp[i][j][1] += dp[i - 1][(j + 1) % 3][1] + dp[i - 1][(j + 2) % 3][1];
				dp[i][j][1] %= 10000;
				dp[i][j][1] += dp[i - 1][(j + 1) % 3][2] + dp[i - 1][(j + 2) % 3][2];
				dp[i][j][1] %= 10000;
				dp[i][j][2] = dp[i - 1][j][1];
				dp[i][j][2] %= 10000;
			}
		}
	}
	for (int i = 0; i < 3; i++) {
		ans += dp[n][i][1] + dp[n][i][2];
		ans %= 10000;
	}
	cout << ans << endl;
	return 0;
}

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

typedef pair<int, int> P;

int d1[101], d2[101], d3[101], s1[101], s2[101], s3[101], sta[101];
int N, K;

void search(int n){
	
	if(n == N) return;
	
	if(n == 0){
		if(sta[n] == 1) s1[n] = 1;
		if(sta[n] == 2) s2[n] = 1;
		if(sta[n] == 3) s3[n] = 1;
		if(sta[n] == 0)s1[0] = s2[0] = s3[0] = 1;
		search(1);
		return;
	}
	
	if(sta[n] == 1){
		d1[n - 1] = 0;
		d1[n] = s1[n - 1];
		s1[n] = d2[n - 1] + d3[n - 1] + s2[n - 1] + s3[n - 1];
	}
	if(sta[n] == 2){
		d2[n - 1] = 0;
		d2[n] = s2[n - 1];
		s2[n] = d1[n - 1] + d3[n - 1] + s1[n - 1] + s3[n - 1];
	}
	if(sta[n] == 3){
		d3[n - 1] = 0;
		d3[n] = s3[n - 1];
		s3[n] = d1[n - 1] + d2[n - 1] + s1[n - 1] + s2[n - 1];
	}
	if(sta[n] == 0){
		d1[n] = s1[n - 1];
		d2[n] = s2[n - 1];
		d3[n] = s3[n - 1];
		s1[n] = d2[n - 1] + d3[n - 1] + s2[n - 1]  + s3[n - 1];
		s2[n] = d1[n - 1] + d3[n - 1] + s1[n - 1]  + s3[n - 1];
		s3[n] = d1[n - 1] + d2[n - 1] + s1[n - 1]  + s2[n - 1];
	}
	
	
	if(sta[n + 1] != 0 && sta[n + 1] == sta[n + 2]){
		if(sta[n] == 1) s1[n] = 0;
		if(sta[n] == 2) s2[n] = 0;
		if(sta[n] == 3) s3[n] = 0;
	}
	search(n + 1);
	return;
}

int main(){
	for(int i = 0;i < 101;i ++) sta[i]= 0;
	
	cin >> N >> K;
	int Ai, Bi;
	for(int i = 0;i < K;i ++){
		cin >> Ai >> Bi;
		sta[Ai] = Bi;
	}
	search(0);
	cout << d1[N - 1] + d2[N - 1] + d3[N - 1] + s1[N - 1] + s2[N - 1] + s3[N - 1] << endl;
	
	return 0;
}

C++: #include<string.h>
#include<iostream>

int n, k;
int dp[101][4][2];
int pasta[101];

int main()
{
	std::cin >> n >> k;
	for( int i = 0; i < k; i++ ){
		int a, b;
		std::cin >> a >> b;
		pasta[a] = b;
	}
	
	if( pasta[1] ){
	 	dp[1][pasta[1]][0] = 1; 
	}
	else{
		dp[1][1][0] = dp[1][2][0] = dp[1][3][0] = 1; 
	}
	
	for( int i = 2; i <= n; i++ ){
		if( !pasta[i] ){
		 	dp[i][1][0] = ( dp[i-1][2][0] + dp[i-1][2][1] + dp[i-1][3][0] + dp[i-1][3][1] ) % 10000;
			dp[i][1][1] = dp[i-1][1][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] ) % 10000;
			dp[i][2][1] = dp[i-1][2][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] ) % 10000;
			dp[i][3][1] = dp[i-1][3][0];
		}
		else{
			for( int j = 1; j <= 3; j++ ){
				if( j != pasta[i] ) dp[i][pasta[i]][0] += ( dp[i-1][j][0] + dp[i-1][j][1] ) % 10000;
			}
			dp[i][pasta[i]][1] = dp[i-1][pasta[i]][0];
		}
	}
	
	std::cout << ( dp[n][1][0] + dp[n][1][1] + dp[n][2][0] + dp[n][2][1] + dp[n][3][0] + dp[n][3][1] ) % 10000 << std::endl;

 	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int dp[110][3][2];
int num[110];

signed main(){
	int n,k;
	cin>>n>>k;
	memset(num,-1,sizeof(num));
	//rep(i,0,110) rep(j,0,3) rep(k,0,2) dp[i][j][k]=-1;
	rep(i,0,k){
		int a,b;
		cin>>a>>b;
		a--; b--;
		num[a]=b;
	}
	if(num[0]==-1) rep(i,0,3) dp[0][i][0]=1;
	else dp[num[0]][0][0]=1;

	rep(i,1,n){
		rep(j,0,3){
			if(!(num[i]==-1||num[i]==j)) continue;
			dp[i][j][1]=(dp[i][j][1]+dp[i-1][j][0])%10000;
			rep(k,0,3){
				if(j==k) continue;
				dp[i][j][0]=(dp[i][j][0]+dp[i-1][k][0]+dp[i-1][k][1])%10000;
			}
		}
	}
	int ans=0;
	rep(i,0,3) rep(j,0,2) ans+=dp[n-1][i][j];
	o(ans%10000);
}