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;

int N,K,num;
int day[101];
int a[101][4][3];

long long int dp(int d,int k,int n){
	if(n == 3)return 0;

	if(a[d][k][n] >= 0)return a[d][k][n];

	if(d == N)return a[d][k][n] = 1;

	long long int b = 0; 
	if(day[d+1] != -1){
		if(day[d+1] == k){
			b += dp(d+1, day[d+1], n+1) % 10000;
		}else{
			b += dp(d+1, day[d+1], 1) % 10000;
		}
	}else{
		for(int i = 1;i <= 3;i++){
			if(i == k){
				b += dp(d+1, i, n+1) % 10000;
			}else{
				b += dp(d+1, i, 1) % 10000;
			}
		}
	}
	return a[d][k][n] = b % 10000;
}

int main(){
	fill_n(**a,101*4*3,-1);
	fill_n(day,101,-1);
	cin >>N >> K;
	for(int i = 0;i<K;i++){
		cin >> num;
		cin >> day[num];
	}
	cout <<dp(0,0,0)<<endl; 
}

C++: #include <iostream>

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

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

    int date[100];
    long dp[100][4][4];
    rep(i, 100) {
        date[i] = 0;
        rep(a, 4) rep(b, 4) dp[i][a][b] = 0;
    }

    int d, p;
    rep(i, k) {
        std::cin >> d >> p;
        date[d - 1] = p;
    }

    if (date[0] == 0)
        dp[0][0][1] = dp[0][0][2] = dp[0][0][3] = 1;
    else
        dp[0][0][date[0]] = 1;
    repr(i, 1, n) {
        if (date[i] == 0) {
            repr(p, 1, 4) rep(a, 4) rep(b, 4) if (a != b || b != p)
                dp[i][a][p] += dp[i - 1][b][a];
        } else {
            rep(a, 4) rep(b, 4) if (a != b || b != date[i]) dp[i][a][date[i]] +=
                dp[i - 1][b][a];
        }
    }
    long total = 0;
    rep(a, 4) rep(b, 4) total += dp[n - 1][a][b];
    std::cout << total % 10000 << std::endl;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

int pasta(int i,int af1,int af2);

int day[102];
int memo[102][4][4];
int n,k;

main(){
  int temp;
  scanf("%d %d",&n,&k);
  for(int i=0;i<k;i++){
    scanf("%d",&temp);
    scanf("%d",&day[temp]);
  }
  printf("%d\n",pasta(0,0,0));
}
int pasta(int i,int af1,int af2){
  int f[4]={0};
  int res=0;

  if(i==n) return 1;
  if(memo[i][af1][af2]) return memo[i][af1][af2];
  if(day[i]) return memo[i][af1][af2]=(pasta(i+1,day[i],af1))%10000;

  if(af1==af2) f[af1]=1;
  if(af1==day[i+1]) f[af1]=1;
  if(day[i+1]==day[i+2]) f[day[i+1]]=1;

  for(int j=1;j<=3;j++){
    if(f[j])continue;
    res+=(pasta(i+1,j,af1))%10000;
  }
  return  memo[i][af1][af2]=(res)%10000;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;
const int MOD = 10000;
int n, k;

int main(void)
{
	while(cin >> n >> k && n)
	{
		int pre[3][2] = {0};//一日前種類、（連続して行けるor行けない）
		int next[3][2] = {0};//次の日
		map<int, int> sche;//予定
		for (int i = 0; i < k; i++)
		{
			int day;
			cin >> day;
			cin >> sche[day - 1];
		}

		if(sche[0]) pre[sche[0] - 1][0] = 1;
		else
		{
			for (int i = 0; i < 3; i++)
			{
				pre[i][0] = 1;
			}
		}

		for (int d = 1; d < n; d++)
		{
			memset(next, 0, sizeof(next));
			if(sche[d])
			{
				for (int i = 0; i < 3; i++)
				{
					if(i == sche[d]-1)
						next[sche[d] - 1][1] += pre[i][0] % MOD;
					else
						next[sche[d] - 1][0] += (pre[i][0] + pre[i][1]) % MOD;
				}
			}
			else
			{
				for (int i = 0; i < 3; i++)
				{
					for (int j = 0; j < 3; j++)
					{
						if(i == j)
							next[i][1] += pre[j][0] % MOD;
						else
							next[i][0] += (pre[j][0] + pre[j][1]) % MOD;
					}
				}
			}

			for (int i = 0; i < 3; i++)
			{
				for (int j = 0; j < 2; j++)
				{
					pre[i][j] = next[i][j];
				}
			}
		}
		int ans = 0;
		for (int i = 0; i < 3; i++)
		{
			ans += next[i][0] + next[i][1];
		}
		cout << ans % MOD << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <string.h>
#define DAY_MAX	123
#define PASTA_MAX	5
#define ONAZI_MAX	123

int max;


int set_pasta[DAY_MAX];
int memo[DAY_MAX][PASTA_MAX][ONAZI_MAX];

int dp(int day, int pasta, int onazi){
	int numbuf = 0;
	int ans = 0;
	if (memo[day][pasta][onazi] >= 0) return memo[day][pasta][onazi];
	if (onazi == 3) return 0;
	if (day == max - 1) return 1;
	if (set_pasta[day + 1] >= 1){
		if (set_pasta[day + 1] == pasta) return memo[day][pasta][onazi] = dp(day + 1, pasta, onazi + 1);
		else return memo[day][pasta][onazi] = dp(day + 1, set_pasta[day + 1], 1);
	}
	else{
		for (int a = 0; a < 3; a++){
			if (a + 1 == pasta) ans += dp(day + 1, pasta, onazi + 1);
			else ans += dp(day + 1, a + 1, 1);
			ans %= 10000;
		}
		return ans;
	}
}

int main(){
	int num;
	int buf;
	int answer = 0;
	memset(memo, 255, sizeof(memo));
	scanf("%d%d", &max, &num);
	for (int a = 0; a < num; a++){
		scanf("%d", &buf);
		scanf("%d", &set_pasta[buf - 1]);
	}
	if (set_pasta[0] >= 1) printf("%d\n", dp(0, set_pasta[0], 1));
	printf("%d\n", (dp(0, 1, 1) + dp(0, 2, 1) + dp(0, 3, 1)) % 10000);
	return 0;
}