Advertisement
yicongli

HDU6395

Mar 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. #define gc c=getchar()
  8. #define r(x) read(x)
  9. #define ll long long
  10.  
  11. template<typename T>
  12. inline void read(T&x){
  13.     x=0;T k=1;char gc;
  14.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  15.     while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
  16. }
  17.  
  18. const int p=1e9+7;
  19.  
  20. struct Matrix{
  21.     int a[3][3];
  22.    
  23.     inline Matrix(){
  24.         memset(a,0,sizeof(a));
  25.     }
  26.    
  27.     inline int* operator [](const int &x){
  28.         return a[x];
  29.     }
  30.    
  31.     inline const int* operator [](const int &x)const {
  32.         return a[x];
  33.     }
  34.    
  35.     inline Matrix operator *(const Matrix &b)const {
  36.         Matrix ret;
  37.         for(int i=0;i<3;++i){
  38.             for(int j=0;j<3;++j){
  39.                 for(int k=0;k<3;++k){
  40.                     ret[i][j]=(ret[i][j]+(ll)a[i][k]*b[k][j])%p;
  41.                 }
  42.             }
  43.         }
  44.         return ret;
  45.     }
  46. }g;
  47.  
  48. inline Matrix qpow(Matrix a,int b){
  49.     Matrix ans;
  50.     for(int i=0;i<3;++i)ans[i][i]=1;
  51.     while(b){
  52.         if(b&1)ans=ans*a;
  53.         a=a*a;
  54.         b>>=1;
  55.     }
  56.     return ans;
  57. }
  58.  
  59. int A,B,C,D,E,n;
  60.  
  61. inline void init(int x){
  62.     g[0][0]=D;
  63.     g[0][1]=1;
  64.     g[0][2]=0;
  65.     g[1][0]=C;
  66.     g[1][1]=0;
  67.     g[1][2]=0;
  68.     g[2][0]=x;
  69.     g[2][1]=0;
  70.     g[2][2]=1;
  71. }
  72.  
  73. int main(){
  74. //  freopen(".in","r",stdin);
  75. //  freopen(".out","w",stdout);
  76.     int T;r(T);
  77.     while(T--){
  78.         r(A),r(B),r(C),r(D),r(E),r(n);
  79.         Matrix f;
  80.         f[0][0]=B;
  81.         f[0][1]=A;
  82.         f[0][2]=1;
  83.         if(E<=n){
  84.             for(int i=3,j;i<=E;i=j+1){
  85.                 j=E/(E/i);
  86.                 init(E/i);
  87.                 g=qpow(g,j-i+1);
  88.                 f=f*g;
  89.             }
  90.             init(0);
  91.             g=qpow(g,n-max(E,2));
  92.             f=f*g;
  93.         }
  94.         else {
  95.             for(int i=3,j;i<=n;i=j+1){
  96.                 j=E/(E/i);
  97.                 init(E/i);
  98.                 if(j<=n)g=qpow(g,j-i+1);
  99.                 else g=qpow(g,n-i+1);
  100.                 f=f*g;
  101.             }
  102.         }
  103.         printf("%d\n",f[0][0]);
  104.     }
  105.     return 0;
  106. }
  107. /*
  108. 2
  109. 3 3 2 1 3 5
  110. 3 2 2 2 1 4
  111.  
  112. 1
  113. 10 15 17 19 15234532 545
  114. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement