现在的位置: 首页 > 综合 > 正文

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

2014年10月22日 ⁄ 综合 ⁄ 共 2505字 ⁄ 字号 评论关闭

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 ≤ 10100. 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 ≤ 10100), 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;
}

抱歉!评论已关闭.