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 <vector>
#include <algorithm>
#include <string.h>

using namespace std;

typedef unsigned long long ull;

int N, K;
int P[101];
int dp[101][4][4];

int solve(int i, int j, int k);

int main(void){
	scanf("%d %d", &N, &K);
	memset(dp, -1, sizeof(dp));

	for(int i = 0;i < K;++i){
		int t, u;
		scanf("%d %d", &t, &u);
		P[t-1] = u;
	}

	printf("%d\n", solve(0, 0, 0));
}
int solve(int i, int j, int k){ //前日にパスタjを食べ，前々日にパスタkを食べた場合の，i日目以降の組合せ
	if(i == N) return 1;

	if(dp[i][j][k] != -1) return dp[i][j][k];

	if(P[i] != 0){
		if(P[i] == j && j == k) return 0;
		dp[i][j][k] = solve(i+1, P[i], j) % 10000;
		return solve(i+1, P[i], j) % 10000;
	}

	int t = 0;

	for(int u = 1;u <= 3;++u){
		if(!(u == j && j == k)){
			t += solve(i+1, u, j) % 10000;
		}
	}

	dp[i][j][k] = t % 10000;
	return t % 10000;
}

C++: 











#include<iostream>
#include<cstdio>

using namespace std;


#define MAX 110
#define PA	10000

int K;
int N;
int dp[MAX][4][4][4];


int days[MAX];


//3日保持
int dfs(int day , int b1 , int b2 , int b3)
{
	//ここにreturn条件
	if (b1 != 0 && b2 != 0 && b3 != 0 &&
		b1 == b2 && b2 == b3)
	{
		return 0;
	}

	//既に到達しているならそのまま返す
	if (dp[day][b1][b2][b3] != 0)
	{
		return dp[day][b1][b2][b3];
	}

	//最後までたどり着いたとき(3日"前"までの値を保持しているので、1日分増やさなければならない)
	if (day == N + 1)
	{
		if (b1 == b2 && b2 == b3)
		{
			return 0;
		}
		return dp[day][b1][b2][b3] + 1;
	}

	if (days[day] != 0)
	{
		return dp[day][b1][b2][b3] += (dfs(day + 1 , days[day] , b1 , b2)) % PA;
	}
	//上の処理にはずれた場合
	dp[day][b1][b2][b3] += dfs(day + 1 , 1 , b1 , b2);
	dp[day][b1][b2][b3] += dfs(day + 1 , 2 , b1 , b2);
	dp[day][b1][b2][b3] += dfs(day + 1 , 3 , b1 , b2);

	return dp[day][b1][b2][b3] % PA;
}

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

	for (int i = 0; i < K; i++)
	{
		int index;
		cin >> index;
		
		cin >> days[index];
	}

	int ans = dfs(1 , 0 , 0 , 0) % PA;

	cout << ans << endl;

	return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

const LL mod = 10000;
LL N, K, A[112], B[112];
std::map<LL, LL>list;
LL memo[112][4][4];

LL dp(LL d, LL b2, LL b1)
{
	if (memo[d][b2][b1] != -1) { return memo[d][b2][b1]; }
	if (d == N) { return memo[d][b2][b1] = 1; }
	if (list[d] > 0)
	{
		if (!(b2 == b1 && b1 == list[d])) { return memo[d][b2][b1] = dp(d + 1, b1, list[d]) % mod; }
		else { return memo[d][b2][b1] = 0; }
	}
	else
	{
		LL res = 0;
		for (int i = 1; i <= 3; ++i)
		{
			if (!(b2 == b1 && b1 == i)) { res += dp(d + 1, b1, i) % mod; }
			res %= mod;
		}
		return memo[d][b2][b1] = res;
	}
}

int main()
{
	memset(memo, -1, sizeof(memo));
	in >> N >> K;
	rep(i, N) { in >> A[i] >> B[i]; }
	rep(i, N) { list[A[i]] = B[i]; }
	out << dp(0, 0, 0) << std::endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const int MOD = 10000;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    vector< P > v(k);
    rep(i, k){
        cin >> v[i].first >> v[i].second;
        --v[i].second;
    }
    SORT(v);

    int dp[101][3][3] = {0};
    if(v[0].first == 1){
        dp[1][(v[0].second + 1) % 3][v[0].second] = 1;
    }else{
        rep(l, 3){
            dp[1][(l + 1) % 3][l] = 1;
        }
    }

    REP(i, 1, n){
        rep(j, 3){
            rep(l, 3){
                rep(m, 3){
                    if(j == l && l == m){
                        continue;
                    }
                    auto it = lower_bound(v.begin(), v.end() - 1, make_pair(i + 1, -1));
                    if((*it).first == i + 1 && (*it).second != m){
                        continue;
                    }
                    dp[i + 1][l][m] += dp[i][j][l];
                    dp[i + 1][l][m] %= MOD;
                }
            }
        }
    }

    int ans = 0;
    rep(j, 3){
        rep(l, 3){
            ans += dp[n][j][l];
            ans %= MOD;
        }
    }

    cout << ans << endl;

    return 0;
}