The nod 1628 non-bourbon tree

Source: Internet
Author: User

Original title Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1628

It took a morning + half an afternoon to finally cut off the problem ...

God problem (film title)

We consider the general formula of the Fibonacci sequence:

$$ F (i) =[(\frac{1+\sqrt{5}}{2}) ^{i}-(\frac{1-\sqrt{5}}{2}) ^{i}]*\frac{1}{\sqrt{5}} $$

The last $ \frac{1}{\sqrt{5}} $ we can take it out and throw it back in the answer.

Then consider separating the previous two items separately.

Set $g (i) = (\frac{1+\sqrt{5}}{2}) ^{i}$ then it can be found that $G (a+b) =g (a) *g (b) $ with this nature, we can count things and multiply them.

Specifically, pre-$2^{i}$ from a step up, the point on the chain from the bottom to go up can get the contribution, from the top down the same, and the chain within the point 22 of the contribution of the answer (in fact, the previous statistics from the top down only for this part of the service).

For each group of inquiries, the first to find the two-point LCA, and then the two points to the LCA chain of the answer, and then merge together, the merger process has many details, see the code.

Lucky #

#include <cstdio>#include<algorithm>#defineMN 100001using namespacestd;intRead_p,read_ca;inlineintRead () {read_p=0; read_ca=GetChar ();  while(read_ca<'0'|| Read_ca>'9') read_ca=GetChar ();  while(read_ca>='0'&&read_ca<='9') read_p=read_p*Ten+read_ca- -, read_ca=GetChar (); returnread_p;}Const ints1=691504013, s2=308495997, mod=1e9+9, s3=276601605;structna{intY,ne;} b[mn<<1];intn,m,x,y,f[mn][ +],g[mn][ +],g[mn][ +],a[mn][ +],k[mn][ +],l[mn],num=0, s[2][mn],x[mn],y[mn],z[mn],s[mn],de[mn],mmh[mn],lx[mn],ly[mn],ap[mn];inlinevoid inch(intXintY) {b[++num].y=y;b[num].ne=l[x];l[x]=num;} InlineintMintx) { while(X>=mod) X-=mod; while(x<0) X+=mod;returnx;}voidDFS (intx) {S[x]=1; Registerinti;  for(i=1; i<= -; i++)if(f[f[x][i-1]][i-1]) f[x][i]=f[f[x][i-1]][i-1];Else  Break;  for(i=1; i<= -; i++)if(f[k[x][i-1]][i]) k[x][i]=f[k[x][i-1]][i];Else  Break;  for(; i;i--) k[x][i]=k[x][i-1]; k[x][0]=x;  for(i=l[x];i;i=b[i].ne)if(b[i].y!=f[x][0]) {DE[B[I].Y]=de[k[b[i].y][0]=f[b[i].y][0]=x]+1;        DFS (B[I].Y); AP[X]=m (ap[x]+1ll*s[x]*s[b[i].y]%MOD); S[X]+=S[B[I].Y]; }}inlineintLcaintXintYint&a,int&b) {     for(RegisterintI= -; i>=0; i--)    if(De[f[x][i]]>=de[y]) x=F[x][i]; if(x==y)returnx;  for(RegisterintI= -; i>=0; i--)    if(F[x][i]!=f[y][i]) x=f[x][i],y=F[y][i]; A=x;b=y; returnf[x][0];}voidDfsintXinto) {a[x][0]=1ll*ap[x]*s[o][1]%MOD; g[x][0]=g[x][0]=1ll*s[x]*s[o][1]%MOD;  for(RegisterintI=1; i<= -; i++)    if(K[x][i]) g[x][i]=m ((1ll* (g[x][i-1]-s[k[x][i-1]]) *s[o][1<< (I-1)]+g[f[x][i-1]][i-1])%MOD), G[x][i]=m ((1ll* (g[f[x][i-1]][i-1]-g[k[x][i-1]][0]) *s[o][1<< (I-1)]+g[x][i-1])%MOD), A[x][i]=m ((1ll*g[x][i-1]* (g[f[x][i-1]][i-1]-g[k[x][i-1]][0]) +a[x][i-1]+a[f[x][i-1]][i-1]-1ll*s[k[x][i-1]]*m (g[f[x][i-1]][i-1]-g[k[x][i-1]][0]))%MOD);  for(RegisterintI=l[x];i;i=b[i].ne)if(b[i].y!=f[x][0]) DFS (b[i].y,o);} InlinevoidWorkintXintZint&_a,int&_g,into) {    intp=0; _a=0; _g=0;  for(RegisterintI= -; i>=0; i--)    if(de[k[x][i]]>=De[z]) {_a=m ((1ll*_g* (g[x][i]-g[p][0]) +_a+a[x][i]-1ll*s[p]*m (g[x][i]-g[p][0]))%MOD); _g=m ((1ll* (_g-s[p]) *s[o][1<<i]+g[x][i])%MOD); if(k[x][i]==z)return; P=K[x][i]; X=F[x][i]; }}inlinevoidCalcintx) {    intAx,ay,gx,gy; Registerinti; DFS (1, x);  for(i=1; i<=m;i++)    if(y[i]==Z[i])        {work (x[i],z[i],ax,gx,x); Mmh[i]=m (mmh[i]+ (X?-1LL:1LL) * (1ll*gx* (N-s[z[i]]) +ax)%MOD); }Else{work (x[i],lx[i],ax,gx,x), work (Y[I],LY[I],AY,GY,X); Mmh[i]=m (mmh[i]+ (X?-1LL:1LL) * (1ll* (gx+gy) * (N-s[lx[i]]-s[ly[i]])%mod*s[x][1]+1ll*gx*gy%mod*s[x][1]+ax+ay+a[z[i]][0]-(1ll*s[lx[i]]* (S[z[i]]-s[lx[i]) +1ll*s[ly[i]]* (S[z[i]]-s[lx[i]]-s[ly[i]))%mod*s[x][1]+ (1ll* (S[z[i]]-s[lx[i]]-s[ly[i]) * (N-s[z[i]])%mod*s[x][1])%MOD); }}intMain () {registerinti; N=read ();  for(i=1; i<n;i++) X=read (), Y=read (),inch(x, Y),inch(Y,X); s[0][0]=s[1][0]=1;  for(i=1; i<=n;i++) s[0][i]=1ll*s[0][i-1]*s1%mod,s[1][i]=1ll*s[1][i-1]*s2%MOD; de[1]=1;D FS (1); M=read ();  for(i=1; i<=m;i++) {X[i]=read (); y[i]=read (); if(de[x[i]]<De[y[i]) swap (x[i],y[i]); Z[i]=LCA (X[i],y[i],lx[i],ly[i]); } Calc (0); Calc (1);  for(i=1; i<=m;i++) printf ("%d\n", 1ll*mmh[i]*s3%MOD);}
View Code

The nod 1628 non-bourbon tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.