Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <cstdio>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. const int Maxn = 10;
  7. const int Maxc = 36;
  8. const int mod = 1000000007;
  9. const int Maxb = 31;
  10.  
  11. struct matrix {
  12.     int x[Maxn][Maxn];
  13.     matrix(int diag = 0) {
  14.         for (int i = 0; i < Maxn; i++)
  15.             for (int j = 0; j < Maxn; j++)
  16.                 x[i][j] = (i == j) * diag;
  17.     }
  18.     matrix operator *(const matrix &b) const {
  19.         matrix c;
  20.         for (int i = 0; i < Maxn; i++)
  21.             for (int j = 0; j < Maxn; j++)
  22.                 for (int l = 0; l < Maxn; l++)
  23.                     c.x[i][j] = (c.x[i][j] + ll(x[i][l]) * b.x[l][j] % mod) % mod;
  24.         return c;
  25.     }
  26. };
  27.  
  28. int fib[Maxc], trib[Maxc];
  29. int val[Maxn];
  30. matrix pw[Maxb];
  31. int t;
  32. int a[Maxn], x;
  33. int res;
  34.  
  35. int friendlyFibonacciFunction(int x)
  36. {
  37.     int a = fib[x + 25], p = trib[x + 20];
  38.     int res = 1;
  39.     while (p) {
  40.         if (p & 1) res = ll(res) * a % mod;
  41.         p >>= 1; a = ll(a) * a % mod;
  42.     }
  43.     return res;
  44. }
  45.  
  46. int main()
  47. {
  48.     fib[1] = trib[1] = fib[2] = trib[2] = 1;
  49.     for (int i = 3; i < Maxc; i++) {
  50.         fib[i] = fib[i - 2] + fib[i - 1];
  51.         trib[i] = trib[i - 3] + trib[i - 2] + trib[i - 1];
  52.     }
  53.     for (int i = 0; i < Maxn; i++)
  54.         val[i] = friendlyFibonacciFunction(i + 1);
  55.     int sum = 0;
  56.     for (int j = Maxn - 1; j >= 0; j--) {
  57.         sum = (sum + val[j]) % mod;
  58.         pw[0].x[0][j] = sum;
  59.     }
  60.     for (int i = 1; i < Maxn; i++)
  61.         pw[0].x[i][i - 1] = 1;
  62.     for (int i = 1; i < Maxb; i++)
  63.         pw[i] = pw[i - 1] * pw[i - 1];
  64.     scanf("%d", &t);
  65.     for (int tc = 1; tc <= t; tc++) {
  66.         for (int i = 0; i < Maxn; i++)
  67.             scanf("%d", &a[i]);
  68.         scanf("%d", &x);
  69.         printf("Case #%d: ", tc);
  70.         if (x < Maxn) printf("%d\n", a[x]);
  71.         else {
  72.             x -= Maxn - 1;
  73.             matrix r(1);
  74.             for (int i = 0; i < Maxb; i++)
  75.                 if (x & 1 << i) r = r * pw[i];
  76.             res = 0;
  77.             for (int j = 0; j < Maxn; j++)
  78.                 res = (res + ll(r.x[0][j]) * a[Maxn - 1 - j] % mod) % mod;
  79.             printf("%d\n", res);
  80.         }
  81.     }
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement