Covering HDU - 6185(暴力递推+矩阵快速幂)

Bob’s school has a big playground, boys and girls always play games here after school.

To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.

Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.

He has infinite carpets with sizes of 1×21×2 and 2×12×1, and the size of the playground is 4×n4×n.

Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?
Input
There are no more than 5000 test cases.

Each test case only contains one positive integer n in a line.

1≤n≤10181≤n≤1018
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5

一看就知道是个规律题,数学上来先打表.但是打表也不太好打…还是太菜了.
暴力打表:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxx=1e3+100;
bool vis[maxx][maxx];
int n;
int ans;

void dfs(int sum)
{
	if(sum==4*n)
	{
		ans++;
		return ;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<4;j++)
		{
			if(vis[i][j]==0)
			{
				if(vis[i][j+1]==0&&j+1<4)
				{
					vis[i][j]=1;
					vis[i][j+1]=1;
					dfs(sum+2);
					vis[i][j]=0;
					vis[i][j+1]=0;
				}
				if(vis[i+1][j]==0&&i+1<n)
				{
					vis[i][j]=1;
					vis[i+1][j]=1;
					dfs(sum+2);
					vis[i][j]=0;
					vis[i+1][j]=0;
				}
				return ;
			}
		}
	}
}
int main()
{
	while(cin>>n)
	{
		memset(vis,0,sizeof(vis));
		ans=0;
		dfs(0);
		cout<<ans<<endl;
	}
	return 0;
}
//f[n]=-f[n-4]+f[n-3]+5f[n-2]+f[n-1]
打表之后的规律

1e18的数据量,矩阵快速幂…
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define mod 1000000007
using namespace std;

const int maxx=5;
ll a[maxx][maxx];
ll ans[maxx][maxx];
ll n,m;

void init()
{
	a[1][1]=1;a[1][2]=5;a[1][3]=1;a[1][4]=-1;
	a[2][1]=1;a[2][2]=0;a[2][3]=0;a[2][4]=0;
	a[3][1]=0;a[3][2]=1;a[3][3]=0;a[3][4]=0;
	a[4][1]=0;a[4][2]=0;a[4][3]=1;a[4][4]=0;
}

void multy(ll b[5][5],ll c[5][5])
{
	int tep[5][5];
	memset(tep,0,sizeof(tep));
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			for(int k=1;k<=n;k++)
			{
				tep[i][j]=(tep[i][j]+(b[i][k]*c[k][j])%mod)%mod;
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			b[i][j]=tep[i][j]%mod;
		}
	}
}

void qsm(ll y)
{
	memset(ans,0,sizeof(ans));
	for(int i=1;i<=4;i++) ans[i][i]=1;
	while(y)
	{
		if(y&1)
		multy(ans,a);
		y/=2;
		multy(a,a);
	}
}

int main()
{
	while(scanf("%lld",&m)!=EOF)
	{
		if(m==1) cout<<1<<endl;
		else if(m==2) cout<<5<<endl;
		else if(m==3) cout<<11<<endl;
		else if(m==4) cout<<36<<endl;
		else
		{
			init();
			n=4;
			m-=4;
			qsm(m);
			ll sum=0;
			sum+=ans[1][1]*36%mod;
			sum=(sum%mod+mod)%mod;
			sum+=ans[1][2]*11%mod;
			sum=(sum%mod+mod)%mod;
			sum+=ans[1][3]*5%mod;
			sum=(sum%mod+mod)%mod;
			sum+=ans[1][4]*1%mod;
			sum=(sum%mod+mod)%mod;
			printf("%lld\n",sum%mod);
		}
	}
}

努力加油a啊,(o)/~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值