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 <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem0568 : Pasta **/
int N, K;
vector<int> A;
const int MOD = 10000;
int dp[101][3][3];

int solve(int n, int old1, int old2)
{
	if (n >= N)
		return 1;
	
	if (dp[n][old1][old2]!=-1)
		return dp[n][old1][old2];
	
	int res=0;
	if  (A[n] != 0) {
		if (old1 != old2 || old2 != A[n])
			res += solve(n+1, old2, A[n]);
		res %= MOD;
	} else {
		rep(i, 3) {
			if (old1 != old2 || old2 != i+1)
				res += solve(n+1, old2, i+1);
			res %= MOD;
		}
	}
	return dp[n][old1][old2] = res;
}

int main()
{
	cin>>N>>K;
	A.resize(N);
	fill(A.begin(), A.end(), 0);
	memset(dp, -1, sizeof(dp));
	
	rep(i, K) {
		int a, b;
		cin>>a>>b;
		A[a-1] = b;
	}
	cout << solve(0, 0, 0) << endl;
}

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

int n,k,a,b,p[100];

int fanc(int i,int x,int y,int z)
{
  if(i==0){return 1;}
  else{
    int c[3][3][3];
    for(int j1=0;j1<3;j1++){
      c[x][y][j1]+=fanc(i--,j1,x,y);
    }
  
    for(int j1=0;j1<3;j1++){
      c[j1][j1][j1]=0;
    }

    if(p[i]){
      for(int j1=0;j1<3;j1++){
	for(int j2=0;j2<3;j2++){
	  for(int j3=0;j3<3;j3++){
	    if(j1!=p[i]){c[j1][j2][j3]=0;}
	  }
	}
      }
    }

    if(p[i+1]){
      for(int j1=0;j1<3;j1++){
	for(int j2=0;j2<3;j2++){
	  for(int j3=0;j3<3;j3++){
	    if(j2!=p[i]){c[j1][j2][j3]=0;}
	  }
	}
      }
    }

    if(p[i+2]){
      for(int j1=0;j1<3;j1++){
	for(int j2=0;j2<3;j2++){
	  for(int j3=0;j3<3;j3++){
	    if(j3!=p[i]){c[j1][j2][j3]=0;}
	  }
	}
      }
    }

    
    for(int j1=0;j1<3;j1++){
      for(int j2=0;j2<3;j2++){
	for(int j3=0;j3<3;j3++){
	  if(c[j1][j2][j3]>10000){c[j1][j2][j3]%=10000;}
	}
      }
    }

    return c[x][y][z];
  }
}

int total()
{
  int sum;
  for(int j1=0;j1<3;j1++){
    for(int j2=0;j2<3;j2++){
      for(int j3=0;j3<3;j3++){
	sum+=fanc(n-3,j1,j2,j3);
      }
    }
  }
  return sum;
}
   
      
int main()
{
  cin>>n>>k;
  for(int i=0;i<k;i++){
    cin>>a>>b;
    p[a-1]=b;
  }

  cout<<total()%10000<<endl;
  return 0;
}

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

# define MAX_N 100
# define MOD 10000

int n, K;

int plan[MAX_N+1];
int memo[MAX_N+1][4][4];

int solve(int day,int t,int y){
	if (day == n + 1)
		return 1;
	if (memo[day][t][y] != -1)
		return memo[day][t][y];
	if (plan[n + 1] != 0){
		if (t == y&&t == plan[n + 1])
			return 0;
		else
			return solve(n + 1, plan[n + 1], t)%MOD;
	}

	int result = 0;
	for (int i = 1; i <= 3; ++i){
		if (t == y&&t == i)
			continue;
		else
			result+=solve(n + 1, i, t)%MOD;
	}
	return memo[n][t][y]%MOD;
}

int main(){
	int d, k;
	std::cin >> n >> K;

	for (int i = 0; i < K; ++i){
		std::cin >> d >> k;
		plan[d] = k;
	}

	for (int i = 1; i <= n; ++i){
		for (int j = 1; j < 4; ++j){
			for (int k = 0; k < 4; ++k){
				memo[i][j][k] = -1;
			}
		}
	}

	std::cout << solve(1, 1, 1) << std::endl;

	return 0;
}