题目链接:http://codeforces.com/problemset/problem/256/E
题意:给出一个数列,开始全是0。给出一个3*3的01矩阵p,定义p[i][j]=1表示(i,j)是Good。现在有m个操作,x y,表示将数列第x个位置上的数字改为y(0<=y<=3)。问改完后,有多少种方式将数列中现有的0改为1到3中任意一个数字使得数列中相邻两个数字的组合都是Good。
思路:线段树节点保存a[i][j],表示该段中以i开始以j结束的方式有多少种。
struct node
{
int L,R,mid,a[4][4];
void init()
{
clr(a,0);
a[1][1]=a[2][2]=a[3][3]=1;
}
};
const int mod=777777777;
const int MAX=77777;
node a[MAX<<2];
int n,m,p[4][4];
void cal(int t)
{
int i,j,x,y;
i64 temp;
for(i=1;i<=3;i++) for(j=1;j<=3;j++)
{
a[t].a[i][j]=0;
for(x=1;x<=3;x++) for(y=1;y<=3;y++) if(p[x][y])
{
temp=(i64)a[t*2].a[i][x]*a[t*2+1].a[y][j]%mod;
a[t].a[i][j]=(a[t].a[i][j]+temp)%mod;
}
}
}
void build(int t,int L,int R)
{
a[t].L=L;
a[t].R=R;
a[t].mid=(L+R)>>1;
if(L==R)
{
a[t].init();
return;
}
build(t*2,L,a[t].mid);
build(t*2+1,a[t].mid+1,R);
cal(t);
}
void update(int t,int x,int y)
{
if(a[t].L==x&&a[t].R==x)
{
if(y==0) a[t].init();
else
{
clr(a[t].a,0);
a[t].a[y][y]=1;
}
return;
}
if(x<=a[t].mid) update(t*2,x,y);
else update(t*2+1,x,y);
cal(t);
}
int main()
{
while(scanf("%d%d",&n,&m)!=-1)
{
int i,j;
for(i=1;i<=3;i++) for(j=1;j<=3;j++)
{
scanf("%d",&p[i][j]);
}
build(1,1,n);
i64 ans;
while(m--)
{
scanf("%d%d",&i,&j);
update(1,i,j);
ans=0;
for(i=1;i<=3;i++) for(j=1;j<=3;j++)
{
ans=(ans+a[1].a[i][j])%mod;
}
printf("%I64d\n",ans);
}
}
return 0;
}