【CodeForces 719E】【线段树+矩阵快速幂】 Sasha and Array

本文解析了SashaandArray问题,介绍了使用线段树和矩阵快速幂解决区间操作及斐波那契数列求和的方法。通过实例展示了算法的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:E. Sasha and Array

描述:

E. Sasha and Array
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:

  1. 1 l r x — increase all integers on the segment from l to r by values x;
  2. 2 l r — find , where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo109 + 7.

In this problem we define Fibonacci numbers as follows: f(1) = 1f(2) = 1f(x) = f(x - 1) + f(x - 2) for all x > 2.

Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 0001 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Then follow m lines with queries descriptions. Each of them contains integers tpiliri and may be xi (1 ≤ tpi ≤ 21 ≤ li ≤ ri ≤ n,1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type.

It's guaranteed that the input will contains at least one query of the second type.

Output

For each query of the second type print the answer modulo 109 + 7.

Examples
input
5 4
1 1 2 1 1
2 1 5
1 2 4 2
2 2 4
2 1 5
output
5
7
9
Note

Initially, array a is equal to 11211.

The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5.

After the query 1 2 4 2 array a is equal to 13431.

The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7.

The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9.

题意:
给出有n个元素的数列ai(1<=i<=n)以及m次操作,操作分为两种:①将区间[l,r]的数加x;②询问∑f(ai)(l<=i<=r),其中f(x)是斐波那契数列的第x个数
题解:
斐波那契数列可以由2*2矩阵A={1 1,1 0}相乘得到,因此可以想到用线段树存储矩阵,每次更新加x就可以用乘以A^x代替

这题写的有点气,初始化没弄对,调半天_(:зゝ∠)_
代码:
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  " ;
#define pl(x) cout << #x << "= " << x << endl;
#define lson l, m, rt<<1  
#define rson m+1, r, rt<<1|1 
#define ll __int64
using  namespace  std;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
const ll M=1e9+7;
const int maxn=1e5+10;

struct Matrix{
    ll v[2][2];
};

void init(Matrix& ad){
  ad.v[0][0]=1; ad.v[0][1]=1;
  ad.v[1][0]=1; ad.v[1][1]=0;
}

Matrix mtAdd(Matrix A, Matrix B){        // 求矩阵 A + B
  int i, j;
  Matrix C;
  for(i = 0; i < 2; i ++)
    for(j = 0; j < 2; j ++){
      C.v[i][j] = (A.v[i][j] + B.v[i][j]) % M;
    }
  return C;
}

Matrix mtMul(Matrix A, Matrix B){        // 求矩阵 A * B
  int i, j, k;
  Matrix C;
  for(i = 0; i < 2; i ++)
    for(j = 0; j < 2; j ++){
      C.v[i][j] = 0;
      for(k = 0; k < 2; k ++){
        C.v[i][j] = (A.v[i][k] * B.v[k][j] + C.v[i][j]) % M;
      }
    }
  return C;
}

Matrix mtPow(Matrix A, int k){           // 求矩阵 A ^ k
  if(k == 0) {
    memset(A.v, 0, sizeof(A.v));
    for(int i = 0; i < 2; i ++){
      A.v[i][i] = 1;
    }
    return A;
  }
  if(k == 1) return A;
  Matrix C = mtPow(A, k / 2);
  if(k % 2 == 0) {
    return mtMul(C, C);
  }
  else {
    return mtMul(mtMul(C, C), A);
  }
}

Matrix sum[maxn<<2],add[maxn<<2],A;

void PushUp(int rt){
  sum[rt]=mtAdd(sum[rt<<1], sum[rt<<1|1]);
}

void PushDown(int rt){
  add[rt<<1]=mtMul(add[rt<<1], add[rt]);
  add[rt<<1|1]=mtMul(add[rt<<1|1], add[rt]);
  sum[rt<<1]=mtMul(sum[rt<<1], add[rt]);
  sum[rt<<1|1]=mtMul(sum[rt<<1|1], add[rt]);
  add[rt]=mtPow(A, 0);
}

void build(int l, int r,int rt){
  add[rt]=mtPow(A, 0);//初始化为单位矩阵
  sum[rt]=mtPow(A, 0); 
  if(l==r){
    int x;read(x);
    sum[rt]=mtPow(A, x-1); 
    return;
  } 
  int m=(l+r)>>1;
  build(lson);  
  build(rson);  
  PushUp(rt); 
}

void update(int L,int R,Matrix c,int l,int r,int rt){
  if(L<=l && R>=r){  
    sum[rt]=mtMul(sum[rt], c);  
    add[rt]=mtMul(add[rt], c); 
    return;  
  } 
  PushDown(rt);
  int m=(l+r)>>1;
  if(L<=m)update(L, R, c, lson);
  if(R>m)update(L, R, c, rson);
  PushUp(rt);
}

ll query(int L,int R,int l, int r,int rt){
  if(L<=l && R>=r){
    return sum[rt].v[0][0];
  }
  PushDown(rt);
  int m=(l+r)>>1;
  ll ret=0;
  if(L<=m)ret=(ret+query(L, R, lson))%M;
  if(R>m)ret=(ret+query(L, R, rson))%M;
  PushUp(rt);
  return ret;
}

//debug用
void print(int l,int r,int rt){  
  if(l==r){  
    for(int i=0;i<2;i++){  
      for(int j=0;j<2;j++)  
        printf("%I64d ",sum[rt].v[i][j]);  
      printf("\n");  
    }  
    printf("\n");  
    return;  
  }  
  int m=(l+r)>>1;  
  print(lson);  
  print(rson);  
}  

int  main(){
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif

  int n,m;
  init(A);
  read(n);read(m); 
  build(1, n, 1);
  int op,a,b;
  while(m--){
    read(op);read(a);read(b);
    if(op==1){
      int x;read(x);
      update(a, b, mtPow(A, x), 1, n, 1);
    }
    else{
      printf("%I64d\n", query(a, b, 1, n, 1));
    }
  }
  return 0;
}



### Codeforces 平台上的快速幂算法题目及相关实现 #### 快速幂算法简介 快速幂是一种高效的计算 $a^b \mod c$ 的算法,其时间复杂度为 $O(\log b)$。通过将指数分解成二进制形式并利用平方倍增的方式减少乘法次数来提高效率。 --- #### 题目解析与实现方法 以下是两道来自 Codeforces 平台上涉及快速幂的经典题目及其解决方案: --- ##### **题目一:Codeforces 1594B** 这是一道典型的快速幂应用题,目标是找到第 $k$ 个以 $n$ 为底的幂底数累加的结果[^1]。 ###### 解决方案 给定整数 $n$ 和 $k$,我们需要按照 $k$ 的二进制表示逐位判断哪些位置对应的幂需要被加入最终结果中。具体实现如下: ```cpp #include <iostream> #define ll long long using namespace std; const int MOD = 1e9 + 7; int main() { int t; cin >> t; while (t--) { ll n, k; cin >> n >> k; ll ans = 0, s = 1; // 初始化答案和当前幂值 while (k != 0) { if (k & 1) { // 如果当前位为1,则加上对应幂值 ans = (ans + s) % MOD; } s = s * n % MOD; // 更新幂值 k >>= 1; // 右移一位 } cout << ans << endl; } return 0; } ``` 此代码的核心在于使用 `while` 循环逐步处理 $k$ 的每一位,并根据是否为 $1$ 来决定是否将其对应的幂值加入到总和中。 --- ##### **题目二:CodeForces - 894B Ralph And His Magic Field** 本题同样涉及到快速幂的应用,但更侧重于组合数学中的计数问题[^2]。 ###### 解决方案 对于输入的三个参数 $n$, $m$, 和 $k$,我们可以通过快速幂函数高效地求解 $(2^{(n-1)})^{(m-1)} \mod INF$ 的值。如果 $k=-1$ 且 $(n+m)\%2\neq0$,则直接返回 $0$;否则继续执行快速幂逻辑。 ```cpp #include <iostream> using namespace std; const long long INF = 1e9 + 7; long long fast_pow(long long base, long long exp) { long long result = 1; base %= INF; while (exp > 0) { if (exp & 1) { result = (result * base) % INF; } base = (base * base) % INF; exp >>= 1; } return result; } int main() { long long n, m, k; cin >> n >> m >> k; if (k == -1 && (n + m) % 2 != 0) { cout << "0" << endl; return 0; } cout << fast_pow(fast_pow(2, n - 1), m - 1) << endl; return 0; } ``` 这段程序定义了一个通用的快速幂辅助函数 `fast_pow`,用于简化主流程中的幂运算部分[^2]。 --- #### 总结 以上两个例子展示了如何在不同场景下灵活运用快速幂技术解决问题。无论是简单的累加还是复杂的嵌套幂运算,都可以借助这一技巧显著提升性能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值