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 n,k,total,a,b;
long long menu[101],t[101][4][3],ans[101];
int ot(int a){
  if(a>3)a-=3;
  return a;
}
int main(){
  cin>>n>>k;
  for(int i=0;i<=100;i++){
    menu[i]=0;
    for(int j=0;j<4;j++){
      for(int k=0;k<3;k++){
	t[i][j][k]=0;
      }
    }
  }

  for(int i=0;i<k;i++){
    cin>>a>>b;
    menu[a]=b;
  }

  if(menu[1]==0){
    ans[1]=3;
    t[1][1][1]=t[1][2][1]=t[1][3][1]=1;
    t[1][1][0]=t[1][2][0]=t[1][3][0]=2;
  }else{
    t[1][menu[1]][1]=1;
    t[1][ot(menu[1]+1)][0]=1;
    t[1][ot(menu[1]+2)][0]=1;
    ans[1]=1;
  }
  for(int i=2;i<=n;i++){
    if(menu[i]==0){
      ans[i]=ans[i-1]*3;
      ans[i]-=t[i-1][1][2]+t[i-1][2][2]+t[i-1][3][2];
      t[i][1][2]=t[i-1][1][1];
      t[i][1][1]=t[i-1][1][0];
      t[i][1][0]=ans[i]-t[i][1][2]-t[i][1][1];

      t[i][2][2]=t[i-1][2][1];
      t[i][2][1]=t[i-1][2][0];
      t[i][2][0]=ans[i]-t[i][2][2]-t[i][2][1];

      t[i][3][2]=t[i-1][3][1];
      t[i][3][1]=t[i-1][3][0];
      t[i][3][0]=ans[i]-t[i][3][2]-t[i][3][1];

    }else{
      ans[i]=ans[i-1]-t[i-1][menu[i]][2];
      t[i][menu[i]][2]=t[i-1][menu[i]][1];
      t[i][menu[i]][1]=t[i-1][menu[i]][0];
      t[i][menu[i]][0]=0;
      t[i][ot(menu[i]+1)][0]=ans[i];
      t[i][ot(menu[i]+2)][0]=ans[i];
      t[i][ot(menu[i]+1)][1]=0;
      t[i][ot(menu[i]+2)][1]=0;
      t[i][ot(menu[i]+1)][2]=0;
      t[i][ot(menu[i]+2)][2]=0;
    }
  }
  cout<<ans[n]%10000<<endl;
  return 0;
}

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

using namespace std;

int menu[101][4][4][4];//menue[i??\???][??¨??\????????????][?????\????????????] = 0 or 1????????????????????????????????????
int pasta[101];
int sum;
int n;
//menue[k][a][b]
const int NG = 0, OK = 1;

int pasta_decision(int nannitime=0, int kyou=0, int kinou=0, int ototoi=0)
{
	if (kyou && kyou == kinou && kinou == ototoi) return NG;
	if (pasta[nannitime] != 0 && pasta[nannitime] != kyou) return NG;
	if (nannitime == n) return OK;
	if (menu[nannitime][kyou][kinou][ototoi] != -1) return menu[nannitime][kyou][kinou][ototoi];
	return menu[nannitime][kyou][kinou][ototoi] =
		   (pasta_decision(nannitime + 1, 1, kyou, kinou) +
	       pasta_decision(nannitime + 1, 2, kyou, kinou) +
		   pasta_decision(nannitime + 1, 3, kyou, kinou) ) % 10000;
}

int main()
{
	int k;
	int a, b;
	
	memset(menu, -1, sizeof(menu));
	
	scanf("%d %d", &n, &k);
	for (int i = 0; i < k; i++){
		scanf("%d %d", &a, &b);
		pasta[a] = b;
	}
	printf("%d\n", pasta_decision());
}

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

//dp[??\???][?????\][????????\]
int dp[110][4][4]={};
int det[110]={};
int main(){
	int N,K;
	cin>>N>>K;
	int tmp;
	for(int i=0;i<K;i++){
		cin>>tmp;
		cin>>det[tmp];
	}
	if(det[1]!=0){
		if(det[2]!=0){
			for(int i=1;i<=3;i++){
				dp[3][i][det[2]]=1;
			}
			if(det[1]==det[2])dp[3][det[2]][det[2]]=0;
		}
		else{
			for(int i=1;i<=3;i++){
				for(int j=1;j<=3;j++){
					dp[3][i][j]=1;
				}
			}
			dp[3][det[1]][det[1]]=0;
		}
	}
	else if(det[2]!=0){
		for(int i=1;i<=3;i++){
			dp[3][i][det[2]]=3;
		}
		dp[3][det[2]][det[2]]=2;
	}
	else{
		for(int i=1;i<=3;i++){
			for(int j=1;j<=3;j++){
				if(i==j) dp[3][i][j]=2;
				else dp[3][i][j]=3;
			}
		}
	}
	if(det[3]!=0)for(int i=1;i<=3;i++){
		if(det[3]!=i)for(int j=1;j<=3;j++){
			dp[3][i][j]=0;
		}
	}

	for(int i=3;i<N;i++){
		if(det[i+1]==0)for(int j=1;j<=3;j++){
			for(int k=1;k<=3;k++){
				if(j==k) dp[i+1][j][k]=dp[i][k][1]+dp[i][k][2]+dp[i][k][3]-dp[i][k][k];
				else dp[i+1][j][k]=dp[i][k][1]+dp[i][k][2]+dp[i][k][3];
				dp[i+1][j][k]%=10000;
			}
		}
		else{
			for(int k=1;k<=3;k++){
				if(det[i+1]==k) dp[i+1][det[i+1]][k]=dp[i][k][1]+dp[i][k][2]+dp[i][k][3]-dp[i][k][k];
				else dp[i+1][det[i+1]][k]=dp[i][k][1]+dp[i][k][2]+dp[i][k][3];
				dp[i+1][det[i+1]][k]%=10000;
			}
		}
	}

	int sum=0;
	for(int i=1;i<=3;i++){
		for(int j=1;j<=3;j++){
			sum+=dp[N][i][j];
		}
	}
	cout<<sum%10000<<endl;
	return 0;
}