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>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define TO_STRING(a) #a
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }
template<class T> void MSG(T a){ cout << TO_STRING(a) << " = " << a << endl; }


/// memo
const int MOD = 10000;
vint scj(100, -1);
vector<vector<vint> > memo(100, vector<vint>(3, vint(3, -1)));

// p : ê
// k : êÂOÌIð
// c : ÌA±
int rec(int& n, int p, int k, int c)
{
	if(p == n) return 1;
	if(0 <= memo[p][k][c]) return memo[p][k][c];
	
	
	int res = 0;
	
	// ùè
	if(0 <= scj[p])
	{
		if(k == scj[p] && c == 2)
			return 0;
		
		res = rec(n, p + 1, scj[p], (k == scj[p] ? c + 1 : 1));
	}
	// ¢è
	else
	{
		rep(i, 3)
		{
			if(k != i || c != 2)
			{
				(res += rec(n, p + 1, i, (k == i ? c + 1 : 1))) %= MOD;
			}
		}
	}
	
	return memo[p][k][c] = res;
}


int main()
{
	int n, k;
	cin >> n >> k;
	
	rep(i, k)
	{
		int a, b;
		cin >> a >> b;
		
		scj[a-1] = b-1;
	}
	
	cout << rec(n, 0, 0, 0) << endl;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<functional>
using namespace std;
long long int dp[101][4][4];
int main(void)
{
	int i,j,k,l,n,m,a,b[101],c;
	long long int sum,x;
	cin>>n>>m;
	for(i=1;i<=n;i++){
		for(j=1;j<=3;j++){
			for(k=0;k<=3;k++) dp[i][j][k]=0;
		}
	}
	for(i=1;i<=n;i++) b[i]=0;
	for(i=1;i<=m;i++){
		cin>>a;
		cin>>b[a];
	}
	if(b[1]>0) dp[1][b[1]][0]=1;
	else{
		for(i=1;i<=3;i++) dp[1][i][0]=1;
	}
	for(i=1;i<n;i++){
		for(j=1;j<=3;j++){
			if(dp[i][j][0]+dp[i][j][1]+dp[i][j][2]+dp[i][j][3]>0){
				for(k=0;k<=3;k++){
					if(b[i+1]>0){
						if(b[i+1]!=k||b[i+1]!=j){
							dp[i+1][b[i+1]][j]+=dp[i][j][k];
							dp[i+1][b[i+1]][j]%=10000;
						}
					}
					else{
						for(l=1;l<=3;l++){
							if(k!=l||j!=l){
								dp[i+1][l][j]+=dp[i][j][k];
								dp[i+1][l][j]%=10000;
							}
						}
					}
				}
			}
		}
	}
	sum=0;
	for(i=1;i<=3;i++){
		for(j=0;j<=3;j++) sum+=dp[n][i][j];
	}
	x=sum%10000;
	cout<<x<<endl;	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
static const int MAX_N = 100;
static const int MOD = 10000;
static const int NIL = -1;

int N, K;
int menu[MAX_N];
int memo[MAX_N][3][3][3];

int main(){
	cin >> N >> K;
	fill(menu, menu + N, NIL);
	fill(memo[0][0][0], memo[N][0][0], 0);
	for(int i = 0; i < K; i++){
		int a, b;
		cin >> a >> b;
		a--; b--;
		menu[a] = b;
	}
	for(int i = 0; i < 3; i++){
		if(menu[0] != NIL && i != menu[0]) continue;
		memo[0][0][0][i]++;
	}
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if(menu[1] != NIL && j != menu[1]) continue;
			memo[1][0][i][j] += memo[0][0][0][i];
		}
	}
	for(int i = 2; i < N; i++){
		for(int j = 0; j < 3; j++){
			for(int k = 0; k < 3; k++){
				for(int l = 0; l < 3; l++){
					if(menu[i] != NIL && l != menu[i]) continue;
					if(l == k && l == j) continue;
					for(int m = 0; m < 3; m++){
						memo[i][j][k][l] += memo[i - 1][m][j][k];
						memo[i][j][k][l] %= MOD;
					}
				}
			}
		}
	}
	int ans = 0;
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			for(int k = 0; k < 3; k++){
				ans += memo[N - 1][i][j][k];
				ans %= MOD;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;

int dp[3][110][2], num[110];
int n,k;

int main(){
	rep(i,110) num[i] = -1;
	cin >> n >> k;
	rep(i,k){
		int a,b;
		cin >> a >> b;
		a--; b--;
		num[a] = b;
	}
	if(num[0] == -1) rep(i,3) dp[i][0][0] = 1;
	else dp[num[0]][0][0] = 1;
	for(int i = 1; i < n; i++){
		rep(j,3){
			if(num[i] != -1 && num[i] != j) continue;
			dp[j][i][1] += dp[j][i-1][0];
			dp[j][i][1] %= 10000;
			rep(u,3){
				if(j == u) continue;
				dp[j][i][0] += dp[u][i-1][0]+dp[u][i-1][1];
				dp[j][i][0] %= 10000;
			}
		}
	}
		/*
	rep(i,n){
		rep(j,3){
			rep(u,2){
				cout << dp[j][i][u] << " ";
			}
			cout << endl;
		}
		cout << endl;
	}
	*/
	int ans = 0;
	rep(i,3) rep(j,2) ans += dp[i][n-1][j];
	cout << ans%10000 << endl;
}