hdu_5950_Recursive sequence(矩阵快速幂)

题目链接:hdu_5950_Recursive sequence

题意:递推求解:F(n) = 2*F(n-2) + F(n-1) + n和F(1) = a,F(2) = b;

题解:

一看数据范围,肯定矩阵加速递推,不过公式不是线性的,需要把公式转换为线性的公式

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=a;i<=b;i++)
 3 using namespace std;
 4 typedef long long ll;
 5
 6 const int mat_N=7;
 7 ll mo=2147493647;
 8
 9 struct mat{
10     ll c[mat_N][mat_N];
11     void init(){memset(c,0,sizeof(c));}
12     mat operator*(mat b){
13         mat M;int N=mat_N-1;M.init();
14         F(i,0,N)F(j,0,N)F(k,0,N)M.c[i][j]=(M.c[i][j]+c[i][k]*b.c[k][j])%mo;
15         return M;
16     }
17     mat operator^(ll k){
18         mat ans,M=(*this);int N=mat_N-1;ans.init();
19         F(i,0,N)ans.c[i][i]=1;
20         while(k){if(k&1)ans=ans*M;k>>=1,M=M*M;}
21         return ans;
22     }
23 }A,B,C;
24
25 int t,n,a,b;
26
27 int main()
28 {
29     scanf("%d",&t);
30     while(t--)
31     {
32         scanf("%d%d%d",&n,&a,&b);
33         A=(mat){0,1,0,0,0,0,0,
34                 2,1,1,4,6,4,1,
35                 0,0,1,4,6,4,1,
36                 0,0,0,1,3,3,1,
37                 0,0,0,0,1,2,1,
38                 0,0,0,0,0,1,1,
39                 0,0,0,0,0,0,1};
40         C=A^(n-2);
41         ll ans=0;
42         ans=(C.c[1][0]*a+C.c[1][1]*b+C.c[1][2]*16+C.c[1][3]*8+C.c[1][4]*4+C.c[1][5]*2+C.c[1][6])%mo;
43         printf("%lld\n",ans);
44     }
45     return 0;
46 }

时间: 2024-10-05 23:34:36

hdu_5950_Recursive sequence(矩阵快速幂)的相关文章

Uva10689 Yet another Number Sequence ( 矩阵快速幂 )

Uva 10689Yet another Number Sequence(  矩阵快速幂  ) 题意: 就是矩阵快速幂,没什么好说的. 分析: 其实还是斐波那契数列.只是最后对应的矩阵不是(1,1)是(a,b)了 MOD = 1; for( int i = 0; i < m; ++i ) MOD *= 10; 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace s

HDU5950-Recursive sequence(矩阵快速幂)

题目链接:Recursive sequence 题意:给出n头母牛,第一头报a,第二头报b,第i头报f[i-2]*2+f[i-1]+i^4,问第n头母牛报数多少 分析:N,a,b<2^31,果断矩阵快速幂,关键是要推出公式,公式如下,仅作参考 1 0 0 0 0 0 0        1               1 1 1 0 0 0 0 0        i                i+1 1 2 1 0 0 0 0       i2              (i+1)2 1 3

UVA - 10689 Yet another Number Sequence 矩阵快速幂

                  Yet another Number Sequence Let’s de?ne another number sequence, given by the following function:f(0) = af(1) = bf(n) = f(n − 1) + f(n − 2), n > 1When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values

hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Holion August will eat every thing he has found. Now there are many foods,but he does not want to eat all of them at once,so he fi

HDU 1005 Number Sequence 矩阵快速幂

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 236241    Accepted Submission(s): 60070 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

HDU 5950 Recursive sequence 矩阵快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=5950 一开始以为i^4不能矩阵快速幂,但是结论是可以得,那么要怎么递推呢? 矩阵快速幂的思路都是一样的,matrix_a * matrix_b ^ n 其中,想要维护什么,就在matrix_a写,比如现在是F[n - 1], F[n - 2],我想要递推到下一项,那么就 会变成F[n], F[n - 1],这个时候,你就要寻找一下F[n]和F[n - 1]有什么关系. i^4也一样,想要从i^4 递推到 (i

5950 Recursive sequence (矩阵快速幂)

题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无法进行运算的.好像有的思路,最后也没想出来,还是参考的大牛的博客 http://blog.csdn.net/spring371327/article/details/52973534 那是讲的很详细了,就不多说了,注意这个取模不是1e9+7,一开始忘了.. 代码如下: #pragma comment

HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers

CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&amp;&amp;bsgs

题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ \prod_{j=1}^k f_{i-j}^{b_j} \ mod \ p, \ \ \ \ \ i > k\end{matrix}\right.$$ 求 $f_k$($1 \leq f_k < p$),使得 $f_m = n$.($1 \leq k\leq 100$) 分析 $f_n$ 可以表示