sgu 197 Nice Patterns Strike Back(构造矩阵快速幂)

197. Nice Patterns Strike Back
time limit per test: 1 sec.
memory limit per test: 65536 KB
input: standard
output: standard



You might have noticed that there is the new fashion among rich people to have their yards tiled with black and white tiles, forming a pattern. The company Broken Tiles is well known as the best tiling company in our region. It provides the widest choices of nice patterns to tile your yard with. The pattern is nice if there is no square of size 2 × 2, such that all tiles in it have the same color. So patterns on the figure 1 are nice, while patterns on the figure 2 are not.





The president of the company wonders whether the variety of nice patterns he can provide to the clients is large enough. Thus he asks you to find out the number of nice patterns that can be used to tile the yard of size N × M. Now he is interested in the long term estimation, so he suggests N ≤ 10 100. However, he does not like big numbers, so he asks you to find the answer modulo P.

Input

The input file contains three integer numbers: N (1 ≤ N ≤ 10 100), M (1 ≤ M ≤ 5) and P (1 ≤ P ≤ 10000).

Output

Write the number of nice patterns of size N × M modulo P to the output file.

Sample test(s)

Input
 
Test #1 2 2 5 Test #2 3 3 23
 

Output
 
Test #1 4 Test #2 0 
题意:求不包含一个完全同色的2*2方块的方案数
题解:根据状态转移是否合法构造矩阵,然后进行快速幂
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int mod,m,cou,all,len;
char s[1008];
char make[35][35];
int a[1008];
struct mat{
    int a[35][35];
}res,e;
void dfs(int x,char *s)
{
    if(x==m)
    {
        s[x]='\0';
        strcpy(make[cou],s);
        cou++;
        return;
    }
    s[x]='0'; dfs(x+1,s);
    s[x]='1'; dfs(x+1,s);
}
int judge(int x,int y)
{
    int i;

    for(i=0;i<m-1;i++)
    {
        if(make[x][i]==make[y][i])
            if(make[x][i+1]==make[y][i+1])
            if(make[x][i]==make[x][i+1])
            return 0;
    }

    return 1;
}
void print(struct mat temp)
{
    int i,j;

    for(i=0;i<all;i++)
    {
        for(j=0;j<all;j++)
            printf("%d  ",temp.a[i][j]);
        printf("\n");
    }
    printf("\n");
}
void init()
{
    char s0[35];
    int i,j;

    len=strlen(s);
    for(j=0,i=len-1;i>=0;i--,j++) a[j]=s[i]-'0';
    for(j=0;j<len;j++)
    {
        a[j]--;
        if(a[j]<0) a[j]=9;
        else break;
    }
    if(j>=len) len--;
    cou=0;  all=(1<<m);
    dfs(0,s0);
    for(i=0;i<all;i++)
    {
        for(j=0;j<all;j++)
        {
            if(judge(i,j)) e.a[i][j]=1;
            else e.a[i][j]=0;
        }
    }
    memset(res.a,0,sizeof(res.a));
    for(i=0;i<all;i++) res.a[i][i]=1;
}
void chuer()
{
    int flag=0,temp,i;

    for(i=len-1;i>=0;i--)
    {
        if(a[i]&1) temp=1;
        else temp=0;
        a[i]=(a[i]+10*flag)>>1;
        flag=temp;
    }
    if(a[len-1]==0) len--;
}
struct mat mult(struct mat x,struct mat y)
{
    struct mat temp;
    int i,j,k;

    for(i=0;i<all;i++)
    {
        for(j=0;j<all;j++)
        {
            for(temp.a[i][j]=k=0;k<all;k++)
                temp.a[i][j]=(temp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
        }
    }

    return temp;
};
void solve()
{
    int ans,i,j;

    while(len)
    {
        if(a[0]&1) res=mult(res,e);
        e=mult(e,e);
        chuer();
    }
    for(ans=i=0;i<all;i++)
    {
        for(j=0;j<all;j++)
            ans=(ans+res.a[i][j])%mod;
    }

    printf("%d\n",ans);
}
int main()
{
    //freopen("t.txt","r",stdin);
    while(scanf("%s%d%d",s,&m,&mod)>0)
    {
        init();
        solve();
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值