【Matrix Multiplication】【SSL 2514】Kindergarten Math Problems Ⅱ

【Matrix Multiplication】【SSL 2514】Kindergarten Math Problems Ⅱ

topic

On this day, when a confused LZH classmate was crying in the examination room, YMW on the side cut off the very simple first question just like cutting vegetables. Could it be that he, after all, the deputy head of the kindergarten and the famous malignant tumor Qiu Biao came out to prevent people from AK, but YMW, as a famous fan of ACrush, has always targeted AK and never give up. See you, the
title is sauce purple, f(n)-f(3)-f(4)-f(5)-...-f(n-3)-f(n-2)=(n+4)( n-1)/2, f(1)=1, f(2)=1
Find the sum of the first n terms of f(n)


enter

Enter a positive integer n (guarantee 0<=n<=2^31-1)


Output

Output a positive integer, which represents the number of whole points in this graph, and you need to calculate the remainder of 1000000007


Sample

input1
1

output1
1

input2
2

output 2
2


Problem-solving ideas

The title is given f[n]=f[3]+f[4]+…f[n-3]+f[n-2]+(n+4)(n-1)
then f[n +1]=f[3]+f[4]+…f[n+1-3]+f[n+1-2]+(n+1+4)(n+1-1)
can find f [n+1] is more than f[n] f[n-1]+n+2
f[n+1]=f[n]+f[n-1]+(n+1)+1
ta just Very familiar, just add a summation to
the previous program and it's OK


Code

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const long long mo=1000000007;
struct lzf{
    
    
	long long n,m,h[10][10];
}a,b,x;
long long n;
lzf operator *(lzf l,lzf y)
{
    
    
	x.n=l.n,x.m=y.m;
	memset(x.h,0,sizeof(x.h));
	for (int k=1;k<=l.m;k++)
	    for (int i=1;i<=x.n;i++)
	        for (int j=1;j<=x.m;j++)
	            x.h[i][j]=(x.h[i][j]+l.h[i][k]*y.h[k][j]%mo)%mo;
	return x;
}
void power(long long n)
{
    
     
     while (n)
     {
    
    
	       if (n & 1) a=a*b;
	       b=b*b;
	       n>>=1;
	 }
}
int main()
{
    
    
	a.n=1,a.m=5;
	a.h[1][1]=a.h[1][2]=a.h[1][4]=1;
	a.h[1][3]=3,a.h[1][5]=1;
	b.n=5,b.m=5;
	b.h[1][2]=b.h[2][1]=b.h[2][2]=b.h[3][2]=1;
	b.h[3][3]=b.h[4][2]=b.h[4][3]=b.h[4][4]=1;
	b.h[2][5]=b.h[5][5]=1;
	scanf("%lld",&n);
	power(n-1);
    printf("%lld",a.h[1][5]);	
} 

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/111407172