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++: //2011情報オリンピック予選問題4「パスタ」
#include<stdio.h>
//初期化関数
void format(int [][2]);
//組み合わせ関数
void dynamic(int [][2], int [][2], int);

int main(void)
{
	int cnt[3][2], out[3][2], *List, day = 0, N, K, A, B, f1, f2, sum = 0;

	scanf("%d %d",&N,&K);

	List = new int [N];

	//初期化
	for(f1 = 0; f1 < N; f1++)
		List[f1] = -1;
	//予定を埋める
	for(f1 = 1; f1 <= K; f1++){
		scanf("%d %d",&A,&B);
		A--; B--;
		List[A] = B;
	}

	format(cnt);

	//実行
	while(day < N){

		format(out);

		//初日の設定
		if(day == 0){

			if(List[day] >= 0)
				out[List[day]][0] = 1;

			else{
				for(f1 = 0; f1 < 3; f1++)
					out[f1][0] = 1;
			}
		}
		else{
			//食べるソースが決まっている
			if(List[day] >= 0)
				dynamic(out, cnt, List[day]);
			//未定の場合
			else{
				for(f1 = 0; f1 < 3; f1++)
					dynamic(out, cnt, f1);
			}
		}

		for(f1 = 0; f1 < 3; f1++){
			for(f2 = 0; f2 < 2; f2++){
				if(out[f1][f2] >= 10000)
					out[f1][f2] %= 10000;
				cnt[f1][f2] = out[f1][f2];
			}
		}
		day++;
	}

	for(f1 = 0; f1 < 3; f1++){
		for(f2 = 0; f2 < 2; f2++){
			sum += out[f1][f2];
		}
	}

	if(sum >= 10000)
		sum %= 10000;

	printf("%d\n",sum);

	return 0;
}
void format(int v[][2])
{
	int f1, f2;

	for(f1 = 0; f1 < 3; f1++){
		for(f2 = 0; f2 < 2; f2++){
			v[f1][f2] = 0;
		}
	}

}
void dynamic(int out[][2], int cnt[][2], int sn)
{
	int f1, f2;

	for(f1 = 0; f1 < 3; f1++){
		for(f2 = 0; f2 < 2; f2++){
			if(sn != f1)
				out[sn][0] += cnt[f1][f2];
		}
	}
	out[sn][1] = cnt[sn][0];
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;

#define pb         push_back
#define mp         make_pair
#define ll         long long
#define PI         acos(-1.0)
#define ALL(A)     ((A).begin(), (A).end())
#define vsort(v)   sort(v.begin(),v.end())
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)

int main(){
    int N, K;
    cin >> N >> K;
    int pasta[101]={0};
    FOR(i, 1, K+1){
        int hi, pas;
        cin >> hi >> pas;
        pasta[hi] = pas;
    }
    ll int dp[101][4][4]={0};
    FOR(i, 1, 4){
        if(pasta[1]==0){
            dp[1][i][0] = 1;
        }
        else{
            if(i==pasta[1]) dp[1][i][0] = 1;
        }
    }

    FOR(i, 1, N+1){
        FOR(j, 0, 4){
            FOR(k, 0, 4){
                if(dp[i][j][k]==0) continue;
                FOR(m, 1, 4){
                    if(j==k && m==k) continue;
                    if(pasta[i+1]==0){
                        dp[i+1][m][j] = dp[i+1][m][j] + dp[i][j][k];
                    }
                    else{
                        if(pasta[i+1]==m){
                            dp[i+1][m][j] = dp[i+1][m][j] + dp[i][j][k];
                        }
                        else dp[i+1][m][j] = 0;
                    }
                }

            }
        }
    }
    int ans = 0;
    FOR(i, 1, 4){
        FOR(j, 1, 4){
            ans += dp[N][i][j];
            ans = ans % 10000;
        }
    }

    cout << ans << endl;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int k;
int a,q[200]={0};
int b,w[200]={0};
int cnt1=0,cnt2=0,cnt3=0;
int cnt=0,ans=0,co=0;
void check (int x);
int main(void){
    cin>>n>>k;

	for(int i=0;i<k;i++){
        cin>>a;
        cin>>b;
        q[a-1]=b;
	}
	/*for(int i=1;i<=20;i++){
	n=i;*/
	check(0);

    cout<<ans<<endl;
    /*co=0;
    ans=0;
	}	*/
	return 0;
}
void check(int x){
    /*for(int i=0;i<x;i++){
        cout<<"-";
    }
    cout<<(co+1)/2<<endl;*/

    if(x==n){
        ans++;
        if(ans>=10000)ans-=10000;
        //cout<<"+"<<co<<endl;
        return;
    }
    if(q[x]!=0){
        if(q[x]==1){
            if(co==2)return;
            else if(co==1)co++;
            else co=1;
            check(x+1);
        }
        else if(q[x]==2){
            if(co==4)return;
            else if(co==3)co++;
            else co=3;
            check(x+1);
        }
        else if(q[x]==3){
            if(co==6)return;
            else if(co==5)co++;
            else co=5;
            check(x+1);
        }

    }
    else{

        if(co!=2){
            int sub=co;
            if(co==1)co++;
            else co=1;
            check(x+1);
            co=sub;
        }
        if(co!=4){
            int sub=co;
            if(co==3)co++;
            else co=3;
            check(x+1);
            co=sub;
        }
        if(co!=6){
            int sub=co;
            if(co==5)co++;
            else co=5;
            check(x+1);
            co=sub;
        }
    }


}