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++: //AOJ 0568: Pasta
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX_N = 100, INF = 10000000;
int n, k, d, p, sch[MAX_N + 1], jud, jud2;
long long int dp[MAX_N + 1][4];
int main(){
	cin >> n >> k;
	for (int i = 0; i < k; i++){
		cin >> d >> p;
		sch[d] = p;
	}
	dp[0][0] = 1;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= 3; j++){
			if (sch[i] == j){
				dp[i][j] = dp[i - 1][0];
				jud = 0;
				jud2 = 0;
				if (i >= 3){
					if (sch[i - 1] != 0)
						jud++;
					if (sch[i - 2] != 0)
						jud++;
					if (sch[i - 1] == j)
						jud2++;
					if (sch[i - 2] == j)
						jud2++;
					if (i == 3){
						if (jud == 0){
							dp[i][j] -= 1;
						}
						if (jud == 1 && jud2 == 1){
							dp[i][j] -= 1;
						}
					}
					if (i > 3){
						if (jud == 0){
							if (j == 1)
								dp[i][1] -= dp[i - 3][2] + dp[i - 3][3];
							if (j == 2)
								dp[i][2] -= dp[i - 3][1] + dp[i - 3][3];
							if (j == 3)
								dp[i][3] -= dp[i - 3][1] + dp[i - 3][2];
						}
						if (jud == 1 && jud2 == 1){
							if (j == 1)
								dp[i][1] -= dp[i - 3][2] + dp[i - 3][3];
							if (j == 2)
								dp[i][2] -= dp[i - 3][1] + dp[i - 3][3];
							if (j == 3)
								dp[i][3] -= dp[i - 3][1] + dp[i - 3][2];
						}
					}
				}
				dp[i][0] += dp[i][j];

			}
			if (sch[i] == 0){
				dp[i][j] = dp[i - 1][0];
				jud = 0;
				jud2 = 0;
				if (i >= 3){
					if (sch[i - 1] != 0)
						jud++;
					if (sch[i - 2] != 0)
						jud++;
					if (sch[i - 1] == j)
						jud2++;
					if (sch[i - 2] == j)
						jud2++;
					if (i == 3){
						if (jud == 0){
							dp[i][j] -= 1;
						}
						if (jud == 1 && jud2 == 1){
							dp[i][j] -= 1;
						}
						if (jud2 == 2){
							dp[i][j] -= 1;
						}
					}
					if (i > 3){
						if (jud == 0){
							if (j == 1)
								dp[i][1] -= dp[i - 3][2] + dp[i - 3][3];
							if (j == 2)
								dp[i][2] -= dp[i - 3][1] + dp[i - 3][3];
							if (j == 3)
								dp[i][3] -= dp[i - 3][1] + dp[i - 3][2];
						}
						if (jud == 1 && jud2 == 1){
							if (j == 1)
								dp[i][1] -= dp[i - 3][2] + dp[i - 3][3];
							if (j == 2)
								dp[i][2] -= dp[i - 3][1] + dp[i - 3][3];
							if (j == 3)
								dp[i][3] -= dp[i - 3][1] + dp[i - 3][2];
						}
						if (jud2 == 2){
							if (j == 1)
								dp[i][1] -= dp[i - 3][2] + dp[i - 3][3];
							if (j == 2)
								dp[i][2] -= dp[i - 3][1] + dp[i - 3][3];
							if (j == 3)
								dp[i][3] -= dp[i - 3][1] + dp[i - 3][2];
						}
					}
				}
				dp[i][0] += dp[i][j];
			}
		}
	}
	cout << dp[n][0] % 10000 << endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin >>n;
int k;
cin >>k;
int a[n][3][3];//n+1個の最後から2番目-1,1番目-1
int b[n];
int c;
c=0;
while(c<n){
b[c]=-1;
c=c+1;}
int d;
int e;
c=0;
while(c<k){
cin >>d;
cin >>e;
b[d-1]=e-1;
c=c+1;}
int f;
int h;
c=0;
while(c<3){
f=0;
while(f<3){
h=0;
if(b[0]!=c){if(b[0]!=-1){h=1;}}
if(b[1]!=f){if(b[1]!=-1){h=1;}}
if(h==1){a[1][c][f]=0;}
if(h==0){a[1][c][f]=1;}
f=f+1;}
c=c+1;}
int g;
c=2;
while(c<n){
f=0;
while(f<3){
g=0;
while(g<3){
a[c][f][g]=a[c-1][0][f]+a[c-1][1][f]+a[c-1][2][f];
if(f==g){a[c][f][g]=a[c][f][g]-a[c-1][f][f];}
if(b[c]!=f){if(b[c]!=-1){a[c][f][g]=0;}}
a[c][f][g]=a[c][f][g]%10000;
g=g+1;}
f=f+1;}
c=c+1;}
cout << a[n-1][0][0]+a[n-1][0][1]+a[n-1][0][2]+a[n-1][1][0]+a[n-1][1][1]+a[n-1][1][2]+a[n-1][2][0]+a[n-1][2][1]+a[n-1][2][2]<<endl;
}

C++: import java.util.*;
public class Main {
	static int cnt=0;
	static int n;
	static void bfs(int a,int b,int c,int day){
		if(a==b&&b==c)return;
		if(day-1==n){
			cnt++;
			return;
		}
		a=b;
		b=c;
		if(hyou[day]>0){
			bfs(a,b,hyou[day],day+1);
		}else{
			for(int i=1;i<=3;i++){
				if(a==b&&b==i){
					continue;
				}else bfs(a,b,i,day+1);
			}
		}
		return;
	}
	static int hyou[] = new int[101];
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		int k = in.nextInt();
		for(int s=0;s<n;s++)hyou[s]=0;
		int a=0,b=0,c=0;
		boolean z=false,x=false,y=false;
		for(int i=0;i<k;i++){
			int w = in.nextInt();
			hyou[w]=in.nextInt();
			if(w==1){
				a=hyou[w];
				z=true;
			}
			else if(w==2){
				b=hyou[w];
				x=true;
			}
			else if(w==3){
				c=hyou[w];
				y=true;
			}
		}
		for(int s=1;s<=3;s++){
			if(!z)a=s;
			for(int i=1;i<=3;i++){
				if(!x)b=i;
				for(int j=1;j<=3;j++){
					if(!y)c=j;
					if(a==b&&a==c)continue;
					bfs(a,b,c,4);
					if(y)break;
				}
				if(x)break;
			}
			if(z)break;
		}
		System.out.println(cnt%10000);
	}
}