zoukankan      html  css  js  c++  java
  • NOIP模拟赛 数列

    Problem 2 数列(seq.cpp/c/pas)

    【题目描述】

    a[1]=a[2]=a[3]=1

    a[x]=a[x-3]+a[x-1]  (x>3)

    求a数列的第n项对1000000007(10^9+7)取余的值。

    【输入格式】

    第一行一个整数T,表示询问个数。

    以下T行,每行一个正整数n。

    【输出格式】

    每行输出一个非负整数表示答案。

    【样例输入】

    3

    6

    8

    10

    【样例输出】

    4

    9

    19

    【数据范围】

    对于30%的数据 n<=100;

    对于60%的数据 n<=2*10^7;

    对于100%的数据 T<=100,n<=2*10^9;

    看了一个小时的矩阵快速幂。。。

    难点主要在于推转移矩阵

    感谢wust_wenhao的资料,非常详细

    矩阵快速幂:

     1 #define REP(i,j,k) for(int i=j;i<=k;i++)
     2 #include<iostream>
     3 using namespace std;
     4 
     5 const int mod=1000000007;
     6 
     7 struct matrix
     8 {
     9     int d[3][3];
    10 };
    11 matrix I,mat,zero;    
    12 int T,n;
    13 
    14 void Init()
    15 {
    16     memset(I.d,0,sizeof(I.d));memset(mat.d,0,sizeof(mat.d));memset(zero.d,0,sizeof(zero.d));
    17     I.d[0][0]=I.d[1][1]=I.d[2][2]=1;
    18     mat.d[0][2]=mat.d[1][0]=mat.d[2][1]=mat.d[2][2]=1;
    19 }
    20 
    21 matrix Mult(matrix x,matrix y)
    22 {
    23     matrix ans=zero;
    24     REP(i,0,2) REP(j,0,2) REP(k,0,2)
    25         ans.d[i][j]=(ans.d[i][j]+x.d[i][k]*y.d[k][j])%mod;
    26     return ans;
    27 }
    28 
    29 int modexp(int x)
    30 {
    31     matrix ret=I,tmp=mat;
    32     while(x)
    33     {
    34         if(x&1) ret=Mult(ret,tmp);
    35         tmp=Mult(tmp,tmp);
    36         x>>=1;
    37     }
    38     return (ret.d[2][0]+ret.d[2][1]+ret.d[2][2])%mod;
    39 }
    40 
    41 int solve(int x)
    42 {
    43     if(x<=3) return 1;
    44     return modexp(x-3);
    45 }
    46 
    47 int main()
    48 {
    49     Init();
    50     cin>>T;
    51     REP(i,1,T)
    52     {
    53         cin>>n;
    54         cout<<solve(n)<<endl;
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    Git 几个常用操作
    Ubuntu16.04安装YouCompleteMe
    常用命令总结
    启动Kernel提示Bad Data CRC
    linux4.15.1编译init/mounts报错
    编译Linux-4.15.1内核时遇到:“error : openssl/bio.h :No such file or folder”
    添加mtdparts引起的问题
    arm-linux-ld:u-boot.lds:1: ignoring invalid character `#' in expression
    smartgit的安装
    ubuntu下安装wine
  • 原文地址:https://www.cnblogs.com/InWILL/p/5986042.html
Copyright © 2011-2022 走看看