HDu 5950 Recursive sequence(矩阵快速幂)

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1323    Accepted Submission(s): 589

Problem Description

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 a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.

Input

The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.

Output

For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.

Sample Input

2
3 1 2
4 1 10

Sample Output

85
369

Hint

In the first case, the third number is 85 = 2*1十2十3^4.
In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

Source

2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

Recommend

jiangzijing2015

题解:

  转移方程 Fn = 2*Fn-2 + Fn-1 + n^4。如果没有这个n^4,这题就会很简单,所以我们就需要化简n^4。

  i^4 = C(0,4) (i-1)^4 + C(1,4)*(i-1)^3 + C(2,4)*(i-1)^2 + C(3,4)*(i-1)^1 + C(4,4)*(i-1)^0

  所以 n^4 = (n-1)^4 + 4*(n-1)^3 + 6*(n-1)^2 + 4*(i-1) + 1。

  Fn = 2*Fn-2 + Fn-1 + (n-1)^4 + 4*(n-1)^3 + 6*(n-1)^2 + 4*(i-1) + 1。

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 #define ms(a, b) memset(a, b, sizeof(a))
15 #define pb push_back
16 #define mp make_pair
17 const int INF = 0x7fffffff;
18 const int inf = 0x3f3f3f3f;
19 const LL mod = 2147493647;
20 const int maxn = 500+10;
21 struct Matrix
22 {
23     LL c[7][7];
24 };
25 Matrix base_Matrix =  {0,2,0,0,0,0,0,
26                        1,1,0,0,0,0,0,
27                        0,1,1,0,0,0,0,
28                        0,4,4,1,0,0,0,
29                        0,6,6,3,1,0,0,
30                        0,4,4,3,2,1,0,
31                        0,1,1,1,1,1,1};
32 Matrix mult(Matrix a, Matrix b)
33 {
34     Matrix hh = {0};
35     for(int i = 0;i<7;i++)
36         for(int j = 0;j<7;j++)
37             for(int k = 0;k<7;k++){
38                 hh.c[i][j] += (a.c[i][k]*b.c[k][j])%mod;
39                 hh.c[i][j] %= mod;
40             }
41     return hh;
42 }
43 Matrix qpow_Matrix(Matrix a, LL b)
44 {
45     Matrix base = a;
46     Matrix ans;
47     for(int i = 0;i<7;i++)
48         for(int j = 0;j<7;j++)
49             if(i==j)    ans.c[i][j] = 1;
50             else ans.c[i][j] = 0;
51
52     while(b){
53         if(b&1) ans = mult(ans, base);
54         base = mult(base, base);
55         b>>=1;
56     }
57     return ans;
58 }
59 void init() {
60
61 }
62 void solve() {
63     LL n, a, b;
64     cin >> n >> a >> b;
65     Matrix begin ={a,b,16,8,4,2,1};
66     if(n==1){
67         cout << a << endl;
68         return;
69     }
70     if(n==2){
71         cout << b << endl;
72         return;
73     }
74     Matrix tmp = qpow_Matrix(base_Matrix, n-2);
75     Matrix ans = mult(begin, tmp);
76 //    for(int i = 0;i<7;i++){
77 //        for(int j = 0;j<7;j++)
78 //            cout << ans.c[i][j] << " ";
79 //        cout << endl;
80 //    }
81 //    cout << endl;
82     cout << ans.c[0][1] << endl;
83 }
84 int main() {
85 #ifdef LOCAL
86     freopen("input.txt", "r", stdin);
87 //        freopen("output.txt", "w", stdout);
88 #endif
89     ios::sync_with_stdio(0);
90     cin.tie(0);
91     int T;
92     cin >> T;
93     while(T--){
94         init();
95         solve();
96     }
97     return 0;
98 }

  具体的矩阵构造会出一篇blog详细讲解

时间: 2024-10-03 14:03:05

HDu 5950 Recursive sequence(矩阵快速幂)的相关文章

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

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

Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但是N最大能到int的最大值, 直接循环推解不了 所以就得用矩阵快速幂咯 现在就看转移矩阵长什么样了 Mi表示要求的矩阵 转移矩阵用A表示 A * Mi = Mi+1 矩阵Mi里面至少得有 F[i-1] F[i-2] i ^ 4 Mi+1就相应的有 F[i] F[i-1] (i+1)^4 (i+1)^4 =

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

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$ 可以表示

HDU 2604 Queuing (矩阵快速幂)

HDU 2604 Queuing (矩阵快速幂) ACM 题目地址:HDU 2604 Queuing 题意: n个人排队,f表示女,m表示男,包含子串'fmf'和'fff'的序列为O队列,否则为E队列,有多少个序列为E队列. 分析: 矩阵快速幂入门题. 下面引用巨巨解释: 用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1): 如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff

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

HDU 2254 奥运(矩阵快速幂+二分等比序列求和)

HDU 2254 奥运(矩阵快速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意: 中问题不解释. 分析: 根据floyd的算法,矩阵的k次方表示这个矩阵走了k步. 所以k天后就算矩阵的k次方. 这样就变成:初始矩阵的^[t1,t2]这个区间内的v[v1][v2]的和. 所以就是二分等比序列求和上场的时候了. 跟HDU 1588 Gauss Fibonacci的算法一样. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * B

HDU 2604 Queuing,矩阵快速幂

题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出:   f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] + f[i-3] + f[i-4] */ #include<cstdio> #include<cstring> using namespace std; const int N = 4; int L, M; struct mtx { int x[N+1][N+1]; mtx(){ mem