求助矩阵快速幂
  • 板块学术版
  • 楼主Ink_Bottle
  • 当前回复7
  • 已保存回复7
  • 发布时间2020/7/7 20:32
  • 上次更新2023/11/6 23:29:33
查看原帖
求助矩阵快速幂
263469
Ink_Bottle楼主2020/7/7 20:32

QAQ,蒟蒻刚学这个,敲了个代码但是全输出 00,请大佬帮忙看一下

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int Maxm=53,mod=1e9+7;
int h[Maxm],inv[Maxm],n,m;
struct matrix{
	int r,c;
	int g[Maxm][Maxm];
	matrix(){}
	matrix(int _r,int _c):
		r(_r),c(_c){memset(g,0,sizeof(g));}
	inline void init()
	{
		for(int i=1;i<=r;i++) g[i][i]=1;
		return;
	}
	inline matrix operator *(const matrix & rhs)const
	{
		matrix res=matrix(r,rhs.c);
		for(int i=1;i<=r;i++)
		  for(int k=1;k<=c;k++)
		    for(int j=1;j<=rhs.c;j++)
		    res.g[i][j]=(1LL*g[i][k]*rhs.g[k][j]+res.g[i][j])%mod;
	}
	inline matrix operator ^(int y)const
	{
		matrix x=* this,res=matrix(r,c);
		res.init();
		while(y)
		{
			if(y&1) res=res*x;
			x=x*x;
			y>>=1;
		}
		return res;
	}
}A,B;
inline void put(int x)
{
	if(x<0)
	{
		putchar('-');
		x=~x+1;
	}
	if(x>9) put(x/10);
	putchar(x%10+'0');
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	n>>=1;
	m>>=1;
	inv[1]=1;
	for(int i=2;i<=m+1;i++)
	inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
	h[0]=2;
	for(int i=1;i<=m-1;i++)
	h[i]=1LL*h[i-1]*(4*i-2)%mod*inv[i+1]%mod;
	A=matrix(1,m);
	A.g[1][1]=1;
	B=matrix(m,m);
	for(int i=1;i<=m;i++) B.g[i][1]=h[i-1];
	for(int i=1;i<=m-1;i++) B.g[i][i+1]=1;
	A=A*(B^n);
	put(A.g[1][1]);
	return 0;
}
2020/7/7 20:32
加载中...