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;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int> > a(k);
  for(int i = 0; i < k; i++){
    cin >> a[i].first >> a[i].second;
    a[i].second %= 3;
  }
  sort(a.begin(), a.end());
  a.push_back(make_pair(-1, -1));
  int dp[101][3][3] = {0};
  int now = 0;
  for(int i = 0; i < 3; i++){
    dp[1][i][1] = 1;
  }
  if(a[0].first == 1){
    ++now;
    for(int i = 0; i < 3; i++){
      if(a[0].second == i) continue;
      dp[1][i][1] = 0;
    }
  }
  for(int i = 2; i <= n; i++){
    if(a[now].first == i){
      for(int K = 0; K < 3; K++){
        if(a[now].second == K) continue;
        dp[i][a[now].second][1] += dp[i - 1][K][1] + dp[i - 1][K][2];
        dp[i][a[now].second][1] %= 10000; 
      }
      dp[i][a[now].second][2] += dp[i - 1][a[now].second][1];
      dp[i][a[now].second][2] %= 10000;
      ++now;
      continue;
    }
    for(int j = 0; j < 3; j++){
      for(int K = 0; K < 3; K++){
        if(j == K) continue;
        dp[i][j][1] += dp[i - 1][K][1] + dp[i - 1][K][2];
        dp[i][j][1] %= 10000; 
      }
      dp[i][j][2] += dp[i - 1][j][1];
      dp[i][j][2] %= 10000;
    }
  }
  int ans = 0;
  for(int i = 0; i < 3; i++){
    ans += dp[n][i][1] + dp[n][i][2];
    ans %= 10000;
  }
  cout << ans << "\n";
}

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

using namespace std;

int n, k;

int sche[100];

int dp[100][4][4];

int rec(int day, int a, int b) // a = i - 2 ??\???, b = i - 1 ??\???
{
	if (day == n) return 1;

	if (dp[day][a][b] >= 0) return dp[day][a][b];
	
	int res = 0;
	
	if (sche[day]) {
		if (sche[day] != a || sche[day] != b) {
			res += rec(day + 1, b, sche[day]);
			res %= 10000;
		}
	} else {
		for (int i = 1; i <= 3; ++i) {
			if (a == b && b == i) continue;
			res += rec(day + 1, b, i);
			res %= 10000;
		}
	}

	return dp[day][a][b] = res;
}

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

	for (int i = 0; i < k; ++i) {
		int day; cin >> day; --day;
		cin >> sche[day];
	}

	memset(dp, -1, sizeof(dp));

    int ans = 0;

    if (sche[0] && sche[1]) {
        ans += rec(2, sche[0], sche[1]) % 10000;
    } else if (sche[0]) {
        for (int j = 1; j <= 3; ++j) {
            ans += rec(2, sche[0], j);
            ans %= 10000;
        }
    } else if (sche[1]) {
        for (int i = 1; i <= 3; ++i) {
            ans += rec(2, i, sche[1]);
            ans %= 10000;
        }
    } else {
        for (int i = 1; i <= 3; ++i) for (int j = 1; j <= 3; ++j) {
            ans += rec(2, i, j);
            ans %= 10000;
        }
    }

	printf("%d\n", ans);
}

C++: // 2013/11/09 Tazoe

#include <iostream>
using namespace std;

int main()
{
	int N, K;
	cin >> N >> K;

	int DP[100][3];
	for(int i=0; i<N; i++){
		for(int j=0; j<3; j++){
			DP[i][j] = 0;
		}
	}

	for(int i=0; i<K; i++){
		int A, B;
		cin >> A >> B;

		if(B==1){
			DP[A-1][1] = DP[A-1][2] = -1;
		}
		else if(B==2){
			DP[A-1][0] = DP[A-1][2] = -1;
		}
		else if(B==3){
			DP[A-1][0] = DP[A-1][1] = -1;
		}
	}

	for(int j=0; j<3; j++){
		if(DP[0][j]==-1)
			continue;

		DP[0][j] = 1;
	}

	for(int j=0; j<3; j++){
		if(DP[1][j]==-1)
			continue;

		int s = 0;
		for(int k=0; k<3; k++){
			if(DP[0][k]==-1)
				continue;

			s += DP[0][k];
		}

		DP[1][j] = s;
	}

	for(int i=2; i<N; i++){
		for(int j=0; j<3; j++){
			if(DP[i][j]==-1)
				continue;

			int s = 0;
			for(int k=0; k<3; k++){
				if(DP[i-1][k]==-1)
					continue;

				s += DP[i-1][k];
			}

			DP[i][j] = s%10000;

			if(DP[i-1][j]!=-1){
				int t = 0;
				for(int k=0; k<3; k++){
					if(DP[i-2][k]==-1)
						continue;

					if(j==k)
						continue;

					t += DP[i-2][k];
				}

				DP[i][j] = (DP[i][j]-(DP[i-1][j]-t+20000)+30000)%10000;
			}
		}
	}
/*
	for(int i=0; i<N; i++){
		for(int j=0; j<3; j++){
			cout << DP[i][j] << ' ';
		}
		cout << endl;
	}
*/

	int s = 0;
	for(int j=0; j<3; j++){
		if(DP[N-1][j]==-1)
			continue;

		s += DP[N-1][j];
	}

	cout << s%10000 << endl;

	return 0;
}

C++: #include<cstdio>
#include<cstring>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int n,fixed[100];

int dp[100][4][3];
int dfs(int i,int pre,int con){ // 今何日目か, 前日のパスタ, pre が何回連続したか
	if(con==3) return 0;
	if(i==n) return 1;

	if(dp[i][pre][con]!=-1) return dp[i][pre][con];

	int res=0;
	if(fixed[i]==0){
		for(int j=1;j<=3;j++){
			res+=dfs(i+1,j,pre==j?con+1:1);
		}
	}
	else{
		res=dfs(i+1,fixed[i],pre==fixed[i]?con+1:1);
	}
	return dp[i][pre][con]=res%10000;
}

int main(){
	int k; scanf("%d%d",&n,&k);
	rep(i,k){
		int a,b; scanf("%d%d",&a,&b); a--;
		fixed[a]=b;
	}

	memset(dp,-1,sizeof dp);
	printf("%d\n",dfs(0,0,0));

	return 0;
}