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<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 10000;
#define MAX 110
signed main() {
	int N, K; cin >> N >> K;
	int A[MAX] = {};
	fill(A, A + MAX, INF);
	rep(i, 0, K) {
		int a, b; cin >> a >> b;
		A[a] = --b;
	}
	int dp[MAX][3][2] = {};
	rep(j, 0, 3) {
		if (A[1] != INF&&A[1] != j)continue;
		dp[1][j][0] = 1;
	};
	rep(i, 1, N) {
		rep(j, 0, 3) {
			dp[i][j][0] %= MOD;
			dp[i][j][1] %= MOD;
			if (A[i] != INF&&A[i] != j)continue;
			rep(x, 0, 3) {
				int k = 0;
				if (x == j)k = 1;
				dp[i + 1][x][k] += dp[i][j][0];
				if (x != j)dp[i + 1][x][0] += dp[i][j][1];
			}
		}
	}
	//rep(i, 0, N) {
	//	cerr << i;
	//	rep(j, 0, 3) {
	//		int ans = 0;
	//		rep(k, 0, 2) {
	//			if (A[i] != INF&&A[i] != j)continue;
	//			ans += dp[i][j][k];
	//		}
	//		cerr << " " << ans;
	//	}
	//	cerr << endl;
	//}
	int ans = 0;
	rep(j, 0, 3)rep(k, 0, 2) {
		if (A[N] != INF&&A[N] != j)continue;
		ans += dp[N][j][k];
	}
	ans %= MOD;
	cout << ans << endl;
	return 0;
}

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

int P[100];
int n, k;
int memo[3][100][3];

int search(int ren, int d, int p){
  if(d == n) return 1;
  if(memo[ren][d][p] != -1) return memo[ren][d][p];



  int res = 0;
  if(P[d] == 0){
    for(int i = 1; i <= 3; i++){
      if(p == i){
	if(ren + 1 >= 3) continue;
	if(P[d+1] == i && ren + 1 >= 2) continue;
	res += search(ren+1, d+1, i);
      }else{
	res += search(1, d+1, i);
      }
    }
  }else{
    if(p == P[d]) res += search(ren+1, d+1, P[d]);
    else res += search(1, d+1, P[d]);
  }

  memo[ren][d][p] = res % 10000;
  return memo[ren][d][p];
  //return res;
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;
  for(int i = 0; i < 3; i++) for(int j = 0; j < 100; j++) for(int k = 0; k < 3; k++) memo[i][j][k] = -1;
  for(int i = 0; i < 100; i++){
    P[i] = 0;
  }
  for(int i = 0; i < k; i++){
    int d, p;
    cin >> d >> p;
    d--;
    P[d] = p;
  }

  cout << search(1, 0, -1) << endl;

  return 0;
}

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

int dp[114][5][14];//day today tomorrow
int pa[114];//pasta

int main(){
	int n,ka;
	cin>>n>>ka;
	for(int i=0;i<ka;i++){
		int a,b;
		cin>>a>>b;
		pa[a]=b;
		}
		
	if(pa[1]){
		for(int i=0;i<3;i++){
			dp[0][i][pa[1]-1]++;
			}
		}
	else{
		for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)dp[0][i][j]++;
		}		
		
	for(int x=1;x<=n;x++){
		if(pa[x]==1){
			for(int i=0;i<3;i++){
				for(int j=0;j<3;j++){
					if(pa[x+1]==i+1||pa[x+1]==0){
					if(i==j&&i==0)continue;
					dp[x][0][i]+=dp[x-1][j][0];
					dp[x][0][i]%=10000;
					}
					}
				}
			}
		else if(pa[x]==2){
			for(int i=0;i<3;i++){
				for(int j=0;j<3;j++){
					if(pa[x+1]==i+1||pa[x+1]==0){
					if(i==j&&i==1)continue;
					dp[x][1][i]+=dp[x-1][j][1];
					dp[x][1][i]%=10000;
					}
				}
				}
			}
		else if(pa[x]==3){
			for(int i=0;i<3;i++){
				for(int j=0;j<3;j++){
					if(pa[x+1]==i+1||pa[x+1]==0){
					if(i==j&&i==2)continue;
					dp[x][2][i]+=dp[x-1][j][2];
					dp[x][2][i]%=10000;
				}
					}
				}
			}
		else{
			for(int i=0;i<3;i++){
				for(int j=0;j<3;j++){
					for(int k=0;k<3;k++){
						if(pa[x+1]==i+1||pa[x+1]==0){
						if(i==j&&j==k)continue;
						dp[x][j][i]+=dp[x-1][k][j];
						dp[x][j][i]%=10000;
						}
					}
					}
				}
			}				
		}
		int ans=0;
		for(int i=0;i<3;i++)	
		for(int j=0;j<3;j++)ans+=dp[n][i][j];
		cout<<ans%10000<<endl;
		return 0;
	}

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

int main() {
	int n, k, l, m;
	cin >> n >> k;
	int krsw[101][3] = {};
	int a[100] = {} ;
	for(int i = 0; i < k; i++) {
		cin >> l >> m;
		a[l] = m;
	}
	if(a[1]) krsw[1][a[1]] = 1;
	else { krsw[1][1] = 1; krsw[1][2] = 1; krsw[1][3] = 1; }
	for(int hi = 2; hi <= n; hi++) {
		if(a[hi]) {
			krsw[hi][a[hi]] = krsw[hi - 1][1] + krsw[hi - 1][2] + krsw[hi - 1][3];
			if(krsw[hi - 1][a[hi]] > 0 && krsw[hi - 2][a[hi]] > 0) {
				krsw[hi][a[hi]] -= krsw[hi - 2][a[hi]];
				krsw[hi - 1][a[hi]] -= krsw[hi - 2][a[hi]];	
			}
			if(krsw[hi][a[hi]]>10000)krsw[hi][a[hi]] %= 10000;
		}
		else
			for(int i = 1; i <= 3; i++) {
				krsw[hi][i] = krsw[hi - 1][1] + krsw[hi - 1][2] + krsw[hi - 1][3];
				if(krsw[hi - 1][i] > 0 && krsw[hi - 2][i] > 0) {
					krsw[hi][i] -= krsw[hi - 2][i];
					krsw[hi - 1][i] -= krsw[hi - 2][i];
				}
				if(krsw[hi][i]>10000)krsw[hi][i] %= 10000;
			}
	}
	cout << (krsw[n][1] + krsw[n][2] + krsw[n][3]) % 10000 << endl;
	return 0;
}