Spoj 8059. Stocks prediction (matrix embedding matrix)

Source: Internet
Author: User

Question:

Returns a recursive relationship.

This recursive relationship can be used to obtain S_1 S_2 S_3... S_m

Then, tell a K and N

Obtain n terms of segma (S_k, S_2 * k, S_3 * k.


Train of Thought Analysis:

The recursive relationship is given first.

So we can use a matrix to deliver s [I]...

But what he wants is the value of every K.

Defines the recursive matrix of S as.

Sum = S_k + S_2 * k +... + s_n * k
Sum = S_k + A ^ K * S_k + A ^ 2 K * S_k...

Sum = (E + A ^ K + A ^ 2 * k + A ^ 3 * k... a ^ (n-1) * S_k ..

So we use the KK matrix to represent a ^ K

Then the problem is transformed

Calculate the sum of E + A ^ K + A ^ 2 * k + A ^ 3 * k.

Therefore, we need to define a matrix. Each element of this matrix is a small matrix.

Then, continue the recursive evaluation.

The recursive matrix is

E

0 a ^ K


#include <cstdio>#include <iostream>#include <cstring>#include <iostream>using namespace std;typedef long long LL;LL mod=1000000007;LL N;struct matrix//N*N{    LL data[10][10];    friend matrix operator * (const matrix A,const matrix B)    {        matrix res;        memset(res.data,0,sizeof res.data);        for(int i=0;i<N;i++)        for(int j=0;j<N;j++)        for(int k=0;k<N;k++)        {            res.data[i][j]+=(A.data[i][k]*B.data[k][j])%mod;            res.data[i][j]%=mod;        }        return res;    }    friend matrix operator + (const matrix A,const matrix B)    {        matrix res;        for(int i=0;i<N;i++)        for(int j=0;j<N;j++)        {            res.data[i][j]=(A.data[i][j]+B.data[i][j])%mod;            res.data[i][j]%=mod;        }        return res;    }    friend matrix operator - (const matrix A,const matrix B)    {        matrix res;        for(int i=0;i<N;i++)        for(int j=0;j<N;j++)        {            res.data[i][j]=((A.data[i][j]-B.data[i][j])+mod)%mod;            res.data[i][j]%=mod;        }        return res;    }    void print()    {        for(int i=0;i<N;i++)        {            for(int j=0;j<N;j++)            printf("%lld ",data[i][j]);            puts("");        }    }}E,zero;struct supermax{    matrix ret[10][10];    friend supermax operator * (supermax A,supermax B)    {        supermax res;        for(int i=0;i<2;i++)        for(int j=0;j<2;j++)        res.ret[i][j]=zero;        for(int i=0;i<2;i++)        for(int j=0;j<2;j++)        for(int k=0;k<2;k++)        {            res.ret[i][j]=res.ret[i][j]+(A.ret[i][k]*B.ret[k][j]);            for(int p=0;p<N;p++)            for(int q=0;q<N;q++)            res.ret[i][j].data[p][q]%=mod;        }        return res;    }};matrix matmod(matrix origin,LL n){    matrix res=E;    while(n)    {        if(n&1)        res=res*origin;        n>>=1;        origin=origin*origin;    }    return res;}supermax Do(supermax origin,LL n)//2*2{    supermax res;    for(int i=0;i<2;i++)    for(int j=0;j<2;j++)    res.ret[i][j]=zero;    for(int i=0;i<2;i++)    res.ret[i][i]=E;    while(n)    {        if(n&1)        res=res*origin;        n>>=1;        origin=origin*origin;    }    return res;}LL S[10];LL a_[10];int main(){    memset(zero.data,0,sizeof zero.data);    memset(E.data,0,sizeof E.data);    LL n,r,k;    int CASE;    scanf("%d",&CASE);    while(CASE--)    {        scanf("%lld%lld%lld",&n,&r,&k);        N=r;        for(int i=0;i<10;i++)        E.data[i][i]=1;        for(int i=1;i<=r;i++)        {            scanf("%lld",&S[i]);            S[i]%=mod;        }        for(int i=1;i<=r;i++)        {            scanf("%lld",&a_[i]);            a_[i]%=mod;        }        matrix init;        for(int i=0;i<r;i++)            init.data[i][0]=S[r-i];        matrix fib;        fib=zero;        LL fans=0;        LL fuck=1;        while(fuck*k<=r)        {            fans+=S[fuck*k];            fuck++;        }        LL b=fuck*k;        for(int i=0;i<r;i++)        {            fib.data[0][i]=a_[i+1];            fib.data[i+1][i]=1;        }        matrix st = matmod(fib,b-r);        st=st*init;        matrix K=matmod(fib,(LL)k);        supermax o;        o.ret[0][0]=E;        o.ret[0][1]=E;        o.ret[1][0]=zero;        o.ret[1][1]=K;        n=(n*k-b)/k+1;        supermax final=Do(o,n);        matrix tmp=(final.ret[0][0]*zero)+(final.ret[0][1]*E);        matrix B=E;        matrix ans = tmp*st;        printf("%lld\n",(fans+ans.data[0][0])%mod);    }    return 0;}/*510 5 71 2 3 4 51 2 3 4 510 5 75 4 3 2 15 4 3 2 110 5 7123 456 789 987 6546 78 9 7 610 5 734587 98237598 123134 523454 52432598 73897 54897 8978979 312425421 5 71 2 3 4 51 2 3 4 5510 7 1004890 78678 6876 54465 798798 567576 894123545 979123124 789475 32789 6786786 5675675 89789*/


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.