Advertisement
phanindhar1

Q4

Mar 24th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. // Phanindhar Bodla
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cctype>
  7. #include <algorithm>
  8. #include <map>
  9. #include <vector>
  10. #include <list>
  11. #include <set>
  12. #include <queue>
  13. #include <deque>
  14. #include <stack>
  15. #include <string>
  16. #include <cmath>
  17. using namespace std;
  18.  
  19. #define FOR(i,a,b) for(int i=a;i<b;i++)
  20. #define FORD(i,a,b) for(int i=a;i>=b;i--)
  21. #define REP(i,n) FOR(i,0,n)
  22. #define mod 1000000007
  23. #define MAXN 1000010
  24.  
  25. typedef pair<int,int>   PI;
  26. typedef vector<int> VI;
  27. typedef long long int LL;
  28.  
  29. //copy both the functions
  30. //dimension of matrix: 3
  31. // matrix_mul(a,b,c): multiplies matrix a with b and puts the result in c
  32. // matrix_expo(a,4,c): calculates a^4 and puts in c
  33.  
  34. void matrix_mul(LL a[3][3],LL b[3][3],LL c[3][3])
  35. {
  36.     REP(i,3)
  37.         REP(j,3)
  38.         {
  39.             c[i][j]=0;
  40.             REP(k,3)
  41.             {
  42.                 LL temp=a[i][k]*b[k][j];
  43.                 if(temp>=mod)
  44.                     temp%=mod;
  45.                 c[i][j]+=temp;
  46.                 if(c[i][j]>=mod)
  47.                     c[i][j]%=mod;
  48.             }
  49.         }
  50. }
  51.  
  52. LL id[3][3]={{1,0,0},{0,1,0},{0,0,1}};
  53.  
  54. void matrix_expo(LL a[3][3], LL n,LL h[3][3])
  55. {
  56.     if(n==1)
  57.     {
  58.         REP(i,3)
  59.             REP(j,3)
  60.                 h[i][j]=a[i][j];
  61.         return;
  62.     }
  63.     else if(n==0)
  64.     {
  65.         REP(i,3)
  66.             REP(j,3)
  67.                 h[i][j]=id[i][j];
  68.         return;
  69.     }
  70.    
  71.     LL c[3][3];
  72.     LL x[3][3];
  73.  
  74.     matrix_expo(a,n/2,x);
  75.     matrix_mul(x,x,c);
  76.  
  77.     if(n%2==1)
  78.     {
  79.         matrix_mul(a, c, h);
  80.         return;
  81.     }
  82.     else
  83.     {
  84.         REP(i,3)
  85.             REP(j,3)
  86.                 h[i][j]=c[i][j];
  87.         return;
  88.     }  
  89. }
  90.  
  91.  
  92. int main(){
  93.  
  94.     int testCases,values,c1,c2,c3,t0,t1,t2;//c = coefficent t = term of sequence
  95.     scanf("%d",&testCases);
  96.     while(testCases--)
  97.     {
  98.         scanf("%d",&c1);
  99.         scanf("%d",&c2);
  100.         scanf("%d",&c3);
  101.         scanf("%d",&t0);
  102.         scanf("%d",&t1);
  103.         scanf("%d",&t2);
  104.         LL a[3][3]={{c1,c2,c3},{1,0,0},{0,1,0}};
  105.         LL b[3][3];
  106.         LL n;
  107.         scanf("%d",&values);
  108.         while(values--)
  109.         {
  110.         scanf("%lld",&n);
  111.         if(n<3)
  112.         {
  113.             if(n==1)
  114.                 printf("%d\n",t1);
  115.             else if(n==2)
  116.                 printf("%d\n",t2);
  117.             else
  118.                 printf("%d\n",t0);
  119.  
  120.         }
  121.         else
  122.         {
  123.             matrix_expo(a,n-2,b);//Seq(n)=Pow(A,n-2)*Seq(2)
  124.             LL ans = t2*b[0][0] + t1*b[0][1] + t0*b[0][2];
  125.             if(ans>=mod)
  126.                 ans%=mod;
  127.             while(ans<0)
  128.                 ans+=mod;
  129.             printf("%lld\n",ans);
  130.         }
  131.         }
  132.  
  133.     }
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement