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;

#define REP(i, s, n) for(int i = (s); i < int(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(co) co.begin(), co.end()

int dp[101][3][2];
int sche[101];
const int MOD = 10000;

int main()
{
	int n, k; cin >> n >> k;
	n--;//0 based
	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;

	rep(i, n - 1)
	{
		if (~sche[i + 1]) rep(j, 3)
		{
			if (j == sche[i + 1]) dp[i + 1][sche[i + 1]][1] += dp[i][sche[i + 1]][0] % MOD;
			else dp[i + 1][sche[i + 1]][0] += (dp[i][j][0] + dp[i][j][1]) % MOD;
		}
		else rep(j, 3) rep(k, 3)
		{
			if (j == k) dp[i + 1][j][1] += dp[i][k][0] % MOD;
			else dp[i + 1][j][0] += (dp[i][k][0] + dp[i][k][1]) % MOD;
		}
	}

	int ans = 0;
	rep(i, 3) (ans += (dp[n][i][0] + dp[n][i][1])) %= MOD;
	cout << ans << endl;

	return 0;

}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_N = 110;
const int MAX_K = 110;
const int MAX_P = 3;
const int MOD = 10000;

int main(){
    int n, k;
    cin >>n >>k;
    vector<int> pasta(n, -1);
    REP(i, k){
        int day, p;
        cin >>day >>p;
        day--;
        p--;
        pasta[day] = p;
    }

    int dp[MAX_N][MAX_P][2];
    memset(dp, 0, sizeof(dp));
    //init
    if(pasta[0] != -1) dp[0][pasta[0]][0] = 1;
    else REP(i, MAX_P) dp[0][i][0] = 1;

    //dp
    REP(i, n - 1){
        REP(j, MAX_P){
            REP(k, MAX_P){
                if(pasta[i] != -1 && pasta[i] != j) continue;

                if(j == k){
                    dp[i + 1][k][1] = (dp[i + 1][k][1] + dp[i][j][0]) % MOD;
                } else{
                    dp[i + 1][k][0] = (dp[i + 1][k][0] + dp[i][j][0] + dp[i][j][1]) % MOD;
                }

            }
        }
    }

    //debug
    //REP(i, n){
    //    REP(j, MAX_P){
    //        cout <<dp[i][j][0] <<" ";
    //    }
    //    cout <<endl;
    //}
    //cout <<endl;
    //REP(i, n){
    //    REP(j, MAX_P){
    //        cout <<dp[i][j][0] <<" ";
    //    }
    //    cout <<endl;
    //}

    int ans = 0;
    if(pasta[n - 1] != -1){
        ans = (ans + dp[n - 1][pasta[n - 1]][0] + dp[n - 1][pasta[n - 1]][1]) % MOD;
    } else{
        REP(i, MAX_P){
            ans = (ans + dp[n - 1][i][0]) % MOD;
            ans = (ans + dp[n - 1][i][1]) % MOD;
        }
    }

    cout <<ans <<endl;


    return 0;
}

C++: #include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional> //sort(A,A.end(),greater<int>());???????????????
#include <queue>
#include <stack>
#include <vector>

#define INF 10 << 10

int date[128];
int n;
int cnt = 0;

int rec(int d1,int d2,int day);

int main() {
	memset(date, 0, sizeof(date));

	int k;
	cin >> n >> k;
	for (int i = 0; i < k; ++i) {
		int a, b;
		cin >> a >> b;
		date[a] = b;
	}

	rec(0,0,1);

	cout << cnt % 10000 << endl;

	return 0;
}

int rec(int d1, int d2, int day) {

	if (day > n) { cnt++; return 0; }
	if (date[day] != 0) {
		if (d1 == d2 && d1 == date[day]) { return 0; }
		rec(date[day], d1, day + 1);
		return 0;
	}

	for (int i = 1; i <= 3; ++i) {
		if (d1 != d2 || d1 != i) { rec(i, d1, day + 1); }
	}
	return 0;
}

C++: #include <iostream>

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

int date[100];  // ignore after date[n]
int func(int ind, int n) {
    while (date[++ind])
        ;
    if (ind == n) return 1;

    int total = 0;
    for (int p = 1; p <= 3; p++) {
        if (ind != 0 && ind != 1 && date[ind - 2] == p && date[ind - 1] == p)
            continue;
        if (ind != 0 && ind != n - 1 && date[ind - 1] == p &&
            date[ind + 1] == p)
            continue;
        if (ind != n - 1 && ind != n - 2 && date[ind + 1] == p &&
            date[ind + 2] == p)
            continue;
        date[ind] = p;

        total += func(ind, n);
    }
    date[ind] = 0;
    return total;
}

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

    rep(i, 100) date[i] = 0;
    int d, p;
    rep(i, k) {
        std::cin >> d >> p;
        date[d - 1] = p;
    }
    std::cout << func(-1, n) % 10000 << std::endl;
}