• 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
     
    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 }
    View Code
      具体的矩阵构造会出一篇blog详细讲解
  • 相关阅读:
    接口测试01
    mysql主从
    linux下配置JDK
    linux常用命令
    mysql基本语句
    线程与进程
    loadrunner函数
    设计模式-模板方法模式
    设计原则-CRP合成复用原则
    设计原则-LOD迪米特法则
  • 原文地址:https://www.cnblogs.com/denghaiquan/p/7215455.html
Copyright © 2020-2023  润新知