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 <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

int main()
{
	for(int N,K;cin>>N>>K && N|K;){
		vi as(K),bs(K);
		for(int i=0;i<K;i++){
			cin>>as[i]>>bs[i];
			as[i]--; bs[i]--;
		}
		
		for(int i=0;i<K;i++)
			for(int j=K;j--;)
				if(as[j]>as[j+1]){
					swap(as[j],as[j+1]);
					swap(bs[j],bs[j+1]);
				}
		
		int si=0;
		vector<vvi> ps(N,vvi(3,vi(3)));
		
		int i=0;
		if(i<N){
			for(int j=0;j<3;j++)
				ps[i][j][0]=1;
			if(si<K && as[si]==i){
				for(int j=0;j<3;j++)
					if(j!=bs[si])
						ps[i][j][0]=0;
				si++;
			}
			i++;
		}
		if(i<N){
			for(int j=0;j<3;j++)
				for(int k=0;k<3;k++)
					for(int l=0;l<3;l++)
						ps[i][j][k]+=ps[i-1][k][l];
			if(si<K && as[si]==i){
				for(int j=0;j<3;j++)
					if(j!=bs[si])
						for(int k=0;k<3;k++)
							ps[i][j][k]=0;
				si++;
			}
			i++;
		}
		for(;i<N;i++){
			for(int j=0;j<3;j++)
				for(int k=0;k<3;k++)
					for(int l=0;l<3;l++){
						if(j==k && k==l)
							continue;
						ps[i][j][k]=(ps[i][j][k]+ps[i-1][k][l])%10000;
					}
			if(si<K && as[si]==i){
				for(int j=0;j<3;j++)
					if(j!=bs[si])
						for(int k=0;k<3;k++)
							ps[i][j][k]=0;
				si++;
			}
		}
		//puts("ps:");
		//for(int i=0;i<N;i++){
		//	for(int j=0;j<3;j++){
		//		cout<<(j?" ":"")<<'[';
		//		for(int k=0;k<3;k++)
		//			cout<<(k?" ":"")<<ps[i][j][k];
		//		cout<<']';
		//	}
		//	cout<<endl;
		//}
		int res=0;
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				res+=ps[N-1][i][j];
		cout<<res%10000<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
 
using namespace std;
 
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(),r.end()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
#define DBG(X) cout<<#X<<" : "<<X<<endl;
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-10;

int dp[110][100];

int main(){
	int n, k;
	cin>>n>>k;
	vi a(k), b(k);
	vi v(110, -1);
	rep(i, k) {
		cin>>a[i]>>b[i];
		b[i]--;
		v[a[i]] = b[i];
	}
	if(v[1]==-1){
		for(int i = 0; i < 3; i++){
			dp[1][i*3] = 1;
		}
	}
	else dp[1][v[1]*3] = 1;

	int past1, past2;
	for(int i = 2; i <= n; i++){
		if(i==2){
			for(int j = 0; j < 3; j++){
				past1 = j;
				if(v[i]!=-1){
					dp[i][v[i]*3+past1] += dp[i-1][j*3];
					dp[i][v[i]*3+past1] %= 10000;
				}
				else{
					for(int k = 0; k < 3; k++){	
						dp[i][k*3+past1] += dp[i-1][j*3];
						dp[i][k*3+past1] %= 10000;
					}
				}
			}
			continue;
		}

		for(int j = 0; j < 9; j++){
			past1 = j/3; past2 = j%3;
			if(v[i]!=-1){
				if((past1!=v[i]||past2!=v[i])){
					dp[i][v[i]*3+past1] += dp[i-1][j];
					dp[i][v[i]*3+past1] %= 10000;
				}
			}
			else{
				for(int k = 0; k < 3; k++){
					if((past1!=k||past2!=k)){
						dp[i][k*3+past1] += dp[i-1][j];
						dp[i][k*3+past1] %= 10000;
					}
				}
			}
		}
	}
	int ans = 0;
	for(int i = 0; i < 9; i++){
		ans += dp[n][i];
		ans %= 10000;
	}
	cout<<ans<<endl;
}

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 && k)
	{
		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++)
			{
				for (int j = 0; j < 2; j++)
				{
					pre[i][j] = 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;
}