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<string>
#include<vector>
#include<stdio.h>
#include<algorithm>
#include <string.h>
int dp[105][3]={0};
int plan[105][3];
using namespace std;
int main(){
	int N,K;
	cin>>N>>K;
	int a,b;
	for(int i=0;i<105;i++){
		for(int j=0;j<3;j++){
			plan[i][j]=1;
		}
	}
	for(int i=0;i<K;i++){
		cin>>a>>b;
		b--;
		b=(b+1)%3;
		plan[a][b]=0;
		b=(b+1)%3;
		plan[a][b]=0;		
	}
	
	for(int i=1;i<=N;i++){
		if(i==1){
			dp[i][0]=plan[i][0];
			dp[i][1]=plan[i][1];
			dp[i][2]=plan[i][2];
		}else if(i==2){
			for(int k=0;k<3;k++){
				dp[i][k]=(dp[i-1][0]+dp[i-1][1]+dp[i-1][2])%10000;
			}
		}else{
			dp[i][0]=(dp[i-1][1]+dp[i-1][2]+plan[i-1][0]*(dp[i-2][1]+dp[i-2][2]))%10000;
			dp[i][1]=(dp[i-1][0]+dp[i-1][2]+plan[i-1][1]*(dp[i-2][0]+dp[i-2][2]))%10000;
			dp[i][2]=(dp[i-1][0]+dp[i-1][1]+plan[i-1][2]*(dp[i-2][0]+dp[i-2][1]))%10000;
		}
		
		for(int j=0;j<3;j++){
			dp[i][j]*=plan[i][j];
		}
		
	}
	/*for(int i=1;i<=N;i++){
		cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
	}*/
	
	cout<<(dp[N][0]+dp[N][1]+dp[N][2])%10000<<endl;
	
	
}

C++: #include<iostream>
using namespace std;
int main(){
	long ans=0;
	int n,k,a,b,i,i2,i3,kk[101][4],dp[101][4];//dp[??\][?????????]
	cin>>n>>k;
	for(i=0;i<=100;i++){
		for(i2=0;i2<=3;i2++){
			dp[i][i2]=0;
			kk[i][i2]=0;
		}
	}
	for(i=1;i<=k;i++){
		cin>>a>>b;
		kk[a][b]=1;
		if(b==1){
			dp[a][2]=-1;
			dp[a][3]=-1;
		}
		else if(b==2){
			dp[a][1]=-1;
			dp[a][3]=-1;
		}
		else if(b==3){
			dp[a][1]=-1;
			dp[a][2]=-1;
		}
	}
	for(i=1;i<=n;i++)for(i2=1;i2<=3;i2++)if(kk[i][i2]==1&&kk[i+2][i2]==1)dp[i+1][i2]=-1;
	for(i=1;i<=3;i++){
		if(dp[1][i]!=-1){
			dp[1][i]=1;
		}
	}
	for(i=1;i<n;i++){
		for(i2=1;i2<=3;i2++){
			if(dp[i][i2]>0){
				for(i3=1;i3<=3;i3++){
					if(i2==i3){
						if(dp[i-1][i2]<=0){
							if(dp[i+1][i3]!=-1){
								dp[i+1][i3]+=dp[i][i2];
							}
						}
					}
					else if(dp[i+1][i3]!=-1){
							dp[i+1][i3]+=dp[i][i2];
					}
				}
			}
		}
	}
	for(i=1;i<=3;i++){
		if(dp[n][i]!=-1){
			ans+=dp[n][i];
		}
	}
	cout<<ans%10000<<endl;

	/*for(i=1;i<=n;i++){
		for(i2=1;i2<=3;i2++){
			cout<<dp[i][i2]<<' ';
			if(dp[i][i2]!=-1){
				cout<<' ';
			}
		}
		cout<<endl;
	}*/
}

C++: #include <bits/stdc++.h>

using namespace std;

int N, K;
int C[101];
int dp[101][4][4];

int solve(int n, int b1, int b2)
{
  if(n == N) return 1;
  int &ret = dp[n][b1][b2];
  if(~ret) return ret;
  if(~C[n] && (b1 != C[n] || b2 != C[n])) return ret = solve(n + 1, C[n], b1) % 10000;
  else if(~C[n]) return ret = 0;
  ret = 0;
  for(int i = 0; i < 3; i++){
    if(b1 == i && b2 == i) continue;
    ret = (ret + solve(n + 1, i, b1)) % 10000;
  }
  return ret % 10000;
}

int main()
{
  cin >> N >> K;
  memset(C, -1, sizeof(C));
  for(int i = 0; i < K; i++){
    int A, B;
    cin >> A >> B;
    A--, B--;
    C[A] = B;
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 3, 3) % 10000 << endl;
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int N, K;
int C[101];
int dp[101][3][3];

int solve(int n, int b1, int b2)
{
  if(n == N) return 1;
  int &ret = dp[n][b1][b2];
  if(~ret) return ret;
  if(~C[n] && (b1 != C[n] || b2 != C[n])) return ret = solve(n + 1, C[n], b1) % 10000;
  else if(~C[n]) return ret = 0;
  ret = 0;
  for(int i = 0; i < 3; i++){
    if(b1 == i && b2 == i) continue;
    ret += solve(n + 1, i, b1) % 10000;
  }
  return ret;
}

int main()
{
  cin >> N >> K;
  memset(C, -1, sizeof(C));
  for(int i = 0; i < K; i++){
    int A, B;
    cin >> A >> B;
    A--, B--;
    C[A] = B;
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, -1, -1) % 10000 << endl;
  return 0;
}

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

int n,k;
int memo[4][4][4][102];
map <int,int> ma;

int rec(int b,int y,int t,int idx){
    if(b == y && y == t) return 0;
    if(ma[idx] != '\0' && ma[idx] != t) return 0;
    if(idx == n) return 1;
    if(memo[b][y][t][idx] != 0) return memo[b][y][t][idx];
    return memo[b][y][t][idx] = (rec(y,t,1,idx+1) + rec(y,t,2,idx+1) + rec(y,t,3,idx+1))%10000;
}

int main(){
    cin >> n >> k;
    for(int i=0;i<k;i++){
        int t,m; cin >> t >> m; ma[t] = m;
    }
    int ans = (rec(0,0,1,1) + rec(0,0,2,1) + rec(0,0,3,1))%10000;
    cout << ans << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;
const int mod = 10000;
int n,k;
int y[102];
int dp[102][4][4];
int main(){
	cin >> n >> k;
	for(int i = 0;i < k;i++){
		int t;
		cin >> t;
		cin >> y[t];
	}
	for(int i = 1;i <= 3;i++){
		if(y[1] >= 1 && y[1] != i)continue;
		dp[1][1][i] = 1;
	}
	for(int i = 2;i <= n;i++){
		for(int j = 1;j <= 3;j++){
			if(y[i] >= 1 && y[i] != j)continue;
			for(int q = 1;q <= 3;q++){
				if(q == j)continue;
				dp[i][1][j] += dp[i-1][1][q] + dp[i-1][2][q];
			}
			dp[i][2][j] += dp[i-1][1][j];
			dp[i][1][j] %= mod;
			dp[i][2][j] %= mod;
		}
	}
	int ans = 0;
	for(int i = 1;i <= 3;i++){
		for(int j = 1;j <= 2;j++){
			ans += (dp[n][j][i]);
			ans %- mod;
		}
	}
	cout << ans % mod << endl;
	return 0;
}