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));
    }
  }

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

  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 <iostream>

using namespace std;

int main() {
	int n, k;
	cin >> n >> k;

	int data[110] = {0};


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

	int dp[110][9] = {0};
	// 0:11, 1:12, 2:13, 3:21, 4:22, 5:23, 6:31, 7:32, 8:33 
	if (data[1] == 0) {
		if (data[2] == 0) {
			for (int i = 0; i < 9; i++) {
				dp[2][i] = 1;
			}
		} else if (data[2] == 1) {
			dp[2][0] = dp[2][3] = dp[2][6] = 1;
		} else if (data[2] == 2) {
			dp[2][1] = dp[2][4] = dp[2][7] = 1;
		} else {
			dp[2][2] = dp[2][5] = dp[2][8] = 1;
		}
	} else if (data[1] == 1) {
		if (data[2] == 0) {
			dp[2][0] = dp[2][1] = dp[2][2] = 1;
		} else if (data[2] == 1) {
			dp[2][0] = 1;
		} else if (data[2] == 2) {
			dp[2][1] = 1;
		} else {
			dp[2][2] = 1;
		}
	} else if (data[1] == 2) {
		if (data[2] == 0) {
			dp[2][3] = dp[2][4] = dp[2][5] = 1;
		} else if (data[2] == 1) {
			dp[2][3] = 1;
		} else if (data[2] == 2) {
			dp[2][4] = 1;
		} else {
			dp[2][5] = 1;
		}
	} else {
		if (data[2] == 0) {
			dp[2][6] = dp[2][7] = dp[2][8] = 1;
		} else if (data[2] == 1) {
			dp[2][6] = 1;
		} else if (data[2] == 2) {
			dp[2][7] = 1;
		} else {
			dp[2][8] = 1;
		}
	}
	// 0:11, 1:12, 2:13, 3:21, 4:22, 5:23, 6:31, 7:32, 8:33 
	for (int i = 3; i <= n; i++) {
		if (data[i] == 0) {
			dp[i][0] += (dp[i-1][3]+dp[i-1][6])%10000;
			dp[i][1] += (dp[i-1][0]+dp[i-1][3]+dp[i-1][6])%10000;
			dp[i][2] += (dp[i-1][0]+dp[i-1][3]+dp[i-1][6])%10000;
			dp[i][3] += (dp[i-1][1]+dp[i-1][4]+dp[i-1][7])%10000;
			dp[i][4] += (dp[i-1][1]+dp[i-1][7])%10000;
			dp[i][5] += (dp[i-1][1]+dp[i-1][4]+dp[i-1][7])%10000;
			dp[i][6] += (dp[i-1][2]+dp[i-1][5]+dp[i-1][8])%10000;
			dp[i][7] += (dp[i-1][2]+dp[i-1][5]+dp[i-1][8])%10000;
			dp[i][8] += (dp[i-1][2]+dp[i-1][5])%10000;
		} else if (data[i] == 1) {
			dp[i][0] += (dp[i-1][3]+dp[i-1][6])%10000;
			dp[i][3] += (dp[i-1][1]+dp[i-1][4]+dp[i-1][7])%10000;
			dp[i][6] += (dp[i-1][2]+dp[i-1][5]+dp[i-1][8])%10000;
		} else if (data[i] == 2) {
			dp[i][1] += (dp[i-1][0]+dp[i-1][3]+dp[i-1][6])%10000;
			dp[i][4] += (dp[i-1][1]+dp[i-1][7])%10000;
			dp[i][7] += (dp[i-1][2]+dp[i-1][5]+dp[i-1][8])%10000;
		} else {
			dp[i][2] += (dp[i-1][0]+dp[i-1][3]+dp[i-1][6])%10000;
			dp[i][5] += (dp[i-1][1]+dp[i-1][4]+dp[i-1][7])%10000;
			dp[i][8] += (dp[i-1][2]+dp[i-1][5])%10000;
		}
	}
	int cc = 0;
	for (int i = 0; i < 9; i++) {
		cc += dp[n][i];
		cc %= 10000;
	}
	cout << cc << endl;
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
using namespace std;
typedef long long ll;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int n, k;
int plan[MAX_N];
int memo[4][4][MAX_N];
int dfs(int a, int b, int depth)
{
    if(memo[a][b][depth]) return memo[a][b][depth];
    if(depth > n) return 1;
    int res = 0;
    if(plan[depth])
    {
        if(not(a == b && b == plan[depth])) res += dfs(b, plan[depth], depth + 1);
    }
    else
    {
        for(int c = 1; c <= 3; c++)
        {
            if(a == b && b == c) continue;
            res += dfs(b, c, depth + 1);
        }
    }
    return memo[a][b][depth] = res % 10000;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> k;
    memset(memo, 0, sizeof(memo));
    for(int i = 0; i < k; i++)
    {
        int a, b; cin >> a >> b;
        plan[a] = b;
    }
    cout << dfs(0, 0, 1) % 10000 << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define loop(i,n) for(int i=1;i<=n;i++)
int dp[102][4][4];
int ket[102];

int main(){
  int n,k,a,b;
  cin>>n>>k;
  loop(i,k){
    cin>>a>>b;
    ket[a]=b;
  }
  loop(s1,3){
    if(ket[1]&&ket[1]!=s1)continue;
    loop(s2,3){
      if(ket[2]&&ket[2]!=s2)continue;
      dp[2][s1][s2]=1;
    }
  }
  loop(i,n){
    if(i<=2)continue;
    loop(s3,3){
      if(ket[i]&&ket[i]!=s3)continue;
      loop(s1,3){
        loop(s2,3){
          if(s1==s2&&s2==s3)continue;
          dp[i][s2][s3]+=dp[i-1][s1][s2];
          dp[i][s2][s3]%=10000;
        }
      }
    }
  }
  int sum=0;
  loop(s1,3)loop(s2,3)sum+=dp[n][s1][s2];
  sum%=10000;
  cout<<sum<<endl;
  return 0;
}