0%

C.Recursive sequence

C. Recursive sequence

题目描述:

输入 $ N, a, b $ ,输出 $ F_N \mod{2147493647} $

数据范围:
$ 1 \le N, a, b \le 2^{31} $

题解:

类斐波那契函数,还是使用迭代矩阵加快速幂的方法,注意一下怎么构造迭代矩阵。

按照惯例,先写成统一的形式

显然需要构造出类似下方的矩阵

需要思考的是 $ n^4 $ 怎么由 $ (n - 1) ^ 4 $ 得到,由与 $ (n - 1) $ 有关的线性组合得到一个 $ n^4 $ ,也就是把 $ n^4 $ 展开成 $ n - 1 $ 。 $ n^4 = (n - 1 + 1) ^ 4 $ ,注意可以使用杨辉三角得到系数

所以最右侧的矩阵应该为

同理也可以得到左边的矩阵,把里面的 $ n - 1 $ 换成 $ n $ 就行,所以最终的迭代关系为

可以发现,系数矩阵的下半部分刚好是杨辉三角

下面直接使用矩阵快速幂就行了

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using namespace std;
using namespace FAST_IO;
const ll mod = 2147493647;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-5;
const int maxn = 1e1 + 10;
const int maxm = 1e5 + 10;
ll t, n, m, k;

ll qpow(ll a, ll b, ll mod)
{
ll ans = 1, tmp = a;
while (b)
{
if (b & 1)
ans = ans * tmp % mod;
tmp = tmp * tmp % mod;
b >>= 1;
}
return ans % mod;
}
// 使用的时候注意一下矩阵乘法自带mod,矩阵的len,type等
struct Matrix
{
using type = ll;
static const int len = maxn;
type a[len][len];
int m, n; // 矩阵的行列
int dim; // 矩阵的维度
// 构造函数
Matrix(int n, int m)
{
this->m = m, this->n = n;
clean();
}
Matrix(int n)
{
this->m = this->n = n;
clean();
}
Matrix(int a[][len], int n, int m)
{
this->n = n, this->m = m;
copy(a, n, m);
}
Matrix(int a[][len], int n)
{
this->n = this->m = n;
copy(a, n, n);
}

void copy(int a[][len], int n, int m)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
this->a[i][j] = a[i][j];
}
}
}
void copy(int a[][len])
{
copy(a, n, m);
}
// type = 0, 行, 1 列
Matrix(int a[], int n, int m, int type)
{
this->n = n, this->m = m;
copy(a, n, m, type);
}
Matrix(int a[], int n, int type)
{
this->n = this->m = n;
copy(a, n, n, type);
}

void copy(int a[], int n, int m, int type)
{
int tmp[2] = {n, m};
for (int i = 1; i <= tmp[type]; i++)
{
if (type == 0)
this->a[1][i] = a[i];
else
this->a[i][1] = a[i];
}
}
void copy(int a[])
{
if (n == 1)
{
for (int i = 1; i <= m; i++)
{
this->a[1][i] = a[i];
}
}
else
{
for (int i = 1; i <= n; i++)
{
this->a[i][1] = a[i];
}
}
}
// 传入 ch 构造特殊矩阵,i 单位矩阵
Matrix(int n, char ch)
{
this->m = this->n = n;
if (ch == 'i' || ch == 'I')
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
a[i][j] = (i == j);
}
}
// 清空矩阵元素
void clean()
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
a[i][j] = 0;
}

// 基本运算符重载
type *operator[](const int x)
{
return a[x];
}

Matrix operator=(const Matrix &a)
{

for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
this->a[i][j] = a.a[i][j];
}
}
return *this;
}

Matrix operator%(const ll &mod) const
{
Matrix ret(n, m);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
ret.a[i][j] = a[i][j] % mod;
}
}
return ret;
}

Matrix operator%=(ll mod)
{
*this = *this % mod;
return *this;
}

// 矩阵乘法,带模除
Matrix operator*(const Matrix &a) const
{
Matrix ret(n, a.m);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= a.m; j++)
{
for (int k = 1; k <= m; k++)
{
ret.a[i][j] = (ret.a[i][j] + this->a[i][k] * a.a[k][j] % mod) % mod;
}
}
}
return ret;
}
Matrix operator*=(const Matrix &a)
{
*this = (*this) * a;
return *this;
}

// 矩阵快速幂,创建单位矩阵,需要和矩阵乘法一起使用
Matrix matrixqpow(ll k)
{
Matrix ans(n, 'i'), tmp = *this;
while (k)
{
if (k & 1)
ans = ans * tmp;
tmp = tmp * tmp;
k >>= 1;
}
return ans;
}
};
ll a, b;
int A_arr[maxn][maxn] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 1, 4, 6, 4, 1},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 4, 6, 4, 1},
{0, 0, 0, 0, 1, 3, 3, 1},
{0, 0, 0, 0, 0, 1, 2, 1},
{0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 1}};
int base_arr[maxn] = {0, 0, 0, 16, 8, 4, 2, 1};
int main()
{
// #define COMP_DATA
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int tcase;
read(tcase);
Matrix base(base_arr, 7, 1);
while (tcase--)
{
Matrix tmp(A_arr, 7);
Matrix ans(7, 7);
read(n, a, b);
base[1][1] = b;
base[2][1] = a;
if (n == 1)
ans.a[1][1] = a;
else if (n == 2)
ans.a[1][1] = b;
else
{
ans = tmp.matrixqpow(n - 2);
ans = ans * base;
}
printf("%lld\n", ans[1][1]);
}
return 0;
}