跳到正文

个人记录

7.20信友队学习总结

Flax_shiep约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月21日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 6 分钟

版本

  1. v1当前

2025/7/20

知识点:
  1.矩阵
2.数论,拉格朗日插值…掉线了

A题(原CF 718C 洛谷CF718C):

因为一些细节,重构了好几次代码,调了一整天
导致一天就只写了一题,直接当题解看就行

题意:

代码
有一个序列a
有两种操作,第一种是区间修改  
第二种是求区间里所有的 f(a[i]) 的和
f(x)是斐波那契数列的第x项
f(1)=1,f(2)=2  
f(x)=f(x-1)+f(x-2)(x>=3)

  矩阵快速幂板子题
动态开点线段树维护矩阵,再用矩阵快速幂求斐波那契数列

矩阵快速幂可以log n求斐波那契数列的第 n 项

代码
设        
0 1         1 1
1 1         0 0
为矩阵A    为矩阵B

设A*B^n为矩阵C
则C[0][1]为斐波那契数列的第n项  

事实上,矩阵快速幂应该可以log n求任何线性函数
查询和修改的操作和普通线段树一样,只是维护对象变成矩阵,可以手写一个数据类型

AC代码

C++
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define int long long
const int maxn=2e5+200;
const int mod=1e9+7;
#define mid (l+r>>1)
#define ls (pos<<1)
#define rs (pos<<1|1)
int n,m,a[maxn];
struct Node{
	int m[4][4];
	void clear(){
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				m[i][j]=0;
			}
		}
	}
	void init(){
		for(int i=0;i<4;i++)m[i][i]=1;
	}
	void prt(){
		for(int i=1;i<=2;i++){
			for(int j=1;j<=2;j++)cout<<m[i][j];
		}
	}
	bool check(){
		if(!m[1][1])return 0;
		if(m[1][2])return 0;
		if(m[2][1])return 0;
		if(!m[2][2])return 0;
		return 1;
	}
	Node operator +(const Node &x)const{
		Node res;
		res.clear();
		for(int i=1;i<=2;i++){
			for(int j=1;j<=2;j++){
				res.m[i][j]=(m[i][j]+x.m[i][j])%mod;
			}
		}
		return res;
	}
	Node operator *(const Node &x)const{
		Node res;
		res.clear();
		for(int i=1;i<=2;i++){
			for(int j=1;j<=2;j++){
				for(int k=1;k<=2;k++)res.m[i][j]=(res.m[i][j]+m[i][k]*x.m[k][j])%mod;
			}
		}
		return res;
	}
}val[maxn<<2];
Node ad[maxn<<2];
Node d,fir;
Node qpow(Node a,int b){
	Node res;
	res.clear();
	res.init();
	while(b){
		if(b&1){
			res=res*a;
		}
		a=a*a;
		b>>=1;
	}
	return res;
}
void pushdown(int pos,int l,int r){
	if(ad[pos].check())return;
	val[ls]=val[ls]*ad[pos];
	val[rs]=val[rs]*ad[pos];
	ad[ls]=ad[ls]*ad[pos];
	ad[rs]=ad[rs]*ad[pos];
	ad[pos].clear();
	ad[pos].init();
}
void build(int pos,int l,int r){
	ad[pos].clear();
	val[pos].clear();
	ad[pos].init();
	if(l==r){
		val[pos]=fir*qpow(d,a[l]-1);
		return;
	}
	build(ls,l,mid);
	build(rs,mid+1,r);
	val[pos]=val[ls]+val[rs];
}
void add(int pos,int l,int r,int L,int R,Node z){	if(L<=l&&R>=r){
		val[pos]=val[pos]*z;
		ad[pos]=ad[pos]*z;
		return;
	}
	pushdown(pos,l,r);
	if(L<=mid)add(ls,l,mid,L,R,z);
	if(R>mid)add(rs,mid+1,r,L,R,z);
	val[pos]=val[ls]+val[rs];
}
Node find(int pos,int l,int r,int L,int R){
	if(L<=l&&R>=r)return val[pos];
	pushdown(pos,l,r);
	Node res;
	res.clear();
	if(L<=mid)res=res+find(ls,l,mid,L,R);
	if(R>mid)res=res+find(rs,mid+1,r,L,R);
	return res;
}
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48); 
		ch=getchar();
	}
	return x*f;
}
signed main(){
	d.clear();
	fir.clear();
	d.m[1][1]=d.m[1][2]=d.m[2][1]=1;
	fir.m[1][1]=fir.m[1][2]=1;
	n=read();
	m=read();
	for(int i=1;i<=n;i++)a[i]=read();
	build(1,1,n);
	while(m--){
		int tp,l,r,z;
		tp=read();
		l=read();
		r=read();
		if(tp==1){
			z=read();
			add(1,1,n,l,r,qpow(d,z));
		}
		else{
			cout<<find(1,1,n,l,r).m[1][2]%mod<<'\n';
		}
	}
	return 0;
}

好像写成题解了

原文