Problem:
Three-way Branch

There is a grid that consists of W \times H cells. The upper-left-most cell is (1, 1).
You are standing on the cell of (1,1) and you are going to move to cell of (W, H).
You can only move to adjacent lower-left, lower or lower-right cells.

There are obstructions on several cells. You can not move to it. You cannot move out the grid, either.
Write a program that outputs the number of ways to reach (W,H) modulo 1,000,000,009.
You can assume that there is no obstruction at (1,1).

Input

The first line contains three integers, the width W, the height H, and the number of obstructions N.
(1 \leq W \leq 75, 2 \leq H \leq 10^{18}, 0 \leq N \leq 30)
Each of following N lines contains 2 integers, denoting the position of an obstruction (x_i, y_i).

The last test case is followed by a line containing three zeros.

Output

For each test case, print its case number and the number of ways to reach (W,H) modulo 1,000,000,009.

Sample Input

2 4 1
2 1
2 2 1
2 2
0 0 0

Output for the Sample Input

Case 1: 4
Case 2: 0

Solution in C++ (judge verdict: Accepted)

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
long long mod=1000000009;
int x[32];
long long y[32];
int mat[63][75][75];
int val[75][75];
int tmp[75][75];
int sen[75][75];
long long now[110];
long long to[110];
long long z[110];
int v[110];

int main(){
	int tc=0;
	int a,c;
	long long b;
	while(scanf("%d%lld%d",&a,&b,&c),a){
		bool dame=false;
		for(int i=0;i<c;i++){
			scanf("%d%lld",x+i,y+i);
			x[i]--;y[i]--;
			z[i]=y[i];
			if(x[i]==a-1&&y[i]==b-1){
				dame=true;
			}
		}
		if(dame){
			printf("Case %d: 0\n",++tc);continue;
		}
		std::sort(z,z+c);
		z[c]=b-1;
		for(int i=0;i<a;i++)for(int j=0;j<a;j++)sen[i][j]=0;
		for(int i=0;i<a;i++){
			sen[i][i]++;
			if(i)sen[i-1][i]++;
			if(i<a-1)sen[i+1][i]++;
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++)mat[0][i][j]=sen[i][j];
		}
		for(int i=1;i<63;i++){
			for(int j=0;j<a;j++)for(int k=0;k<a;k++)mat[i][j][k]=0;
			for(int j=0;j<a;j++)for(int k=0;k<a;k++)for(int l=0;l<a;l++)
				mat[i][j][k]=(mat[i][j][k]+(long long)mat[i-1][j][l]*mat[i-1][l][k])%mod;
		}
		for(int i=0;i<a;i++)now[i]=0;
		now[0]=1;
		int at=0;
		long long last=0;
		for(int i=0;i<=c;i++){
			if(i&&z[i]==z[i-1])continue;
			for(int j=0;j<a;j++)for(int k=0;k<a;k++)val[j][k]=0;
			for(int j=0;j<a;j++)val[j][j]=1;
			long long d=z[i]-last;
			for(int s=0;s<63;s++){
				if(d&(1LL<<s)){
					for(int j=0;j<a;j++)for(int k=0;k<a;k++)tmp[j][k]=0;
					for(int l=0;l<a;l++)for(int j=0;j<a;j++)if(mat[j][l])for(int k=0;k<a;k++){
						if(val[l][k])tmp[j][k]=(tmp[j][k]+(long long)mat[s][j][l]*val[l][k])%mod;
					}
					for(int j=0;j<a;j++)for(int k=0;k<a;k++)val[j][k]=tmp[j][k];
				}
			}
			for(int j=0;j<a;j++){
				to[j]=0;
			}
			for(int j=0;j<a;j++){
				for(int k=0;k<a;k++)to[j]=(to[j]+now[k]*val[j][k])%mod;
			}
			for(int j=0;j<a;j++)now[j]=to[j];
			if(i==c)break;
			last=z[i]+1;
			for(int j=0;j<a;j++)v[j]=0;
			for(int j=0;j<c;j++)if(z[i]==y[j])v[x[j]]=1;
			for(int j=0;j<a;j++)to[j]=0;
			for(int j=0;j<a;j++){
				if(v[j])continue;
				for(int k=-1;k<=1;k++){
					if(0<=j+k&&j+k<a)to[j+k]=(to[j+k]+now[j])%mod;
				}
			}
			for(int j=0;j<a;j++)now[j]=to[j];
		}
		printf("Case %d: %lld\n",++tc,now[a-1]);
	}
}