Guest User

SEQ SPOJ

a guest
Jun 23rd, 2016
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. /*
  2.    Mayank Pratap Singh
  3.    MNNIT, 2nd year IT
  4.    AC ho.
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. #define ff first
  9. #define ss second
  10. #define pb push_back
  11. #define mp make_pair
  12. #define MOD 1000000000
  13. typedef long long ll;
  14. typedef long double ld;
  15. const int INF=(int)(1e9);
  16. const ll INF64=(ll)(1e18);
  17. const ld EPS=1e-9;
  18. const ld PI=3.1415926535897932384626433832795;
  19. typedef vector<int> vi;
  20. typedef vector<pair<int,int> > vii;
  21. typedef vector<list<int> > vl;
  22. typedef vector<list<pair<int,int> > > vlp;
  23. typedef vector<list<pair<int,double> > >vld;
  24. typedef map<int,int> mi;
  25. typedef map<string,int> ms;
  26. typedef set<int> si;
  27.  
  28. vector<vector<ll> >identity; // Identity Matrix
  29. vector<vector<ll> > matMultiply(vector<vector<ll> >mat1,int p,int q,vector<vector<ll> >mat2,int r){
  30.  
  31.    vector<vector<ll> >mat3;
  32.    vector<ll>row;
  33.  
  34.    /* Preparing answer matrix */
  35.  
  36.    for(int i=0;i<p;++i){
  37.      row.clear();
  38.      for(int j=0;j<r;++j){
  39.         row.pb(0);
  40.      }
  41.      mat3.pb(row);
  42.  
  43.    }
  44.  
  45.    /* Matrix Multiplication Step */
  46.  
  47.    for(int i=0;i<p;++i){
  48.       for(int j=0;j<r;++j){
  49.          mat3[i][j]=0;
  50.          for(int k=0;k<q;++k){
  51.               mat3[i][j]=(mat3[i][j]%MOD+(mat1[i][k]%MOD*mat2[k][j]%MOD)%MOD)%MOD;
  52.          }
  53.       }
  54.    }
  55.  
  56.   return mat3;
  57.  
  58. }
  59.  
  60. vector<vector<ll> > matPower(vector<vector<ll> >mat1,int p,int q,int expo){
  61.  
  62.    if(expo==0)
  63.      return identity; // Identity Matrix
  64.  
  65.    vector<vector<ll> >F(p,vector<ll>(q));
  66.  
  67.    F=matPower(mat1,p,q,expo/2);
  68.    F=matMultiply(F,p,q,F,q);   // F*F
  69.  
  70.    if(expo%2==0)
  71.     return F;
  72.    else
  73.      return matMultiply(F,p,q,mat1,q);
  74.  
  75. }
  76.  
  77.  
  78.  
  79. int main(){
  80.  
  81.    int t;
  82.    cin>>t;
  83.  
  84.    while(t--){
  85.  
  86.      int k;
  87.      cin>>k;
  88.      ll b[11],c[11];
  89.      for(int i=0;i<k;++i)
  90.        cin>>b[i];
  91.  
  92.  
  93.      for(int i=0;i<k;++i)
  94.        cin>>c[i];
  95.  
  96.  
  97.      ll n;
  98.      cin>>n;
  99.  
  100.      if(n<=k)
  101.       cout<<b[n-1]%MOD<<endl;
  102.  
  103.      else{
  104.        
  105.        // Preparing identity matrix to be used in power function
  106.      
  107.        vector<ll>row;
  108.  
  109.        for(int i=0;i<k;++i){
  110.          row.clear();
  111.  
  112.          for(int j=0;j<k;++j){
  113.             if(j==i)
  114.               row.pb(1);
  115.             else
  116.               row.pb(0);
  117.          }
  118.          identity.pb(row);
  119.  
  120.        }
  121.  
  122.        // Preparing basic matrix of size k * 1
  123.  
  124.        vector<vector<ll> >F1;  
  125.  
  126.        for(int i=0;i<k;++i){
  127.            row.clear();
  128.            for(int j=0;j<1;++j){
  129.               row.pb(b[i]);
  130.            }
  131.            F1.pb(row);
  132.        }
  133.  
  134.        // Preparing tranformation matrix of size k*k
  135.  
  136.       // F2=T*F1,  Fn=T^(n-1)*F1  
  137.  
  138.        vector<vector<ll> >T;  // Transform matrix;
  139.  
  140.        for(int i=0;i<k;++i){
  141.          row.clear();
  142.          for(int j=0;j<k;++j){
  143.              row.pb(0);
  144.          }
  145.          T.pb(row);
  146.        }
  147.  
  148.        for(int i=0;i<k-1;++i)
  149.          T[i][i+1]=1;
  150.  
  151.  
  152.        for(int j=0;j<k;++j)
  153.          T[k-1][j]=c[k-j-1];
  154.  
  155.        vector<vector<ll> >ans(k,vector<ll>(1));
  156.        T=matPower(T,k,k,n-1);
  157.        ans=matMultiply(T,k,k,F1,1);
  158.        cout<<ans[0][0]%MOD<<endl;
  159.  
  160.  
  161.      }
  162.  
  163.  
  164.  
  165.    }
  166.  
  167.  
  168.    return 0;
  169. }
Add Comment
Please, Sign In to add comment