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<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,b,n) for(int i=b;i<n;i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int N,k;
const int MOD =10000;
const int MAX_N =110;
int plan[MAX_N+1];
int memo[MAX_N][4][4];
int solve(int n,int a,int b){//n日目n-1日目がa,n-2日目がbの時計算
	if(n==N+1)return 1;
	if(memo[n][a][b]!=-1)return memo[n][a][b];
	if(plan[n]!=0){
		if(a==b&&a==plan[n])return 0;
		else return solve(n+1,plan[n],a)%MOD;
	}
	int t = 0;
	for(int i =1;i <=3;i++){
		if(a==b&&a==i)continue;
		t +=solve(n+1,i,a)%MOD;
	}
	return memo[n][a][b]=t%MOD;
}


int main(){
	cin>>N>>k;
	rep(i,k){
		int d,k;
		cin>>d>>k;
		plan[d]=k;
	}
	for(int i =0;i <=N;i++){
		for(int j =0;j <4;j++){
			for(int k =0;k <4;k++){
				memo[i][j][k] =-1;
			}
		}
	}
	cout <<solve(1,0,0)<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define MOD 10000
using namespace std;

int P[100];
int n, k;
int memo[4][101][4];

int search(int ren, int d, int p){
  if(d == n) return 1;
  if(memo[ren][d][p] != -1) return memo[ren][d][p];

  int res = 0;
  if(P[d] == 0){
    for(int i = 1; i <= 3; i++){
      if(p == i){
	if(ren + 1 >= 3) continue;
	res += search(ren+1, d+1, i) % MOD;
      }else{
	res += search(1, d+1, i) % MOD;
      }
    }
  }else{
    if(p == P[d]){
      if(ren + 1 >= 3) res = 0;
      else res += search(ren+1, d+1, P[d]) % MOD;
    }
    else res += search(1, d+1, P[d]) % MOD;
  }

  memo[ren][d][p] = res % MOD;
  return memo[ren][d][p];
  //return res;
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;
  for(int i = 0; i < 4; i++) for(int j = 0; j < 100; j++) for(int k = 0; k < 4; k++) memo[i][j][k] = -1;
  for(int i = 0; i < 100; i++){
    P[i] = 0;
  }
  for(int i = 0; i < k; i++){
    int d, p;
    cin >> d >> p;
    d--;
    P[d] = p;
  }

  cout << search(1, 0, 0) << endl;

  return 0;
}

C++: #include<bits/stdc++.h>
#define N 102
using namespace std;

int main(){
  int dp[N][4][4]={},n,k,d[N]={};
  cin>>n>>k;
  int a,b;
  for(int i=0;i<k;i++)cin>>a>>b,d[a]=b;
    
  if(d[1]>0 && d[2]>0) dp[3][d[2]][d[1]]=1;
  else if(d[1]>0)for(int i=1;i<4;i++)dp[3][i][d[1]]=1;
  else if(d[2]>0)for(int i=1;i<4;i++)dp[3][d[2]][i]=1;
  else{
    for(int i=1;i<4;i++)
      for(int j=1;j<4;j++)dp[3][j][i]=1;
  }
  
  for(int x=1;x<=n;x++){
    for(int d1=1;d1<4;d1++){
      for(int d2=1;d2<4;d2++){
	if(d[x]==d1 && d1==d2) continue;
	if(d[x]) dp[x+1][d[x]][d1]=(dp[x][d1][d2]+dp[x+1][d[x]][d1])%10000;
	else{
	  for(int i=1;i<4;i++){
	    if(d2==i && d2==d1)continue;
	    dp[x+1][i][d1]=(dp[x][d1][d2]+dp[x+1][i][d1])%10000;
	  }
	}
      }
    }
  }
  
  int ans=0;
    for(int i=1;i<4;i++)
      for(int j=1;j<4;j++)ans+=dp[n+1][i][j] ;
    
  cout<<ans%10000<<endl;
  
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
typedef pair<int,int> P;
int n,k;
P a[1000];
int i,j,l,m;
long long int dp[1000][4][4],cnt=0;
int flg1=0,flg2=0;
void func();
int main(void){
	scanf("%d %d",&n,&k);
	for(i=1;i<=k;i++){
		scanf("%d %d",&a[i].first,&a[i].second);
	}
	sort(a,a+k+1);
	for(i=1;i<=n+1;i++){
		for(j=1;j<=3;j++){
			for(l=1;l<=3;l++){
				dp[i][j][l]=0;
			}
		}
	}
	for(i=3;i<=n+1;i++){
		for(m=1;m<=k;m++){
			if(a[m].first==i-2){//もしi-2日目に設定があったら
				for(j=1;j<=3;j++){
					for(l=1;l<=3;l++){
						if(l!=a[m].second){
							dp[i][j][l]=-1;
						}
					}
				}
			}
			if(a[m].first==i-1){
				for(l=1;l<=3;l++){
					for(j=1;j<=3;j++){
						if(j!=a[m].second){
							dp[i][j][l]=-1;
						}
					}
				}
			}
		}
	}
	for(j=1;j<=3;j++){
		for(l=1;l<=3;l++){
			if(dp[3][j][l]!=-1){
				dp[3][j][l]=1;
			}
		}
	}
	for(i=3;i<=n+1;i++){
		for(j=1;j<=3;j++){
			for(l=1;l<=3;l++){
				if(dp[i][j][l]>-1){
					for(m=1;m<=3;m++){
						if(dp[i+1][m][j]>-1){
							if(m==j && j==l);
							else {
								dp[i+1][m][j]+=dp[i][j][l];
								dp[i+1][m][j]%=10000;
							}
						}
					}
				}
			}
		}
	}
	
	/*for(i=3;i<=n+1;i++){
		for(j=1;j<=3;j++){
			for(l=1;l<=3;l++){
				printf("%lld ",dp[i][j][l]);
			}
			printf("\n");
		}
		printf("\n");
	}*/
	long long int cnt=0;
	for(j=1;j<=3;j++){
		for(l=1;l<=3;l++){
			if(dp[n+1][j][l]>-1){
				cnt+=dp[n+1][j][l];
				cnt%=10000;
			}
		}
	}
	printf("%lld\n",cnt);
	return 0;
}