"LibreOJ NOI Round # 2" alone

topic

This question I can not find success by multiplying the moment

The answer is a continued fraction, it does not look like a general way to do data structure, setting \ (x_ {l, r} , y_ {l, r} \) represent \ ([l, r] \ ) Inquiry numerator and denominator

Then there

\[\frac{x_{l,r}}{y_{l,r}}=a_{l}+\frac{y_{l+1,r}}{x_{l+1,r}}=\frac{y_{l+1,r}+a_l\times x_{l+1,r}}{x_{l+1,r}}\]

Kichiku so things should be written in the form of a matrix

then

\[ \begin{bmatrix} a_l& 1\\ 1&0 \end{bmatrix}\times \begin{bmatrix} x_{l+1,r}\\ y_{l+1,r} \end{bmatrix}=\begin{bmatrix} y_{l+1,r}+a_l\times x_{l+1,r}\\ x_{l+1,r} \end{bmatrix}= \begin{bmatrix} x_{l,r}\\ y_{l,r} \end{bmatrix} \]

This is a \ (2 \ times 2 \) is a matrix multiplication \ (2 \ times 1 \) vector, it must be a matrix vector multiply to goIn fact, I had been put in front of vector write

Thus for \ (l, r \) a set of challenge we require is of such \ (\ begin {bmatrix} a_r \\ 1 \ end {bmatrix} \) such a vector, multiplying \ (r-1 \) to \ (L \) of these matrices, and must be from \ (r-1 \) by the \ (L \) , since a complete matrix by a vector is obtained, the vector or on the back, so the foregoing associativity It is from the \ (L \) by the \ (r-1 \)

It is

\[ \begin{bmatrix} a_l& 1\\ 1&0 \end{bmatrix}\times \begin{bmatrix} a_{l+1}& 1\\ 1&0 \end{bmatrix}\times \begin{bmatrix} a_{l+2}& 1\\ 1&0 \end{bmatrix}\times ...\times \begin{bmatrix} a_{r-1}& 1\\ 1&0 \end{bmatrix}\times \begin{bmatrix} a_r\\ 1 \end{bmatrix} \]

So we need to query product within a range of matrices, data range \ (10 ^ 6 \) , it seems not easy to use with \ (\ log \) things to maintain, so consider forced inversion

\ (2 \ times 2 \) inverse matrix hands push and it'll launch it, that is, \ (\ the begin {bmatrix} 0 & 1 \\ 1 & -a_ {L} \ End {bmatrix} \) , so we have to maintain a prefix inverse matrix product can quickly query a range of

But prefix inverse matrix of the product requires maintenance from \ (i \) is multiplied to \ (1 \) product, the prefix matrix product requires maintenance from \ (1 \) to \ (i \) product, the query time is that prefix inverse matrix product multiplied by the matrix product prefix, so the front in order to eliminate twenty-two

Code

#include<bits/stdc++.h>
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read() {
    char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int maxn=1e6+5;
const int mod=998244353;
struct mat{int a[2][2];};
inline int qm(int x) {return x>=mod?x-mod:x;}
inline mat operator*(mat a,mat b) {
    mat c;
    c.a[0][0]=qm(1ll*a.a[0][0]*b.a[0][0]%mod+1ll*a.a[0][1]*b.a[1][0]%mod);
        c.a[0][1]=qm(1ll*a.a[0][0]*b.a[0][1]%mod+1ll*a.a[0][1]*b.a[1][1]%mod);
        c.a[1][0]=qm(1ll*a.a[1][0]*b.a[0][0]%mod+1ll*a.a[1][1]*b.a[1][0]%mod);
        c.a[1][1]=qm(1ll*a.a[1][0]*b.a[0][1]%mod+1ll*a.a[1][1]*b.a[1][1]%mod);
        return c;
} 
inline void out(mat c) {
    printf("%d %d\n",c.a[0][0],c.a[0][1]);
    printf("%d %d\n",c.a[1][0],c.a[1][1]);
    puts("");
}
mat pre[maxn],inv[maxn];
int n,m,type,a[maxn];
int main() {
    n=read(),m=read(),type=read();
    pre[0].a[0][0]=pre[0].a[1][1]=inv[0].a[0][0]=inv[0].a[1][1]=1;
    for(re int i=1;i<=n;i++) pre[i].a[0][1]=pre[i].a[1][0]=1;
    for(re int i=1;i<=n;i++) inv[i].a[0][1]=inv[i].a[1][0]=1;
    for(re int i=1;i<=n;i++) pre[i].a[0][0]=a[i]=read(),inv[i].a[1][1]=qm(mod-a[i]);
    for(re int i=1;i<=n;i++) pre[i]=pre[i-1]*pre[i],inv[i]=inv[i]*inv[i-1];
    int lstx=0,lsty=0,op,x,l,r;
    while(m--) {
        op=read();
        if(op==1) {
            x=read();
            if(type) x^=(lstx^lsty);a[++n]=x;
            pre[n].a[0][1]=pre[n].a[1][0]=inv[n].a[0][1]=inv[n].a[1][0]=1;
            pre[n].a[0][0]=x,inv[n].a[1][1]=qm(mod-x);
            pre[n]=pre[n-1]*pre[n],inv[n]=inv[n]*inv[n-1];
        }
        if(op==2) {
            l=read(),r=read();
            if(type) l^=(lstx^lsty),r^=(lstx^lsty);
            if(l==r) lstx=a[l],lsty=1;
            else {
                mat c=inv[l-1]*pre[r-1];
                lstx=qm(1ll*a[r]*c.a[0][0]%mod+c.a[0][1]);
                lsty=qm(1ll*a[r]*c.a[1][0]%mod+c.a[1][1]);
            }
            printf("%d %d\n",lstx,lsty);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/asuldb/p/11542312.html