• ZOJ 3772 Calculate the Function 线段树+矩阵


    Calculate the Function
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
    Submit Status
    Appoint description: 

    Description

    You are given a list of numbers A1A2 .. AN and M queries. For the i-th query:

    • The query has two parameters Li and Ri.
    • The query will define a function Fi(x) on the domain [Li, Ri] ∈ Z.
    • Fi(Li) = ALi
    • Fi(Li + 1) = A(Li + 1)
    • for all x >= Li + 2Fi(x) = Fi(x - 1) + Fi(x - 2) × Ax

    You task is to calculate Fi(Ri) for each query. Because the answer can be very large, you should output the remainder of the answer divided by 1000000007.

    Input

    There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:

    The first line contains two integers NM (1 <= NM <= 100000). The second line contains N integers A1A2 .. AN (1 <= Ai <= 1000000000).

    The next M lines, each line is a query with two integer parameters LiRi (1 <= Li <= Ri <= N).

    Output

    For each test case, output the remainder of the answer divided by 1000000007.

    Sample Input

    1
    4 7
    1 2 3 4
    1 1
    1 2
    1 3
    1 4
    2 4
    3 4
    4 4
    

    Sample Output

    1
    2
    5
    13
    11
    4
    4
    

    题目大意:给一个n的序列,若干查询(L,R)。输出F(R)的值。

    函数关系:

            F(L)=A(L)

            F(L+1)=A(L+1)

            F(X)=F(X-1)+F(X-2)*A(X) (X-L>=2)

    因此每段(L,R)区间               

    | F(R) |   | 1 A(R)|*| 1  A(R-1)| *......* | 1  A(L+2)|*| A(L+1)|

    |F(R-1)| = | 1   0 | | 1    0   |  ......  | 1    0   | | A(L)  |

    每次查询(L+2,R)区间的矩阵乘积再稍微处理一下就行了(当R-L>1时)。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 #define Mod 1000000007
     7 typedef long long LL;
     8 const int maxn=100010;
     9 LL a[maxn];
    10 
    11 struct node
    12 {
    13     LL mat[2][2];
    14     void set(int x)//初始化矩阵
    15     {
    16         mat[0][0]=1;
    17         mat[0][1]=a[x]%Mod;
    18         mat[1][0]=1;
    19         mat[1][1]=0;
    20         
    21     }
    22 };
    23 
    24 struct IntervalTree
    25 {
    26     int left,right;
    27     node matrix;
    28 }f[maxn<<2];
    29 
    30 
    31 node mat_mul_mod(node A,node B)//矩阵乘法取模
    32 {
    33     node ret;
    34     ret.mat[0][0]=(A.mat[0][0]*B.mat[0][0]%Mod+A.mat[0][1]*B.mat[1][0]%Mod)%Mod;
    35     ret.mat[0][1]=(A.mat[0][0]*B.mat[0][1]%Mod+A.mat[0][1]*B.mat[1][1]%Mod)%Mod;
    36     ret.mat[1][0]=(A.mat[1][0]*B.mat[0][0]%Mod+A.mat[1][1]*B.mat[1][0]%Mod)%Mod;
    37     ret.mat[1][1]=(A.mat[1][0]*B.mat[0][1]%Mod+A.mat[1][1]*B.mat[1][1]%Mod)%Mod;
    38     return ret;
    39 }
    40 
    41 void bulid(int left,int right,int i)//建树
    42 {
    43     int mid;
    44     f[i].left=left;
    45     f[i].right=right;
    46     if(left==right)
    47     {
    48         f[i].matrix.set(left);
    49         return;
    50     }
    51     mid=(left+right)>>1;
    52     bulid(left,mid,i<<1);
    53     bulid(mid+1,right,i<<1|1);
    54     f[i].matrix=mat_mul_mod(f[i<<1|1].matrix,f[i<<1].matrix);
    55     return ;
    56 }
    57 
    58 node query(int left,int right,int i)//查询
    59 {
    60     int mid;
    61     if(f[i].left==left && f[i].right==right) return f[i].matrix;
    62     mid=(f[i].left+f[i].right)>>1;
    63     if(right<=mid) return query(left,right,i<<1);
    64     else if(left>mid) return query(left,right,i<<1|1);
    65     else return mat_mul_mod(query(mid+1,right,i<<1|1),query(left,mid,i<<1));
    66 }
    67 
    68 int main()
    69 {
    70     int t,n,m,i,lp,rp;
    71     scanf("%d",&t);
    72     while(t--)
    73     {
    74         scanf("%d %d",&n,&m);
    75         for(i=1;i<=n;i++) 
    76             scanf("%lld",&a[i]);
    77         bulid(1,n,1);
    78         while(m--)
    79         {
    80             scanf("%d %d",&lp,&rp);
    81             if(lp==rp || lp+1==rp)
    82             {
    83                 printf("%lld
    ",a[rp]%Mod);
    84                 continue;
    85             }
    86             node temp=query(lp+2,rp,1);
    87             printf("%lld
    ",(temp.mat[0][0]*a[lp+1]%Mod+temp.mat[0][1]*a[lp]%Mod)%Mod);
    88         }
    89     }
    90     return 0;
    91 }

                         

  • 相关阅读:
    SQL计算生日所属的星座 XuZhao
    IIS 操作系统 .net FrameWork 的一些常见问题
    Python面向对象4.实例属性、实例方法
    Python面向对象6.init方法
    Python面向对象3.定义类、创建对象
    源码部署zabbix4.0监控
    IE6 Select控件挡住DIV内容解决办法
    转自网上的c#日期大全
    已更新或者删除的行值不能使该行成为唯一行(sql2005) 解决办法
    asp 多行多列加分页代码
  • 原文地址:https://www.cnblogs.com/xiong-/p/3752965.html
Copyright © 2020-2023  润新知