• YbtOJ#732斐波那契【特征方程,LCT】


    正题

    题目链接:http://www.ybtoj.com.cn/contest/125/problem/2


    题目大意

    给出\(n\)个点的一棵树,以\(1\)为根,每个点有点权\(a_i\)。要求支持\(m\)次操作

    1. 修改一个修改一个节点的父节点
    2. 修改一条路径的权值为\(w\)
    3. 给出\(u\)询问\(Fbi(a_u)\)
    4. 给出\(u,v\),将路径\(u->v\)的点权排列好后设为\(b\)

    \[\sum_{i=1}^k\sum_{j=i}^kFbi(\sum_{z=i}^jb_z) \]

    其中\(Fbi(i)\)表示第\(i\)个斐波那契数。输出答案模\(998244353\)的值

    \(1\leq n,m\leq 10^5,a_i,w\in[1,10^9]\)


    解题思路

    嗯这个斐波那契很麻烦,可以考虑一些用特征方程\(1-x-x^2=0\),可以得到斐波那契的通项公式

    \[Fbi(n)=\frac{(\frac{\sqrt 5+1}{2})^n-(\frac{\sqrt 5-1}{2})^n}{\sqrt 5} \]

    为了方便上面\(\frac{\sqrt 5\pm 1}{2}\)分别记为\(X_0,X_1\)
    那么如果设\(c_i=X_0^{a_i},d_i=X_1^{a_i}\)的话我们要求的就是

    \[\frac{\sum_{i=1}^k\sum_{j=i}^k\prod_{z=i}^jc_z-\sum_{i=1}^k\sum_{j=i}^k\prod_{z=i}^jd_z}{\sqrt 5} \]

    这个好像看起来好维护一点,不过首先我们要解决这个\(\sqrt 5\)的问题,因为其实\(\sqrt 5\)在模\(998244353\)意义下是没有值的,我们不能直接用二次剩余带入数字。

    考虑维护一个类似于多项式的东西,每个数字记为二元组\((a,b)=a\sqrt 5+b\)。加减乘都很好搞,除法的话需要推导一下,

    \[\frac{1}{a\sqrt 5+b}=c\sqrt 5+d \]

    \[5ac+\sqrt 5(ad+cb)+bd=1 \]

    \[5ac+bd=1,ad+cb=0 \]

    解出来

    \[c=-\frac{a}{b^2-5a^2},d=\frac{b}{b^2-5a^2} \]

    这样四则运算都搞定了,可以开始考虑如何在\(LCT\)上面维护了。

    类似线段树的,设\(pro\)表示所有数乘积,\(pre/suf\)表示所有前/后缀乘积和,\(ans\)表示我们维护的答案,那么就可以合并两个东西了。\(LCT\)维护的时候顺便把单个的节点也合并进去就好了。

    然后还剩下一个最麻烦的东西就是树链修改的时候我们需要快速算出连续\(x\)\(u\)的信息。
    \(pro\)很好搞就是\(u^x\)\(suf\)\(pre\)就是一个简单的等比数列求和,上通项公式就好了。
    \(ans\)比较麻烦,考虑每个\(u^i\)的个数答案就是

    \[\sum_{i=1}^xu^i(x-i+1)=(x+1)\sum_{i=1}^xu^i-\sum_{i=1}^xu^ii \]

    \[\Rightarrow (x+1)\frac{u^{x+1}-u}{u-1}-\frac{xu^{x+1}-\frac{u^x-u}{u-1}}{u-1} \]

    这样就可以在\(log\)时间复杂度以内合并了。

    然后答案\(0\)次项一定是\(0\)的,所以输出\(\sqrt 5\)的项就好了。
    时间复杂度\(O(n\log^2 n)\)


    code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<stack>
    #define ll long long
    using namespace std;
    const ll P=998244353,N=1e5+10;
    struct node{
    	ll a,b;//a带√5 
    	node(ll aa=0,ll bb=0)
    	{a=aa;b=bb;return;}
    };
    ll power(ll x,ll b=P-2){
    	ll ans=1;x%=P;
    	while(b){
    		if(b&1)ans=ans*x%P;
    		x=x*x%P;b>>=1;
    	}
    	return ans;
    }
    const node X((P+1)/2,(P+1)/2);
    node operator+(node x,node y)
    {return node((x.a+y.a)%P,(x.b+y.b)%P);}
    node operator-(node x,node y)
    {return node((x.a-y.a)%P,(x.b-y.b)%P);}
    node operator*(node x,node y)
    {return node((x.a*y.b+x.b*y.a)%P,(x.b*y.b+5*x.a*y.a)%P);}
    node inv(node x){
    	ll tmp=power(x.b*x.b-5*x.a*x.a);
    	return node(-x.a,x.b)*node(0,tmp);
    }
    node power(node x,ll b){
    	node ans(0,1);
    	while(b){
    		if(b&1)ans=ans*x;
    		x=x*x;b>>=1;
    	}
    	return ans;
    }
    struct Tnode{
    	node ans,pre,suf,pro;
    };
    Tnode operator+(Tnode x,Tnode y){
    	Tnode w;
    	w.ans=x.ans+y.ans+x.suf*y.pre;
    	w.pre=x.pre+y.pre*x.pro;
    	w.suf=y.suf+x.suf*y.pro;
    	w.pro=x.pro*y.pro;return w;
    }
    struct SegTree{
    	ll fa[N],t[N][2],siz[N];
    	Tnode w[N];node v[N],lazy[N];
    	bool r[N],hlz[N];stack<ll> s;
    	bool Nroot(ll x)
    	{return fa[x]&&(t[fa[x]][0]==x||t[fa[x]][1]==x);}
    	bool Direct(ll x)
    	{return t[fa[x]][1]==x;}
    	void Rev(ll x)
    	{swap(t[x][0],t[x][1]);swap(w[x].pre,w[x].suf);r[x]^=1;return;}
    	void PushUp(ll x){
    		siz[x]=siz[t[x][0]]+siz[t[x][1]]+1;
    		w[x]=(Tnode){v[x],v[x],v[x],v[x]};
    		if(t[x][0])w[x]=w[t[x][0]]+w[x];
    		if(t[x][1])w[x]=w[x]+w[t[x][1]];
    		return;
    	}
    	void Updata(ll x,node u){
    		ll s=siz[x];lazy[x]=v[x]=u;
    		node tmp=inv(node(0,1)-u);
    		hlz[x]=1; w[x].pro=power(u,s);
    		w[x].pre=w[x].suf=(u-w[x].pro*u)*tmp;
    		w[x].ans=(node(0,s)-w[x].pre)*u*tmp;
    		return;
    	}
    	void PushDown(ll x){
    		if(hlz[x]){
    			if(t[x][0])Updata(t[x][0],lazy[x]);
    			if(t[x][1])Updata(t[x][1],lazy[x]);
    			hlz[x]=0;
    		}
    		if(!r[x])return;
    		Rev(t[x][0]);Rev(t[x][1]);
    		r[x]=0;return;
    	}
    	void Rotate(ll x){
    		ll y=fa[x],z=fa[y];
    		ll xs=Direct(x),ys=Direct(y);
    		ll w=t[x][xs^1];
    		if(Nroot(y))t[z][ys]=x;
    		t[x][xs^1]=y;t[y][xs]=w;
    		if(w)fa[w]=y;fa[y]=x;fa[x]=z;
    		PushUp(y);PushUp(x);return;
    	}
    	void Splay(ll x){
    		ll y=x;s.push(x);
    		while(Nroot(y))y=fa[y],s.push(y);
    		while(!s.empty())PushDown(s.top()),s.pop();
    		while(Nroot(x)){
    			ll y=fa[x];
    			if(!Nroot(y))Rotate(x);
    			else if(Direct(x)==Direct(y))
    				Rotate(y),Rotate(x);
    			else Rotate(x),Rotate(x);
    		}
    		return;
    	}
    	void Access(ll x){
    		for(ll y=0;x;y=x,x=fa[x])
    			Splay(x),t[x][1]=y,PushUp(x);
    		return;
    	}
    	void MakeRoot(ll x)
    	{Access(x);Splay(x);Rev(x);return;}
    	void Link(ll x,ll y){
    		MakeRoot(1);Access(x);Splay(x);
    		fa[t[x][0]]=0;t[x][0]=0;PushUp(x);
    		fa[x]=y;return;
    	}
    	ll Split(ll x,ll y){
    		MakeRoot(x);Access(y);Splay(y);
    		return (w[y].ans.a+P)%P*2%P;
    	}
    	void Change(ll x,ll y,node val){
    		MakeRoot(x);Access(y);Splay(y);
    		Updata(y,val);return;
    	}
    }T;
    ll n,m;
    signed main()
    {
    //	freopen("fibonacci.in","r",stdin);
    //	freopen("fibonacci.out","w",stdout);
    	scanf("%lld%lld",&n,&m);
    	for(ll i=1;i<=n;i++){
    		ll x;scanf("%lld",&x);
    		T.v[i]=power(X,x);
    		T.PushUp(i);
    	}
    	for(ll i=2;i<=n;i++)
    		scanf("%lld",&T.fa[i]);
    	while(m--){
    		ll op,u,v,w;
    		scanf("%lld",&op);
    		if(op==1){
    			scanf("%lld%lld",&u,&v);
    			T.Link(u,v);
    		}
    		else if(op==2){
    			scanf("%lld%lld%lld",&u,&v,&w);
    			T.Change(u,v,power(X,w));
    		}
    		else if(op==3){
    			scanf("%lld",&u);
    			printf("%lld\n",T.Split(u,u));
    		}
    		else if(op==4){
    			scanf("%lld%lld",&u,&v);
    			printf("%lld\n",T.Split(u,v));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    在IE中下载Office2007文件时在对话框中下载文件变成ZIP文件的问题
    异常:tomcat与windows时间不同步
    centos 解决"不在 sudoers 文件中。此事将被报告"的问题
    linux device drivers
    linux device drivers
    linux device drivers
    Source Insight 空格和Tab出现乱码
    光和颜色
    RTOS Thread stack and MSP/PSP registers in ARM Cortex-M3
    Keil MDK编译出现Error: L6405E
  • 原文地址:https://www.cnblogs.com/QuantAsk/p/14453801.html
Copyright © 2020-2023  润新知