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++: //Name: Pasta
//Level: 2
//Category: 動的計画法,DP
//Note:

/*
 * 状態となるのは
 * ・今何日目か
 * ・最後にどのソースを使ったか
 * ・最後のソースは2日連続か
 * の3つ．
 * N日目にソースiを使ったときのパターン数は
 * ・N-1日目にソースj(j≠i)を使うときの合計パターン数(このとき，最後に使ったソースiは1日目)
 * ・N-1日目にソースiを使い，かつそれが1日目であるときのパターン数(このとき，最後に使ったソースiは2日連続)
 * で表わせる．
 * あらかじめ使うソースが決まっている日では，そのソース以外の数値が0になるよう処理すればよい．
 */
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MOD = 10000;

// dp[day][last][second]
int dp[100][3][2];
int plan[100];

int main() {
    int N, K;
    cin >> N >> K;
    fill_n(plan, N, -1);
    for(int i = 0; i < K; ++i) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        plan[a] = b;
    }

    if(plan[0] == -1) {
        dp[0][0][0] = dp[0][1][0] = dp[0][2][0] = 1;
    }
    else {
        dp[0][plan[0]][0] = 1;
    }
    for(int day = 1; day < N; ++day) {
        if(plan[day] == -1) {
            for(int sauce = 0; sauce < 3; ++sauce) {
                for(int prev = 0; prev < 3; ++prev) {
                    if(prev != sauce) {
                        dp[day][sauce][0] += dp[day-1][prev][0] + dp[day-1][prev][1];
                    }
                    else {
                        dp[day][sauce][1] += dp[day-1][prev][0];
                    }
                }
            }
        }
        else {
            int sauce = plan[day];
            for(int prev = 0; prev < 3; ++prev) {
                if(prev != sauce) dp[day][sauce][0] += dp[day-1][prev][0] + dp[day-1][prev][1];
                else dp[day][sauce][1] += dp[day-1][prev][0];
            }
        }
        for(int sauce = 0; sauce < 3; ++sauce) {
            dp[day][sauce][0] %= MOD;
            dp[day][sauce][1] %= MOD;
        }
    }
    int sum = 0;
    for(int sauce = 0; sauce < 3; ++sauce) {
        sum += dp[N-1][sauce][0] + dp[N-1][sauce][1];
    }
    cout << sum%MOD << endl;
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#define  _SCL_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include<map>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
//Boost
/*
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/miller_rabin.hpp>
#include <boost/random.hpp>
namespace mp = boost::multiprecision;
*/
using namespace std;
#define REP(a,b) for(long long a = 0;a < b;++a)
long long dp[1000][6] = {};
int is_it[1000] = {};
int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	vector<pair<int, int>> hoge;
	REP(i, k) {
		int a, b;
		cin >> a >> b;
		is_it[a] = b;
	}
	if (is_it[1] == 0) {
		dp[1][0] = 1;
		dp[1][2] = 1;
		dp[1][4] = 1;
	}
	else {
		if (is_it[1] == 1) {
			dp[1][0] = 1;
		}
		else if(is_it[1] == 2) {
			dp[1][2] = 1;
		}
		else {
			dp[1][4] = 1;
		}
	}
	for (int i = 2;i <= n;++i) {
		if (is_it[i] == 0) {
			dp[i][1] = dp[i - 1][0];
			dp[i][3] = dp[i - 1][2];
			dp[i][5] = dp[i - 1][4];
			dp[i][0] = dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][5];
			dp[i][2] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][4] + dp[i - 1][5];
			dp[i][4] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3];
		}
		else {
			if (is_it[i] == 1) {
				dp[i][1] = dp[i - 1][0];
				dp[i][0] = dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][5];
			}
			else if (is_it[i] == 2) {
				dp[i][3] = dp[i - 1][2];
				dp[i][2] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][4] + dp[i - 1][5];
			}
			else {
				dp[i][5] = dp[i - 1][4];
				dp[i][4] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][3];
			}
		}
		for (int q = 0;q < 6;++q) {
			dp[i][q] %= 10000;
		}
	}
	long long ans = 0;
	for (int i = 0;i < 6;++i) {
		ans += dp[n][i];
	}
	ans %= 10000;
	cout << ans << endl;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 10000;

int main(){
  int N, K;
  while(cin>>N>>K){
    int limit[101];
    memset(limit,-1,sizeof(limit));
    REP(i,K){
      int day, spa;
      cin>>day>>spa;
      day--; spa--;
      limit[day] = spa;
    }
    int dp[9] = {};
    REP(i,3)if(limit[0]==-1||limit[0]==i){
      REP(j,3)if(limit[1]==-1||limit[1]==j){
        dp[i*3+j] = 1;
      }
    }
    for(int d = 2; d < N; d++){
      int nextdp[9] = {};
      REP(next, 3)if(limit[d]==-1 || limit[d]==next){
        REP(i,3)REP(j,3)if(!(i == j && j == next)){
          nextdp[j*3+next] += dp[i*3+j];
          nextdp[j*3+next] %= MOD;
        }
      }
      memcpy(dp, nextdp, sizeof(dp));
    }
    int ans = 0;
    REP(i,9)ans += dp[i];
    cout<<ans%MOD<<endl;
  }
  return 0;
}