zoukankan      html  css  js  c++  java
  • HDU 5950 Recursive sequence(矩阵快速幂)

    题目链接:Recursive sequence

    题意:给出前两项和递推式,求第n项的值。

    题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$

    主要问题是$i^4$处理,容易想到用矩阵快速幂,那么$i^4$就需要从$(i-1)$转移过来。

    $ i^4 = (i-1)^4 + 4*(i-1)^3 + 6*(i-1)^2 + 4*(i-1) + 1$

    $f_i$ $f_{i-1}$ $i^4$ $i^3$ $i^2$ $i$ $1$ = $f_{i-1}$ $f_{i-2}$ $(i-1)^4$ $(i-1)^3$ $(i-1)^2$ $(i-1)$ $1$ *

    $egin{pmatrix}
    1 & 1 & 0 & 0 & 0 & 0 & 0 \
    2 & 0 & 0 & 0 & 0 & 0 & 0 \
    1 & 0 & 1 & 0 & 0 & 0 & 0 \
    4 & 0 & 4 & 1 & 0 & 0 & 0 \
    6 & 0 & 6 & 3 & 1 & 0 & 0 \
    4 & 0 & 4 & 3 & 2 & 1 & 0 \
    1 & 0 & 1 & 1 & 1 & 1 & 1 \
    end{pmatrix}$

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #define N 7
     6 using namespace std;
     7 
     8 typedef long long ll;
     9 const ll mod=2147493647;
    10 
    11 struct mat
    12 {
    13     ll m[N][N]=
    14     {
    15      {1,1,0,0,0,0,0},
    16      {2,0,0,0,0,0,0},
    17      {1,0,1,0,0,0,0},
    18      {4,0,4,1,0,0,0},
    19      {6,0,6,3,1,0,0},
    20      {4,0,4,3,2,1,0},
    21      {1,0,1,1,1,1,1}
    22     };
    23 };
    24 
    25 mat mul(mat a,mat b)
    26 {
    27     mat ans;
    28     int i,j,k;
    29     for(i=0;i<N;i++)
    30     for(j=0;j<N;j++)
    31     ans.m[i][j]=0;
    32 
    33     for(i=0;i<N;i++)
    34     for(j=0;j<N;j++)
    35     for(k=0;k<N;k++)
    36     ans.m[i][j]=(ans.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
    37     return ans;
    38 }
    39 
    40 ll matpow(int p,ll A,ll B)
    41 {
    42     mat ans,tmp;
    43     int i,j;
    44     for(int i=0;i<N;i++)
    45     for(int j=0;j<N;j++)
    46     ans.m[i][j]=0;
    47     p-=2;
    48     ans.m[0][0]=B;ans.m[0][1]=A;
    49     ans.m[0][2]=16;ans.m[0][3]=8;ans.m[0][4]=4;ans.m[0][5]=2;ans.m[0][6]=1;
    50     while(p)
    51     {
    52         if(p&1) ans=mul(ans,tmp);
    53         tmp=mul(tmp,tmp);
    54         p=p>>1;
    55     }
    56     return ans.m[0][0];
    57 }
    58 
    59 int main(){
    60     int t;
    61     scanf("%d",&t);
    62     while(t--){
    63         ll M,A,B;
    64         scanf("%lld%lld%lld",&M,&A,&B);
    65         printf("%lld
    ",matpow(M,A,B)%mod);
    66     }
    67     return 0;
    68 }
    View Code

     

  • 相关阅读:
    PIP 全局换源
    专利检索常用的十八个网站
    【PHP】PHPMailer 中文使用说明小结
    去你的,奋斗逼,别把加班文化带到微软来
    Maven、spring Boot 与 Spring Cloud
    vue指令总结
    c++多个源文件共用一个new动态分配类对象(extern 及new的用法)
    git版本控制软件
    Axios 禁用缓存
    怎么写,简历才不会被丢到非洲
  • 原文地址:https://www.cnblogs.com/pavtlly/p/9807970.html
Copyright © 2011-2022 走看看