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 <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<int> LArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

int itiv;
inline int getInt() { return (cin >> itiv, itiv); }
void readAll(Array& vec, int n) { for (int i = 0; i < n; i++) cin >> vec[i]; }
inline bool between(int first, int last, int n) { return first <= n && n <= last; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int k, n;
int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

// day, pizza, renzoku
int dp[101][3][2];
const int MOD = 10000;

int main(void)
{
	while(cin >> n >> k && n)
	{
		memset(dp, 0, sizeof(dp));
		Array kimari(n+1);
		for (int i = 0; i < k; i++)
		{
			int a, b; cin >> a >> b;
			kimari[--a] = b;
		}

		if(kimari[0])
			dp[0][kimari[0] - 1][0] = 1;
		else
			dp[0][0][0] = dp[0][1][0] = dp[0][2][0] = 1;
		for (int i = 1; i < n; i++)
		{
			for (int j = 0; j < 3; j++) //今日
			{
				if(kimari[i] && kimari[i] - 1 != j) continue;

				dp[i][j][1] = dp[i-1][j][0];
				for (int k = 0; k < 3; k++) //前日
				{
					if(j != k)
						dp[i][j][0] = (dp[i][j][0] + dp[i - 1][k][0] + dp[i - 1][k][1]) % MOD;
				}
			}
		}
		int ans = 0;
		for (int i = 0; i < 3; i++)
		{
			ans += dp[n-1][i][0] + dp[n-1][i][1];
		}
		cout << ans % MOD << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n;i>=0;i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[101], dp[101][3][3];
int main(){
	int n, k,t1,t2;
	scanf("%d%d", &n, &k);
	rep(i,k){
		scanf("%d%d", &t1, &t2);
		a[t1] = t2;
	}
	if(a[1] == 0 || a[1] == 1)dp[1][0][1] = 1;
	if(a[1] == 0 || a[1] == 2)dp[1][1][2] = 1;
	if(a[1] == 0 || a[1] == 3)dp[1][2][0] = 1;
	for(int i = 1;i <= n;i++){
		if(a[i] == 0 || a[i] == 1){
			dp[i][0][0] = dp[i-1][0][1]+dp[i-1][0][2];
			rep(j,3)dp[i][0][1] += dp[i-1][1][j];
			rep(j,3)dp[i][0][2] += dp[i-1][2][j];
		}
		if(a[i] == 0 || a[i] == 2){
			dp[i][1][1] = dp[i-1][1][0]+dp[i-1][1][2];
			rep(j,3)dp[i][1][0] += dp[i-1][0][j];
			rep(j,3)dp[i][1][2] += dp[i-1][2][j];
		}
		if(a[i] == 0 || a[i] == 3){
			dp[i][2][2] = dp[i-1][2][0]+dp[i-1][2][1];
			rep(j,3)dp[i][2][0] += dp[i-1][0][j];
			rep(j,3)dp[i][2][1] += dp[i-1][1][j];
		}
		rep(j,3)rep(k,3)dp[i][j][k] = dp[i][j][k]%10000;
	}
	int ans = 0;
	rep(i,3)rep(j,3)ans += dp[n][i][j];
	printf("%d\n", ans%10000);
	return 0;
}

C++: #include <cstdio>
#include <cstring>

#define MOD (10000)

using namespace std;

int sum(int *p)
{
	return (p[0] + p[1]);
}

int main(void)
{
	int n, k;
	static int dp[100][3][2];
	int done[100];
	
	scanf("%d %d", &n, &k);
	
	memset(done, -1, sizeof(done));
	for (int i = 0; i < k; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		done[--a] = --b;
	}
	
	for (int i = 0; i < 3; i++){
		if (~done[0]) dp[0][done[i]][0] = 1;
		else dp[0][i][0] = 1;
	}
	
	for (int i = 1; i < n; i++){
		for (int j = 0; j < 3; j++){
			if (~done[i]){
				j = done[i];
				dp[i][j][0] = (sum(dp[i - 1][(j + 1) % 3]) + sum(dp[i - 1][(j + 2) % 3])) % MOD;
				dp[i][j][1] = dp[i - 1][j][0] % MOD;
				break;
			}
			else {
				dp[i][j][0] = (sum(dp[i - 1][(j + 1) % 3]) + sum(dp[i - 1][(j + 2) % 3])) % MOD;
				dp[i][j][1] = dp[i - 1][j][0] % MOD;
			}
		}
	}
	
	int s = 0;
	for (int i = 0; i < 3; i++){
		s += sum(dp[n - 1][i]);
	}
	
	printf("%d\n", s % MOD);
	
	return (0);
}