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<climits>
#include<cassert>
#include<algorithm>
#include<cassert>
#include<map>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 101;
const int mod = 10000;
int dp[N][3][3];
int solve(int n,int *in){
  rep(i,N)rep(j,3)rep(k,3)dp[i][j][k] = 0;
  rep(i,3){
    if (in[0] != -1 && i != in[0])continue;
    rep(j,3){
      if (in[1] != -1 && j != in[1])continue;
      dp[1][i][j] =1;
    }
  }
  REP(i,2,n){
    rep(j,3){//two day
      rep(k,3){//one day before
	rep(ii,3){//ii-th day
	  if (in[i] != -1 && in[i] != ii)continue;
	  if (j == k && j == ii)continue;//ï¼æ¥é£ç¶åã
	  dp[i][k][ii] += dp[i-1][j][k];
	  dp[i][k][ii] %= mod;
	}
      }
    }
  }
  int ret = 0;
  rep(i,3){
    rep(j,3){
      ret = (ret + dp[n-1][i][j])%mod;
    }
  }
  return ret;
}

main(){
  int n,k;
  while(cin>>n>>k && n){
    int in[n];rep(i,n)in[i] = -1;
    rep(i,k){
      int day,type;
      cin>>day>>type;
      in[day-1] = type-1;
    }
    cout << solve(n,in) << endl;
  }
}

C++: // 2012/02/23 Tazoe

#include <iostream>
using namespace std;

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

	int DP[101][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] = DP[A][2] = -1;
		else if(B==2)
			DP[A][0] = DP[A][2] = -1;
		else if(B==3)
			DP[A][0] = DP[A][1] = -1;
	}

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

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

		if(DP[2][j]!=-1)
			DP[2][j] = sum;
	}

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

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

			int sub = 0;
			if(DP[i-1][j]!=-1){
				sub = DP[i-1][j]+20000;

				for(int k=0; k<3; k++){
					if(j==k)
						continue;

					if(DP[i-2][k]!=-1)
						sub -= DP[i-2][k];
				}

				sub %= 10000;
			}

			DP[i][j] = (sum+10000-sub)%10000;
		}
	}

//	for(int i=0; i<=N; i++)
//		cout << DP[i][0] << ' ' << DP[i][1] << ' ' << DP[i][2] << endl;

	int sum2 = 0;
	for(int j=0; j<3; j++)
		if(DP[N][j]!=-1)
			sum2 += DP[N][j];

	cout << sum2%10000 << endl;

	return 0;
}

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

typedef pair<int, int> P;

int d1[101], d2[101], d3[101], s1[101], s2[101], s3[101], sta[101];
int N, K;

void search(int n){
	
	if(n == N) return;
	
	if(n == 0){
		if(sta[0] == 1) s1[0] = 1;
		if(sta[0] == 2) s2[0] = 1;
		if(sta[0] == 3) s3[0] = 1;
		if(sta[0] == 0)s1[0] = s2[0] = s3[0] = 1;
		//cout << d1[0] << d2[0] << d3[0] << s1[0] << s2[0] << s3[0] << endl;
		search(1);
		return;
	}
	
	if(sta[n] == 1){
		d1[n] = s1[n - 1];
		s1[n] = d2[n - 1] + d3[n - 1] + s2[n - 1] + s3[n - 1];
		
	}
	if(sta[n] == 2){
		d2[n] = s2[n - 1];
		s2[n] = d1[n - 1] + d3[n - 1] + s1[n - 1] + s3[n - 1];
		
	}
	if(sta[n] == 3){
		d3[n] = s3[n - 1];
		s3[n] = d1[n - 1] + d2[n - 1] + s1[n - 1] + s2[n - 1];
		
	}
	if(sta[n] == 0){
		d1[n] = s1[n - 1];
		d2[n] = s2[n - 1];
		d3[n] = s3[n - 1];
		s1[n] = d2[n - 1] + d3[n - 1] + s2[n - 1]  + s3[n - 1];
		s2[n] = d1[n - 1] + d3[n - 1] + s1[n - 1]  + s3[n - 1];
		s3[n] = d1[n - 1] + d2[n - 1] + s1[n - 1]  + s2[n - 1];
	}
	
	if(sta[n + 1] != 0){
	
		if(sta[n + 1] == sta[n + 2]){
			if(sta[n + 1] == 1) s1[n] = 0; 
			if(sta[n + 1] == 2) s2[n] = 0;
			if(sta[n + 1] == 3) s3[n] = 0;
		}
	}
	//cout << d1[n] << d2[n] << d3[n] << s1[n] << s2[n] << s3[n] << endl;
	search(n + 1);
	return;
}

int main(){
	for(int i = 0;i < 101;i ++)sta[i] = 0;
	d1[0] = d2[0] = d3[0] = s1[0] = s2[0] = s3[0] = 0;
	
	cin >> N >> K;
	int Ai, Bi;
	for(int i = 0;i < K;i ++){
		cin >> Ai >> Bi;
		sta[Ai - 1] = Bi;
	}
	search(0);
	cout << d1[N - 1] + d2[N - 1] + d3[N - 1] + s1[N - 1] + s2[N - 1] + s3[N - 1] << endl;
	
	return 0;
}

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

int N, K, A, B;

int dfs(vector<bool>& dec, vector<int>& pasta,int n)
{
	int res = 0;
	if (n == N + 1)
		return 1;
	if (dec[n]) {
		return dfs(dec, pasta, n + 1);
	}
	for (int i = 1; i <= 3; i++) {
		if ((n - 2 >= 0 && pasta[n - 2] == pasta[n - 1] && pasta[n - 1] == i)
			|| (n - 1 >= 0 && n + 1 <= N && dec[n + 1] == 1 && pasta[n - 1] == pasta[n + 1] && pasta[n - 1] == i)
			|| (n + 2 <= N && dec[n + 1] == 1 && dec[n + 2] == 1 && pasta[n + 1] == pasta[n + 2] && pasta[n + 1] == i))
			continue;
		pasta[n] = i;
		res += dfs(dec, pasta, n + 1);
	}
	return res;
}

int main()
{
	cin >> N >> K;
	vector<bool> dec(N + 1);
	vector<int> pasta(N + 1);
	for (int i = 0; i < K; i++) {
		cin >> A >> B;
		dec[A] = 1;
		pasta[A] = B;
	}
	cout << dfs(dec, pasta,1) % 10000 << endl;
	return 0;
}