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<bits/stdc++.h>
using namespace std;

long long int dp[100][4][4];
int plan[100], N, K;

long long int solve(int today, int before1, int before2) {
  if( today == N ) return 1;
  if( dp[today][before1][before2] != -1 ) 
    return (dp[today][before1][before2]);
  if( plan[today] ) {
    if( plan[today] == before1 && before1 == before2 ) {
      return 0;
    } 
    else {
      return (solve(today+1, plan[today], before1) % 10000);
    }
  }

  long long int ret = 0;
  for(int i=1; i<=3; i++) {
    if( i != before1 || before1 != before2 ) {
      ret += solve(today+1, i, before1) % 10000;
    }
  }

  return dp[today][before1][before2] = ret;
} 
	      


int main() {
  cin >> N >> K;
  memset(plan, 0, sizeof(plan));
  memset(dp, -1, sizeof(dp));

  for(int i=0; i<K; i++) {
    int a, b;
    cin >> a >> b;
    plan[a-1] = b;
  }

  cout << solve(0,0,0) % 10000 << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int n;
int k;
int p[100];
int memo[101][4][4];
int solve(int idx,int t1,int t2){
  if(idx==n) return 1;
  if(memo[idx][t1][t2]!=-1) return memo[idx][t1][t2];
  if(p[idx]>0){
    if(t1==t2 && t2==p[idx]){
      return 0;
    }else{
      return solve(idx+1,t2,p[idx]);
    }
  }else{
    int res=0;
    for(int i=1;i<=3;i++){
      if(t1==t2 && t2==i) continue;
      res+=solve(idx+1,t2,i);
    }
    return memo[idx][t1][t2]=res;
  }
}
int main(){
  cin>>n>>k;
  memset(p,-1,sizeof(p));
  memset(memo,-1,sizeof(memo));
  for(int i=0;i<k;i++){
    int a,b;
    cin>>a>>b;
    p[a-1]=b;
  }
  cout<<solve(0,0,0)%10000<<endl;
}

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

int dp[101][4][4];//何日目から、どのぱすたを、何日連続で食べているか
int plan[101];//先に決められている予定　要素：日付

#define MOD 10000;

int main(void)
{
	while(1){
		int day;
		int k;
		cin >> day >> k;
		memset(plan,0,sizeof(plan));
		for(int i=0;i<k;++i){
			int d,p;
			cin >> d >> p;
			plan[d] = p;
		}
		memset(dp,0,sizeof(dp));

		for(int i=1;i<=3;++i){
			if(plan[1] != 0)
				dp[1][plan[1]][1] = 1;
			else
				dp[1][i][1] = 1;
		}

		for(int d=2;d<=day;++d){//どの日から？
			for(int p=1;p<=3;++p){//どのパスタを？
				if(plan[d]){
					dp[d][plan[d]][1] = (dp[d-1][(plan[d]+1)%3+1][1] + dp[d-1][(plan[d]+1)%3+1][2] + dp[d-1][plan[d]%3+1][1] + dp[d-1][plan[d]%3+1][2])%MOD;
					dp[d][plan[d]][2] = dp[d-1][plan[d]][1] % MOD;
				}
				else{
					dp[d][p][1] = (dp[d-1][(p+1)%3+1][1] + dp[d-1][(p+1)%3+1][2] + dp[d-1][p%3+1][1] + dp[d-1][p%3+1][2])%MOD;
					dp[d][p][2] = dp[d-1][p][1] % MOD;
				}
			}
		}
		int ans = dp[day][1][1] + dp[day][2][1] + dp[day][3][1] + dp[day][1][2] + dp[day][2][2] + dp[day][3][2];
		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define AMA (10000)
int N,K;
int D[111];
int dp[111][4][4];
int solve(int day,int k,int r){
  if(dp[day][k][r]!=-1) return dp[day][k][r];
  if(day==N+1) return dp[day][k][r]=1;
  if(D[day]){
    if(r==2 && D[day]==k) return dp[day][k][r]=0;
    if(D[day]==k)
      return dp[day][k][r]=solve(day+1,D[day],r+1);
    else
      return dp[day][k][r]=solve(day+1,D[day],1);   
  }
  int ret=0;
  for(int i=1;i<=3;i++){
    if(r==2 && i==k) continue;
    if(i==k)
      ret+=solve(day+1,i,r+1);
    else
      ret+=solve(day+1,i,1);
    ret%=AMA;
  }
  return dp[day][k][r]=ret;
}
main(){
  memset(dp,-1,sizeof(dp));
  scanf("%d %d",&N,&K);
  for(int i=0;i<K;i++){
    int a,b;
    scanf("%d %d",&a,&b);
    D[a]=b;
  }
  printf("%d\n",solve(1,0,0)%AMA);
}