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<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<utility>
#include<queue>
#include<string.h>
#include<string>
#include<set>
#include<functional>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef queue<P> Q;

#define _CRT_SECURE_NO_WARNINGS

int N=0,K = 0;
int a[105]={};
int dp[5][5][105]={};

int Saiki(int last,int beforeLast,int day){
    if(dp[last][beforeLast][day]!=0){
        return dp[last][beforeLast][day];
    }
    if(day==N){
        return 1;
    }
    if(a[day]!=0){
        return dp[last][beforeLast][day]=(Saiki(a[day],last,day+1)%10000);
    }
    set<int> cantUse;
    if(a[day+1]==a[day+2] && a[day+1]!=0){
        cantUse.insert(a[day+1]);
    }
    if(last==a[day+1]){
        cantUse.insert(last);
    }
    if( last==beforeLast ){
        cantUse.insert(last);
    }
    int temp=0;
    for(int i=1;i<=3;i++){
        if( cantUse.find(i)==cantUse.end() ){
            temp+=(Saiki(i,last,day+1)%10000);
        }
    }
    return dp[last][beforeLast][day]=(temp%10000);
}

int main() {
	cin >> N >> K;
    for(int i=0;i<K;i++){
        int b,c;
        cin >> b>>c;
        a[b-1]=c;
    }
	int i = Saiki(0, 0, 0) % 10000;
    cout << i <<endl;
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

typedef vector<vvi> vvvi;
int main(){
	int n,k;
	cin>>n>>k;
	vi given(n+1);
	REP(i,k){
		int day,kind;
		cin>>day>>kind;
		given[day]=kind;
	}
	vvvi dp(n+1,vvi(3,vi(3)));
	REP(i,3){
		if(!given[1]||given[1]-1==i){
			dp[1][0][i]=1;
		}
	}
	FOR(day,2,n+1){
		REP(prev,3){
			FOR(cur,given[day]?given[day]-1:0,given[day]?given[day]:3){
				REP(pp,3){
					if(prev==cur&&cur==pp&&day!=2)continue;
					dp[day][prev][cur]+=dp[day-1][pp][prev];
					dp[day][prev][cur]%=10000;
				}
			}
		}
	}
	int ans=0;
	REP(i,3){
		REP(j,3){
			ans+=dp[n][i][j];
			ans%=10000;
		}
	}
	cout<<ans<<endl;
}

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

enum Sauce{NOT_DETERMINED=0, Tomato=1, Cream=2, Basil=3};
enum Seq{SAME, DIFF};

const int divisor = 10000;

class Pasta{
public:
	Pasta(int _N);
	~Pasta();

	void sauceFix(int A, Sauce B);
	int schedule(int i, Sauce yesterday, Seq seq);

private:
	int N;
	int** memo;
	Sauce* sauceary;
};

Pasta::Pasta(int _N){
	N = _N;
	sauceary = new Sauce[N+1];
	memo = new int*[N+1];

	for (int i = 0; i <= N; i++) sauceary[i] = NOT_DETERMINED;

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

Pasta::~Pasta(){
	delete[] sauceary;
	delete[] memo;
}

void Pasta::sauceFix(int A, Sauce B){
	sauceary[A] = B;
}

int Pasta::schedule(int i, Sauce yesterday, Seq seq){
	if (i == N+1) return 1;
	if (seq == SAME && yesterday == sauceary[i]) return 0;

	if (sauceary[i] == NOT_DETERMINED){
		switch (yesterday){
		case Tomato: 
			if (seq == SAME) return (schedule(i+1, Cream, DIFF) + schedule(i+1, Basil, DIFF)) % divisor;
			else if (seq == DIFF) return (schedule(i+1, Tomato, SAME) + schedule(i+1, Cream, DIFF) + schedule(i+1, Basil, DIFF)) % divisor;
		case Cream:
			if (seq == SAME) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Basil, DIFF)) % divisor;
			else if (seq == DIFF) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Cream, SAME) + schedule(i+1, Basil, DIFF)) % divisor;
		case Basil:
			if (seq == SAME) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Cream, DIFF)) % divisor;
			else if (seq == DIFF) return (schedule(i+1, Tomato, DIFF) + schedule(i+1, Cream, DIFF) + schedule(i+1, Basil, SAME)) % divisor;
		}
	}
	else {
		if (yesterday == sauceary[i]) return schedule(i+1, sauceary[i], SAME) % divisor;
		else return schedule(i+1, sauceary[i], DIFF) % divisor;
	}
}

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

	Pasta* pasta = new Pasta(N);

	for (int i = 0; i < K; i++){
		int A, B;
		cin >> A >> B;
		pasta->sauceFix(A, (Sauce)B);
	}

	int ans = pasta->schedule(1, NOT_DETERMINED, DIFF);
	/*if (pasta->sauceary[1] == NOT_DETERMINED){
		ans = pasta->schedule(2, Tomato, DIFF) + pasta->schedule(2, Cream, DIFF) + pasta->schedule(2, Basil, DIFF);
	} else {
		ans = pasta->schedule(2, pasta->sauceary[1], DIFF);
	}*/

	cout << ans % divisor << endl;

	delete pasta;
	return 0;
}