算法提高 翔集合

算法提高 翔集合

//矩阵快速幂实现翔集合 
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
struct node{
     
	ll A[5][5];
	node(){
     
		for(int i = 0;i<5;i++)
		 for(int j = 0;j<5;j++)
		  A[i][j]=0;
		}
}x,y;
ll n;
void set()
{
     
	x.A[0][0]=x.A[0][2]=x.A[0][3]=1;
	x.A[1][0] = 1;
	x.A[2][1] =1;
	x.A[3][3]=x.A[3][4]=1;
	x.A[4][4]=1;
	
    y.A [3][0] =1;
	y.A [4][0] = 1;
}
struct node Mul(node tmp1,node tmp2)
{
     
	node tmp3;
	for(int i =0;i<5;i++)
	{
     
		for(int j = 0;j<5;j++)
		{
     
			for(int k = 0;k<5;k++)
			{
     
				tmp3.A[i][j]+=(tmp1.A[i][k]*tmp2.A[k][j])%1000007;
			}
		}	
    }
    return tmp3;
}
struct node quick2_pow(ll k)
{
     
	node ans = x;
	//cout<
	while(k)
	{
       
		if(k&1) ans=Mul(ans,x);
		x = Mul(x,x);
	    k>>=1;
	}
  return ans;
}

int main()
{
        
	set();
	cin>>n;
	if(n<4) 
	{
     
		printf("0\n");
		return 0;
	}
	node s;
	s = Mul(quick2_pow(n-4),y);

	printf("%lld\n",s.A[0][0]%1000007);
} 

转载

你可能感兴趣的:(算法提高 翔集合)