Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Phanindhar Bodla
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cctype>
- #include <algorithm>
- #include <map>
- #include <vector>
- #include <list>
- #include <set>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <string>
- #include <cmath>
- using namespace std;
- #define FOR(i,a,b) for(int i=a;i<b;i++)
- #define FORD(i,a,b) for(int i=a;i>=b;i--)
- #define REP(i,n) FOR(i,0,n)
- #define mod 1000000007
- #define MAXN 1000010
- typedef pair<int,int> PI;
- typedef vector<int> VI;
- typedef long long int LL;
- //copy both the functions
- //dimension of matrix: 3
- // matrix_mul(a,b,c): multiplies matrix a with b and puts the result in c
- // matrix_expo(a,4,c): calculates a^4 and puts in c
- void matrix_mul(LL a[3][3],LL b[3][3],LL c[3][3])
- {
- REP(i,3)
- REP(j,3)
- {
- c[i][j]=0;
- REP(k,3)
- {
- LL temp=a[i][k]*b[k][j];
- if(temp>=mod)
- temp%=mod;
- c[i][j]+=temp;
- if(c[i][j]>=mod)
- c[i][j]%=mod;
- }
- }
- }
- LL id[3][3]={{1,0,0},{0,1,0},{0,0,1}};
- void matrix_expo(LL a[3][3], LL n,LL h[3][3])
- {
- if(n==1)
- {
- REP(i,3)
- REP(j,3)
- h[i][j]=a[i][j];
- return;
- }
- else if(n==0)
- {
- REP(i,3)
- REP(j,3)
- h[i][j]=id[i][j];
- return;
- }
- LL c[3][3];
- LL x[3][3];
- matrix_expo(a,n/2,x);
- matrix_mul(x,x,c);
- if(n%2==1)
- {
- matrix_mul(a, c, h);
- return;
- }
- else
- {
- REP(i,3)
- REP(j,3)
- h[i][j]=c[i][j];
- return;
- }
- }
- int main(){
- int testCases,values,c1,c2,c3,t0,t1,t2;//c = coefficent t = term of sequence
- scanf("%d",&testCases);
- while(testCases--)
- {
- scanf("%d",&c1);
- scanf("%d",&c2);
- scanf("%d",&c3);
- scanf("%d",&t0);
- scanf("%d",&t1);
- scanf("%d",&t2);
- LL a[3][3]={{c1,c2,c3},{1,0,0},{0,1,0}};
- LL b[3][3];
- LL n;
- scanf("%d",&values);
- while(values--)
- {
- scanf("%lld",&n);
- if(n<3)
- {
- if(n==1)
- printf("%d\n",t1);
- else if(n==2)
- printf("%d\n",t2);
- else
- printf("%d\n",t0);
- }
- else
- {
- matrix_expo(a,n-2,b);//Seq(n)=Pow(A,n-2)*Seq(2)
- LL ans = t2*b[0][0] + t1*b[0][1] + t0*b[0][2];
- if(ans>=mod)
- ans%=mod;
- while(ans<0)
- ans+=mod;
- printf("%lld\n",ans);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement