HDU 4332(状态压缩dp+矩阵连乘)

Constructing Chimney

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 947    Accepted Submission(s): 368


Problem Description
Now we are planning to construct a big chimney. The chimney’s section is a 3*3 square and the center of it will have nothing like the picture below. Now we only have a lot of bricks whose size if 1*1*2, and we want to build a chimney whose height is N, so can help us to calculate how many ways we can build the chimney? The answer may be very large, so you can just tell the answer’s remainder divided by 1000000007.

                                          

Input
The first line of the input contains an integer T (1<=T<=10) which means the number of test cases.
For each test cases, there is only one line with an integer N (1<=N<=10^9) which means the height of the chimney we want to build.
 

Output
For each test case, please output a line which is "Case X: Y ", X means the number of the test case and Y means the remainder divided by 1000000007 of the answer.
 

Sample Input
2
1
2
 

Sample Output
Case 1: 2
Case 2: 49

 

题目大意是求横截面如上图的高为h的烟囱,用1*1*2的砖块有几种搭法。

 

用状态压缩dp求出相邻两层的各种状态之间到达种数的矩阵。再用快速幂求矩阵连乘的和。

由于奇数块砖的状态一定无法到达所以,状态其实只有128种;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#define mod 1000000007
using namespace std;
__int64 q[300][300],f[35][200][200],ans[200][200];
int ou[300],g[300];
void calc(int a){
    __int64 c[200][200];
    int i,j,k;
    for(i=0;i<=127;i++)
	    for(j=0;j<=127;j++){
	        c[i][j]=0;
	        for(k=0;k<=127;k++){
	            c[i][j]+=ans[i][k]*f[a][k][j];
	            c[i][j]%=mod;
	        }
	    }
    for(i=0;i<=127;i++)
    	for(j=0;j<=127;j++)
        	ans[i][j]=c[i][j];
   	return ;
}
void cheng(int a,int b)
{
    __int64 c[200][200];
    int i,j,k;
    for(i=0;i<=127;i++)
	    for(j=0;j<=127;j++){
	        f[a][i][j]=0;
	        for(k=0;k<=127;k++){
	            f[a][i][j]+=f[b][i][k]*f[b][k][j];
	            f[a][i][j]%=mod;
	        }
	    }
    return ;
}
void erci(){
    int i, j;
    for(i=0;i<=127;i++)
    	for(j=0;j<=127;j++){
        	f[0][i][j]=q[i][j];
    	}
    for(i=1;i<=31;i++){
        cheng(i,i-1);
    }
}
int find(int a,int b)
{
    int i,t1,tt,j;
    if(a==255)a=b,b=255;
    if(a==0)t1=0;
    else{
        for(i=0;i<8;i++){
            if(i==0)j=7;
            else j=i-1;
            if(((1<<j)&a)==0&&((1<<(i))&a)){
                t1=i;break;
            }
        }
    }
    tt=0;i=t1;
    while(tt<8){        
		j=(i+1)%8;
        if(((1<<i)&a)==0){
            if(((1<<i)&b)==0)return 0;
        }
        else{
            if(((1<<j)&a)&&((1<<i)&b)&&((1<<j)&b))i++;
            else if(((1<<i)&b)!=0)return 0;
        }
        tt++;i=(i+1)%8;
    }
    return 1;
}
void start()
{
    int i,j,k;
    for(i=0;i<=255;i++)
    	for(j=0;j<=255;j++)
    		if(ou[i]&&ou[j])q[g[i]][g[j]]=find(i,j);
    q[127][127]=2;
    return ;
}
void panou(){
	int i,j,tt; 
    for(i=0;i<=255;i++){
        tt=0;
        for(j=0;j<8;j++)
        	if(((1<<j)&i))tt++;
        if(tt%2==0)ou[i]=1;
        else ou[i]=0;
    }
    tt=0;
    for(i=0;i<=255;i++)
		if(ou[i])g[i]=tt++;
   	return ;
}
int main()
{
    int t,tt,n,i,j,k;
    panou();
    start();
    erci();
    scanf("%d", &t);
    for(i=1;i<=t;i++){
        scanf("%d", &n);
        for(j=0;j<=127;j++)
        	for(k=0;k<=127;k++)
        		ans[j][k]=0;
  		for(j=0;j<=127;j++)
  			ans[j][j]=1;
		tt=0;
		while(1<<tt<=n){
		   if((1<<tt)&n)calc(tt);
		   tt++;
        }
        printf("Case %d: %I64d\n",i,ans[127][127]);
    }
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值