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 <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

const int mod = 10000;

int sche[100];
int dp[100][3][3]; // dp[??\??°][?????\][??¨??\]

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

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

	rep(i, K) {
		int a, b; cin >> a >> b, -- a, -- b;
		sche[a] = b;
	}

	if (~sche[0]) {
		dp[0][sche[0]][0] = 1;
	} else {
		rep(i, 3) {
			dp[0][i][0] = 1;
		}
	}

	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 3; ++j) {
			if (~sche[1]) {
				dp[1][sche[1]][i] += dp[0][i][j];
			} else {
				for (int k = 0; k < 3; ++k) {
					dp[1][i][j] += dp[0][j][k];
				}
			}
		}
	}

	for (int i = 2; i < N; ++i) {
		for (int j = 0; j < 3; ++j) {
			for (int k = 0; k < 3; ++k) {
				if (~sche[i]) {
					if (sche[i] == j && j == k) continue;
					dp[i][sche[i]][j] += dp[i - 1][j][k];
					dp[i][sche[i]][j] %= mod;
				} else {
					for (int l = 0; l < 3; ++l) {
						if (j == k && k == l) continue;
						dp[i][j][k] += dp[i - 1][k][l];
						dp[i][j][k] %= mod;
					}
				}
			}
		}
	}
	int ans = 0;

	for (int i = 0; i < 3; ++i) {
		for (int j = 0; j < 3; ++j) {
			ans += dp[N - 1][i][j];
			ans %= mod;
		}
	}

	cout << ans << endl;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int k;
int q[200]={0};
int a,b;
int ans=0;
int mem[110][110][110]={{{0}}};
int check(int x,int y,int z);//x日目,前日y,前々日z
int main(void){
    cin>>n>>k;

	for(int i=0;i<k;i++){
        cin>>a;
        cin>>b;
        q[a-1]=b;
	}



    cout<<check(0,0,0)<<endl;
	return 0;
}
int check(int x,int y,int z){
    if(mem[x][y][z]!=0)return mem[x][y][z];

    if(x==n){

        return 1;
    }
    if(q[x]!=0){
        if(q[x]==1){
            if(y!=1||z!=1){
            return check(x+1,1,y);
            }
            else return 0;
        }
        else if(q[x]==2){
            if(y!=2||z!=2){
            return check(x+1,2,y);
            }
            else return 0;
        }
        else if(q[x]==3){
            if(y!=3||z!=3){
            return check(x+1,3,y);
            }
            else return 0;
        }

    }
    else{

        if(y!=z||y==0){
            int a=check(x+1,1,y)+check(x+1,2,y)+check(x+1,3,y);
            a%=10000;
            mem[x][y][z]=a;
            return a;

        }
        else if(y==1){

            int a=check(x+1,2,y)+check(x+1,3,y);
            a%=10000;
            mem[x][y][z]=a;
            return a;
        }
        else if(y==2){

            int a=check(x+1,1,y)+check(x+1,3,y);
            a%=10000;
            return a;
        }
        else if(y==3){

            int a=check(x+1,1,y)+check(x+1,2,y);
            a%=10000;
            mem[x][y][z]=a;
            return a;

        }
    }