vijos P1067 守望者的烦恼

预处理然后矩阵加速递推

就是预处理的时候还可以以自己作为起点所以还有个自加所以在初始化矩阵的时候我们还需要+1,忽略这点就只有30了= =

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define LL long long 
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline LL read()
{
	LL d=0,f=1;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
	return d*f;
}
#define N 10
#define inf 7777777
int n,m;

struct matrix
{
	LL a[N][N];
	void clear()
	{
		memset(a,0,sizeof(a));
	}
	void check()
	{
		fo(i,0,m-1)
		{
			fo(j,0,m-1)
			{
				cout<<a[i][j];
			}
			cout<<endl;
		}
		cout<<endl;
	}
	matrix operator*(const matrix b)const
	{
		matrix anss;anss.clear();
		fo(i,0,m-1)
		fo(j,0,m-1)
		{
			fo(k,0,m-1)
			{
				anss.a[i][j]+=a[i][k]*(b.a[k][j]);
				anss.a[i][j]%=inf;
			}
		}
		return anss;
	}
};
matrix I;

void getI()
{
	fo(i,0,m-1)
	fo(j,0,m-1)
	if(i==j)I.a[i][j]=1;
}

matrix KSM(matrix a,LL b)
{
	if(b==0)return I;
	if(b==1)return a;
	matrix ret=KSM(a,b/2);
	ret=ret*ret;
	if(b%2)ret=ret*a;
	return ret;
}

int main()
{
	m=read();n=read();
	getI();
	matrix A,ans,cnt;ans.clear();
	ans.a[m-1][0]=1;ans.a[m-2][0]=2;
	down(i,m-3,0)
	{
		LL anss=1;
		fo(j,i+1,i+m)
		if(j>=m)break;
		else anss+=ans.a[j][0];
		ans.a[i][0]=anss;
	}
	if(n<=m)
	{
		cout<<ans.a[m-n][0]<<endl;
		return 0;
	}
	fo(i,0,m-1)A.a[0][i]=1;
	fo(i,1,m-1)
	fo(j,0,m-1)
	if(i==j+1)A.a[i][j]=1;
	else A.a[i][j]=0;
	
	cnt=KSM(A,n-m);
//	cnt.check();
	ans=cnt*ans;
//	ans.check();
	cout<<ans.a[0][0]<<endl;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值