[HDOJ6395] Sequence (反演思想,整除分块,矩阵快速幂)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395

就算个式子。

这题关键在于如何处理后面的下取整分式,会发现如何构造这个矩阵都不太好弄这个式子。知道一点莫比乌斯反演,考虑到了按照整除结果对取整式进行分块,每一块单独跑块长次矩阵快速幂,然后累计到结果上就行。

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
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL maxn = 5;
const LL mod = 1e9+7;
typedef struct Matrix {
LL m[maxn][maxn];
LL r;
LL c;
Matrix(){
r = c = 0;
memset(m, 0, sizeof(m));
}
} Matrix;
Matrix mul(Matrix m1, Matrix m2) {
Matrix ret = Matrix();
ret.r = m1.r; ret.c = m2.c;
for(LL i = 1; i <= m1.r; i++) {
for(LL j = 1; j <= m2.r; j++) {
for(LL k = 1; k <= m2.c; k++) {
if(m2.m[j][k] == 0) continue;
ret.m[i][k] = (ret.m[i][k] + (m1.m[i][j] * m2.m[j][k]) % mod) % mod;
}
}
}
return ret;
}
Matrix quickmul(Matrix m, LL n) {
Matrix ret = Matrix();
for(LL i = 1; i <= m.r; i++) ret.m[i][i] = 1;
ret.r = m.r;
ret.c = m.c;
while(n) {
if(n & 1) ret = mul(m, ret);
m = mul(m, m);
n >>= 1;
}
return ret;
}

LL a, b, c, d, p, n;

signed main() {
// freopen("in", "r", stdin);
LL T;
scanf("%lld", &T);
Matrix mat, ret;
while(T--) {
scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&p,&n);
mat.r = mat.c = 3;
ret.r = 3; ret.c = 1;
mat.m[1][1] = d, mat.m[1][2] = c;
mat.m[2][1] = 1; mat.m[2][2] = 0; mat.m[2][3] = 0;
mat.m[3][1] = 0; mat.m[3][2] = 0; mat.m[3][3] = 1;
ret.m[1][1] = b; ret.m[2][1] = a; ret.m[3][1] = 1;
if(n == 1) {
printf("%lld\n", a);
continue;
}
if(n < p) {
for(LL i = 3; i <= n; i=p/(p/i)+1) {
mat.m[1][1] = d, mat.m[1][2] = c;
mat.m[2][1] = 1; mat.m[2][2] = 0; mat.m[2][3] = 0;
mat.m[3][1] = 0; mat.m[3][2] = 0; mat.m[3][3] = 1;
mat.m[1][3] = (p / i);
if(n <= p / (p/i)) mat = quickmul(mat, n-i+1);
else mat = quickmul(mat, p/(p/i)+1-i);
ret = mul(mat, ret);
}
printf("%lld\n", ret.m[1][1]);
continue;
}
for(LL i = 3; i <= p; i=p/(p/i)+1) {
mat.m[1][1] = d, mat.m[1][2] = c;
mat.m[2][1] = 1; mat.m[2][2] = 0; mat.m[2][3] = 0;
mat.m[3][1] = 0; mat.m[3][2] = 0; mat.m[3][3] = 1;
mat.m[1][3] = (p / i);
mat = quickmul(mat, p/(p/i)+1-i);
ret = mul(mat, ret);
}
mat.m[1][1] = d, mat.m[1][2] = c;
mat.m[2][1] = 1; mat.m[2][2] = 0; mat.m[2][3] = 0;
mat.m[3][1] = 0; mat.m[3][2] = 0; mat.m[3][3] = 1;
mat.m[1][3] = 0LL;
mat = quickmul(mat, n-max(p, 2LL));
ret = mul(mat, ret);
printf("%lld\n", ret.m[1][1]);
}
return 0;
}