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++: //Pasta
#include <iostream>
#include <map>

using namespace std;

static const int MOD = 10000;

int main() {
    int N, K, a, b;
    cin >> N >> K;
    map<int, int> m;
    for(int i = 0; i < K; i++) {
        cin >> a >> b;
        m.insert(make_pair(a - 1, b - 1));
    }
    int dp[N][3][3];
    for(int i = 0; i < N; i++) {
        for (int j = 0; j < 3; j++) {
            for(int k = 0; k < 3; k++) {
                dp[i][j][k] = 0;
            }
        }
    }

    auto itr1 = m.find(0);
    if(itr1 == m.end()) {
        for(int j = 0; j < 3; j++) {
            auto itr2 = m.find(1);
            if(itr2 == m.end()) {
                for (int k = 0; k < 3; k++) {
                    dp[1][j][k] = 1;
                }
            } else dp[1][j][itr2 -> second] = 1;
        }
    } else {
        auto itr2 = m.find(1);
        if(itr2 == m.end()) {
            for (int k = 0; k < 3; k++) {
                dp[1][itr1 -> second][k] = 1;
            }
        } else dp[1][itr1 -> second][itr2 -> second] = 1;
    }

    for(int i = 2; i < N; i++) {
        auto itr = m.find(i);
        if(itr == m.end()) {
            for(int k = 0; k < 3; k++) {
                for(int j = 0; j < 3; j++) {
                    if(j != k) dp[i][j][k] = dp[i - 1][0][j] + dp[i - 1][1][j] + dp[i - 1][2][j];
                    else dp[i][j][k] = dp[i - 1][0][j] + dp[i - 1][1][j] + dp[i - 1][2][j] - dp[i - 1][j][j];
                    dp[i][j][k] %= MOD;
                }
            }
        } else {
            for(int j = 0; j < 3; j++) {
                if(j != itr -> second) dp[i][j][itr -> second] = dp[i - 1][0][j] + dp[i - 1][1][j] + dp[i - 1][2][j];
                else dp[i][j][itr -> second] = dp[i - 1][0][j] + dp[i - 1][1][j] + dp[i - 1][2][j] - dp[i - 1][j][j];
                dp[i][j][itr -> second] %= MOD;
            }
        }
    }

    int c = 0;
    for(int j = 0; j < 3; j++) for(int k = 0; k < 3; k++)
            c += dp[N - 1][j][k];
    c %= MOD;

    cout << c << endl;

    return 0;
}

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

int P[100];
int n, k;
int memo[3][100][3];

int search(int ren, int d, int p){
  if(d == n) return 1;
  if(memo[ren][d][p] != -1) return memo[ren][d][p];



  int res = 0;
  if(P[d] == 0){
    for(int i = 1; i <= 3; i++){
      if(p == i){
	if(ren + 1 >= 3) continue;
	if(P[d+1] == i && ren + 1 >= 2) continue;
	res += search(ren+1, d+1, i) % MOD;
      }else{
	res += search(1, d+1, i) % MOD;
      }
    }
  }else{
    if(p == P[d]) res += search(ren+1, d+1, P[d]) % MOD;
    else res += search(1, d+1, P[d]) % MOD;
  }

  memo[ren][d][p] = res % MOD;
  return memo[ren][d][p];
  //return res;
}

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

  cin >> n >> k;
  for(int i = 0; i < 3; i++) for(int j = 0; j < 100; j++) for(int k = 0; k < 3; k++) memo[i][j][k] = -1;
  for(int i = 0; i < 100; i++){
    P[i] = 0;
  }
  for(int i = 0; i < k; i++){
    int d, p;
    cin >> d >> p;
    d--;
    P[d] = p;
  }

  cout << search(1, 0, -1) << endl;

  return 0;
}

C++: #include<stdio.h>
int main()
{
    int n,m,dp[3][110],a,b;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            dp[j][i] = -1;
        }
    }
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d",&a,&b);
        for(int j = 0; j < 3; j++)
        {
            if(j == b - 1);
            else dp[j][a - 1] = 0;
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 3; j++)
        {

            if(dp[j][i] == 0);
            else if(i == 0)dp[j][i] = 1;
            else if(i == 1)dp[j][i] = (dp[0][i - 1] + dp[1][i - 1] + dp[2][i - 1]) % 10000;
            else
            {
                dp[j][i] = 0;
                for(int k = 0; k < 3; k++)
                {
                    for(int l = 1; l < 3; l++)
                    {
                        dp[j][i] = (dp[j][i] + dp[k][i - l]) % 10000;
                    }
                }
                if(dp[j][i - 1] == 0)
                {
                    for(int k = 0; k < 3; k++)
                    {
                        dp[j][i] = (dp[j][i] - dp[k][i - 2]) % 10000;
                    }
                }
                else
                {
                    for(int k = 1; k < 3; k++)
                    {
                        dp[j][i] = (dp[j][i] - dp[j][i - k]) % 10000;
                    }
                }
            }
        }
    }
    printf("%d\n",(dp[0][n - 1] + dp[1][n - 1] + dp[2][n - 1]) % 10000);
    return 0;
}