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>
using namespace std;

struct day
{
  int kind[4];
  int order;
  int sum;
};

int n, k;
int a[100], b[100];
day dp[101];

int main()
{
  cin >> n >> k;
  for(int i = 0; i < k; i++)
    cin >> a[i] >> b[i];

  for(int i = 0; i < k; i++)
    dp[a[i]].order = b[i];

  //day 1
  if(dp[1].order)
    dp[1].sum = dp[1].kind[dp[1].order] = 1;
  else
    for(int i = 1; i <= 3; i++)
      dp[1].sum += dp[1].kind[i] = 1;

  //day 2
  if(dp[2].order)
    dp[2].sum = dp[2].kind[dp[2].order] = dp[1].sum;
  else
    for(int i = 1; i <= 3; i++)
      dp[2].sum += dp[2].kind[i] = dp[1].sum;

  //day 3
  for(int i = 3; i <= n; i++){
    if(dp[i].order){
      if(!dp[i - 1].kind[dp[i].order])
	dp[i].sum = dp[i].kind[dp[i].order] = dp[i - 1].sum;
      else
	dp[i].sum = dp[i].kind[dp[i].order] = dp[i - 1].sum - dp[i - 1].kind[dp[i].order] + dp[i - 2].sum - dp[i - 2].kind[dp[i].order];
    }
    else{
      for(int j = 1; j <= 3; j++){
	
	if(!dp[i - 1].kind[j])
	  dp[i].sum += dp[i].kind[j] = dp[i - 1].sum;
	else
	  dp[i].sum += dp[i].kind[j] = dp[i - 1].sum - dp[i - 1].kind[j] + dp[i - 2].sum - dp[i - 2].kind[j];
      }

      dp[i].sum = dp[i].sum % 10000;
    }
  }

  cout << (dp[n].sum % 10000 + 10000) % 10000 << endl;
  return 0;
}

C++: #include<stdio.h>
#include<string.h>
 
int n,k;
int dp[105][5][5]={};
//dp[a][b][c] .. a日目に、今日はb、昨日はcのパスタを食べた場合の通り数
int dat[105]={};
 
int main(void){
	scanf("%d%d",&n,&k);
	int d,i,j,p;
	for(i=1;i<=k;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		dat[a] = b;
	}
 
	for(i=1;i<=3;i++){
		for(j=1;j<=3;j++){
			if(dat[1]!=0 && dat[1]!=j){
				continue;
			}
			if(dat[2]!=0 && dat[2]!=i){
				continue;
			}
			dp[2][i][j]=1;
		}
	}
	//カレンダーに合わない食べ方なら、0
	//そうでなければ、1
	for(d=3;d<=n;d++){
		for(i=1;i<=3;i++){
			for(j=1;j<=3;j++){
				//dp[d][i][j]について
				if(dat[d-1]!=0 && dat[d-1]!=j){
					dp[d][i][j]=0;
				}
				else if(dat[d]!=0 && dat[d]!=i){
					dp[d][i][j]=0;
				}
				//カレンダーに合わないなら、飛ばす(0)
				else{
					for(p=1;p<=3;p++){
						if(i==j && j==p)continue;
						//三日連続で同じのなら、飛ばす（足さない）
						dp[d][i][j] += dp[d-1][j][p];
					}
					dp[d][i][j]%=10000;
				}
				printf("%d ",dp[d][i][j]);
			}
		}
		printf("\n");
	}
 
	int ans=0;
	for(i=1;i<=3;i++){
		for(j=1;j<=3;j++){
			//総和
			ans += dp[n][i][j];
		}
	}
	ans %= 10000;
 
	printf("%d\n",ans);
	return 0;
}

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);
	}
}

C++: #include<iostream>
#include<map>
using namespace std;
int main(){
  int n,m;
  map<int,int> day;
  cin >>n>>m;
  for(int i=0,a,b; i<m; i++){
    cin >>a>>b;
    day[a-1] = b;
  }
  int dp[101][3][3]={0};
  if(day[0]!=0) dp[0][day[0]-1][0] = 1;
  else for(int i=0; i<3; i++) dp[0][i][0] = 1;
  for(int i=1; i<n; i++){
    if(day[i] == 0){
      for(int kinou=0; kinou<3; kinou++){
	for(int kyou=0; kyou<3; kyou++){
	  for(int len=0; len<2; len++){
	    if(kyou == kinou) dp[i][kyou][len+1] =(dp[i][kyou][len+1]+dp[i-1][kinou][len])%10000;
	    else dp[i][kyou][0] = (dp[i][kyou][0]+dp[i-1][kinou][len])%10000;
	  }
	}
      }
    }
    else{
      for(int kinou=0; kinou<3; kinou++){
	for(int len=0; len<2; len++){
	  if(day[i]-1 == kinou) dp[i][day[i]-1][len+1] = (dp[i][day[i]-1][len+1]+dp[i-1][kinou][len])%10000;
	  else dp[i][day[i]-1][0] = (dp[i][day[i]-1][0]+dp[i-1][kinou][len])%10000;
	}
      }
    }
  }
  int ans = 0;
  for(int i=0; i<3; i++){
    for(int j=0; j<2; j++){
      ans+=dp[n-1][i][j];
    }
  }
  cout <<ans%10000<<endl;
  return 0;
}

C++: #include<cstdio>
#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cctype>
#define MM 0
#define rep(i,a) for(int i=0;i<a;i++)
#define rep2(i,a) for(int i=1;i<=a;i++)
#define rep3(i,a) for(int i=1;i<a;i++)
#define rep4(i,a) for(int i=0;i<=a;i++)
using namespace std;
int dp[101][4][4];
long long int sum;
int main(void)
{
	int x,y;
	int day,al,sosu[101]={MM};
	cin>>day>>al;
	rep(i,al) {cin>>x>>y;sosu[x]=y;}
	dp[MM][MM][MM]=1;
	rep2(i,day)rep(j,4)rep(k,4)rep3(kk,4) {
		if((j!=k || k!=kk)&&(sosu[i]==MM || sosu[i]==kk)) {
            dp[i][k][kk]+=dp[i-1][j][k];
            dp[i][k][kk]%=10000;
		}
	}
	sum=0;
	rep2(i,3)rep4(j,3) sum+=dp[day][i][j];
	int sm;
	sm=sum%10000;
	cout<<sm<<endl;
	return 0;
}