Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- using namespace std;
- typedef long long ll;
- const int Maxn = 10;
- const int Maxc = 36;
- const int mod = 1000000007;
- const int Maxb = 31;
- struct matrix {
- int x[Maxn][Maxn];
- matrix(int diag = 0) {
- for (int i = 0; i < Maxn; i++)
- for (int j = 0; j < Maxn; j++)
- x[i][j] = (i == j) * diag;
- }
- matrix operator *(const matrix &b) const {
- matrix c;
- for (int i = 0; i < Maxn; i++)
- for (int j = 0; j < Maxn; j++)
- for (int l = 0; l < Maxn; l++)
- c.x[i][j] = (c.x[i][j] + ll(x[i][l]) * b.x[l][j] % mod) % mod;
- return c;
- }
- };
- int fib[Maxc], trib[Maxc];
- int val[Maxn];
- matrix pw[Maxb];
- int t;
- int a[Maxn], x;
- int res;
- int friendlyFibonacciFunction(int x)
- {
- int a = fib[x + 25], p = trib[x + 20];
- int res = 1;
- while (p) {
- if (p & 1) res = ll(res) * a % mod;
- p >>= 1; a = ll(a) * a % mod;
- }
- return res;
- }
- int main()
- {
- fib[1] = trib[1] = fib[2] = trib[2] = 1;
- for (int i = 3; i < Maxc; i++) {
- fib[i] = fib[i - 2] + fib[i - 1];
- trib[i] = trib[i - 3] + trib[i - 2] + trib[i - 1];
- }
- for (int i = 0; i < Maxn; i++)
- val[i] = friendlyFibonacciFunction(i + 1);
- int sum = 0;
- for (int j = Maxn - 1; j >= 0; j--) {
- sum = (sum + val[j]) % mod;
- pw[0].x[0][j] = sum;
- }
- for (int i = 1; i < Maxn; i++)
- pw[0].x[i][i - 1] = 1;
- for (int i = 1; i < Maxb; i++)
- pw[i] = pw[i - 1] * pw[i - 1];
- scanf("%d", &t);
- for (int tc = 1; tc <= t; tc++) {
- for (int i = 0; i < Maxn; i++)
- scanf("%d", &a[i]);
- scanf("%d", &x);
- printf("Case #%d: ", tc);
- if (x < Maxn) printf("%d\n", a[x]);
- else {
- x -= Maxn - 1;
- matrix r(1);
- for (int i = 0; i < Maxb; i++)
- if (x & 1 << i) r = r * pw[i];
- res = 0;
- for (int j = 0; j < Maxn; j++)
- res = (res + ll(r.x[0][j]) * a[Maxn - 1 - j] % mod) % mod;
- printf("%d\n", res);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement