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

using namespace std;

int n,k;

int dp[100][3][2];
int yt[100];

const int MOD = 10000;

int next(int a){
	return (a + 1) % 3;
}
int prev(int a){
	return (a - 1 + 3) % 3;
}

int main(){
	scanf("%d %d",&n,&k);
	int a,b;
	for(int i=0;i<100;i++){
		yt[i] = -1;
	}
	for(int i=0;i<k;i++){
		scanf("%d %d",&a,&b);
		yt[a-1] = b-1;
	}
	if(yt[0] != -1){
		dp[0][yt[0]][0] = 1;
	}else{
		for(int j=0;j<3;j++){
			dp[0][j][0] = 1;
		}
	}
	for(int i=1;i<n;i++){
		if(yt[i] != -1){
			dp[i][yt[i]][0] = (
				dp[i - 1][next(yt[i])][0] +
				dp[i - 1][next(yt[i])][1] +
				dp[i - 1][prev(yt[i])][0] +
				dp[i - 1][prev(yt[i])][1]
			                  ) % MOD;
			dp[i][yt[i]][1] = (
				dp[i - 1][yt[i]][0]
							  ) % MOD;
		}else{
			for(int j=0;j<3;j++){
				dp[i][j][0] = (
					dp[i - 1][next(j)][0] +
					dp[i - 1][next(j)][1] +
					dp[i - 1][prev(j)][0] +
					dp[i - 1][prev(j)][1]
					              ) % MOD;
				dp[i][j][1] = (
					dp[i - 1][j][0]
								  ) % MOD;
			}
		}
	}
	//for(int i=0;i<n;i++){
	//	printf("%d:(%d,%d),(%d,%d),(%d,%d)\n",yt[i] + 1,dp[i][0][0],dp[i][0][1],dp[i][1][0],dp[i][1][1],dp[i][2][0],dp[i][2][1]);
	//}
	int ans = 0;
	if(yt[n - 1] != -1){
		ans += dp[n-1][yt[n-1]][0] + dp[n-1][yt[n-1]][1];
		ans %= MOD;
	}else{
		for(int i=0;i<3;i++){
			ans += dp[n-1][i][0] + dp[n-1][i][1];
			ans %= MOD;
		}
	}
	printf("%d\n",ans);
}

C++: #include <bits/stdc++.h>
#define MOD 10000
using namespace std;
 
int P[100];
int n, k;
int memo[4][101][4];
 
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;
    res += search(ren+1, d+1, i) % MOD;
      }else{
    res += search(1, d+1, i) % MOD;
      }
    }
  }else{
    if(p == P[d]){
      if(ren + 1 >= 3) res = 0;
      else res += search(ren+1, d+1, P[d]) % MOD;
    }
    else res += search(1, d+1, P[d]) % MOD;
  }
 
  memo[ren][d][p] = res % MOD;
  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 < 4; i++) for(int j = 0; j < 100; j++) for(int k = 0; k < 4; 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, 0) << endl;
 
  return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int N, K;
int memo[100][3][3];
int p_fixed[100];
int mod = 10000;

int pasta(int n, int y2, int y1) {
	if (n == N) return 1;
	if (memo[n][y2][y1] > -1) return memo[n][y2][y1];
	memo[n][y2][y1] = 0;

	if (p_fixed[n] > -1) {
		if (! (y2 == y1 && y1 == p_fixed[n])) memo[n][y2][y1] += pasta(n + 1, y1, p_fixed[n]);
	} else {
		REP(i, 3) if (! (y2 == y1 && y1 == i)) 
			memo[n][y2][y1] += pasta(n + 1, y1, i);
	}

	return memo[n][y2][y1];
}

int main() {

	fill(memo[0][0], memo[100][0], -1);
	fill(p_fixed, p_fixed + 100, -1);

	cin >> N >> K;
	REP(i, K) {
		int a, b;
		scanf("%d %d", &a, &b);
		p_fixed[a - 1] = b - 1;
	}

	int ans = 0;
	if (p_fixed[0] == -1) {
		if (p_fixed[1] == -1)
			REP(i, 3) REP(j, 3) ans += pasta(2, i, j);
		else
			REP(i, 3) ans += pasta(2, i, p_fixed[1]);
	} else {
		if (p_fixed[1] == -1)
			REP(i, 3) ans += pasta(2, p_fixed[0], i);
		else
			ans += pasta(2, p_fixed[0], p_fixed[1]);
	}

	cout << ans % mod << endl;
	
	return 0;
}

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

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