C. 加乐一眼题 [ Problem 4306 ]
Description
亨亨随手出了一个序列:an=2∗an−1+n∗n,a0=0;由于加乐在寒假期间已经讲过矩阵快速幂了,所以亨亨把这个题命名为加乐一眼题,然而亨亨总喜欢骗,偷袭他的学弟学妹们,所以亨亨送给你们一句话:不会吧不会吧,真的有人觉得这是矩阵快速幂吧!!!
Input
输入n,在long long 范围内
Output
输出第n项对1e9+9取余的结果
Samples
Input Copy
1
Output
1
Input Copy
2
Output
6
Input Copy
3
Output
21
快速幂模板题,直接附上构造矩阵的过程
上代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 9;
void mul(ll c[][5],ll a[][5],ll b[][5])
{
ll tmp[5][5] = { 0 };
for (int i = 0;i < 5;i++)
{
for (int j = 0;j < 5;j++)
{
for (int k = 0;k < 5;k++)
{
tmp[i][j] = (((tmp[i][j] % mod) + mod) % mod + (1ll * ((a[i][k] % mod) + mod) % mod * ((b[k][j] % mod) + mod) % mod) % mod) % mod;
}
}
}
memcpy(c, tmp, sizeof tmp);
}
int main()
{
ll n;
ll A[5][5] = {
{5,1,0,0,0},
{-9,0,1,0,0},
{7,0,0,1,0},
{-2,0,0,0,1},
{0,0,0,0,0}
};
ll F[5][5] = {
{141,58,21,6,1},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0} };
cin >> n;
if (n == 0) cout << 0 << endl;
else if (n>=1&&n <= 5)
{
cout << F[0][5 - n] << endl;
}
else {
n -= 5;
while (n)
{
if (n % 2 != 0) mul(F, F, A);
mul(A, A, A);
n /= 2;
}
cout << ((F[0][0]%mod)+mod)%mod << endl;
}
return 0;
}