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<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,j) for(int i=0;i<j;i++)
int dp[1000][4]={{0},{0}};
const int INF=454354643;
int main(){
	int n,k;
	cin>>n>>k;
	for(int i=0;i<k;i++){
		int a,b;
		cin>>a>>b;
		if(b==1)dp[a][2]=INF,dp[a][3]=INF;
		if(b==2)dp[a][1]=INF,dp[a][3]=INF;
		if(b==3)dp[a][1]=INF,dp[a][2]=INF;
	}
	for(int i=1;i<=3;i++)if(dp[1][i]!=INF)dp[1][i]=1;
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=3;j++){
			if(dp[i+1][j]!=INF){
				for(int k=1;k<=3;k++){
					if(dp[i][k]!=INF){
						if(j==k&&i+1>=3&&dp[i-1][k]!=INF){
						dp[i+1][j]=(dp[i+1][k]+dp[i][k]-dp[i-1][k])%10000;
						}
						else dp[i+1][j]+=(dp[i][k])%10000;
					}
				}
			}
		}
	}
	
//	REP(i,n+1){
	//for(int j=1;j<=3;j++)cout<<dp[i][j]<<" ";
//	cout<<endl;
//	}
	int ans=0;
	for(int i=1;i<=3;i++)ans+=dp[n][i];
	cout<<ans%10000<<endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;

// →↑←↓
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};


const double EPS = 1e-9;

int main(){
    int N,K;
    cin >> N >> K;
    vector<int> pasta(N);
    for(int i=0;i<K;i++){
        int a,b;
        cin >> a >> b;
        a--;
        pasta[a] = b;
    }

    vector<vector<vector<int> > > V(N+1,vector<vector<int> >(4,vector<int>(3)));
    if(pasta[0] == 0){
        V[1][1][1] = 1;
        V[1][2][1] = 1;
        V[1][3][1] = 1;
    }else{
        V[1][pasta[0]][1] = 1;
    }

    const int MOD = 10000;
    for(int i=1;i<N;i++){
        int p = pasta[i];

        if(p != 0){
            for(int j=1;j<=3;j++){
                for(int k=1;k<=2;k++){
                    if(p != j){
                        V[i+1][p][1] = (V[i+1][p][1] + V[i][j][k]) % MOD;
                    }else{
                        V[i+1][p][k+1] = (V[i+1][p][k+1] + V[i][j][k]) % MOD;
                    }
                }
            }
        }else{
            for(int l=1;l<=3;l++){
                for(int j=1;j<=3;j++){
                    for(int k=1;k<=2;k++){
                        if(l == j){
                            V[i+1][l][k+1] = (V[i+1][l][k+1] + V[i][l][k]) % MOD;
                        }else{
                            V[i+1][l][1] = (V[i+1][l][1] + V[i][j][k]) % MOD;
                        }
                    }
                }
            }
        }
    }
    int ret = 0;
    for(int i=1;i<=3;i++){
        for(int k=1;k<=2;k++){
            ret = (ret + V[N][i][k]) % MOD;
        }
    }
    cout << ret << endl;

    return 0;
}

C++: #include <iostream>
#include <cmath>
#define MOD 10000
using namespace std;

int main(){
	int n,k,num[100] = {},dp[100][3] = {};
	cin >> n >> k;
	for(int i = 0;i < k;i++) {
		int a,b;
		cin >> a >> b;
		num[a - 1] = b;
	}
	if(num[0] != 0) dp[0][num[0] - 1] = 1;
	else {
		dp[0][0] = 1;
		dp[0][1] = 1;
		dp[0][2] = 1;
	}
	if(num[1] != 0) dp[1][num[1] - 1] = dp[0][0] + dp[0][1] + dp[0][2];
	else {
		dp[1][0] = dp[0][0] + dp[0][1] + dp[0][2];
		dp[1][1] = dp[0][0] + dp[0][1] + dp[0][2];
		dp[1][2] = dp[0][0] + dp[0][1] + dp[0][2];
	}
	for(int i = 2;i < n;i++){
		if(num[i] != 0) dp[i][num[i] - 1] = (max(0,dp[i - 1][num[i] - 1] - dp[i - 2][num[i] - 1]) + dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2] - dp[i - 1][num[i] - 1]);
		else {
			dp[i][0] = (max(0,dp[i - 1][0] - dp[i - 2][0]) + dp[i - 1][1] + dp[i - 1][2]);
			dp[i][1] = (dp[i - 1][0] + max(0,dp[i - 1][1] - dp[i - 2][1]) + dp[i - 1][2]);
			dp[i][2] = (dp[i - 1][0] + dp[i - 1][1] + max(0,dp[i - 1][2] - dp[i - 2][2]));
		}
		//cout << dp[i][0] << dp[i][1] << dp[i][2] << endl;
	}
	cout << (dp[n - 1][0] + dp[n - 1][1] + dp[n - 1][2]) % MOD << endl;
	return 0;	
}