CC

自然万物都趋向从有序变得无序

0%

HDU 6442 GuGu Convolution(组合数学|分治)

题目链接


其实我们可以发现最后化简出来(A+B)n=a+bB(A+\sqrt B)^n = a + b * \sqrt B

(AB)n=abB(A-\sqrt B)^n = a - b * \sqrt B

我们可以得到最后的ans=bBans = b \sqrt B,那么我们只需要算一个b就可以了,这里算b除了上面题解说的那种分治的思想其实还可以用矩阵快速幂计算。

我们假设(A+B)n=an+bnB(A+ \sqrt B)^n = a_n + b_n \sqrt B

那么(A+B)n+1=an+1+bn+1B=(an+bnB)(A+B)=(Aan+bnB)+(an+Abn)B(A+ \sqrt B)^{n+1} \\ = a_{n+1} + b_{n+1} \sqrt B \\ = (a_n + b_n\sqrt B) \ast (A+\sqrt B)\\ = (A \ast a_n + b_n \ast B) + (a_n + A \ast b_n)\sqrt B

那么就可以构造递推式计算了。

代码一:

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
/*************************************************************************
> File Name: HDU-6442.cpp
> Author: wood
> Mail: cbcruel@gmail.com
> Created Time: 2018年08月26日 星期日 08时49分26秒
************************************************************************/

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<LL,LL> pr;
#define fi first
#define se second
#define mp make_pair

inline pr mul(pr a, pr b, LL mod, LL B){
return mp((a.fi *b.fi % mod + a.se *b.se%mod*B%mod) % mod ,(a.fi *b.se%mod+a.se*b.fi%mod) % mod);
}

pr solve(LL a, LL b, LL B, LL mod,LL n){
pr ans = mp(a,b);
pr x = mp(a,b);n--;
while(n){
if(n&1) ans = mul(ans,x,mod,B);
x = mul(x,x,mod,B);
n >>= 1;
}return ans;
}

int main(){
int t;
scanf("%d", &t);
while(t--){
LL n,p;
pr x;
scanf("%lld%lld%lld%lld", &x.fi, &x.se, &n,&p);
LL tt = solve(x.fi,1,x.se,p,n).se;
tt = (tt + p) % p;
for(LL i = 2; i *i <= x.se; ++i)
if(x.se%(i*i) == 0){
while(x.se%(i*i) == 0){
x.se /=(i*i);
tt = tt * i % p;
}
}
printf("1 %lld %lld\n",(tt%p+p)%p, x.se);
}return 0;
}

代码二:

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
/*************************************************************************
> File Name: HDU-6442.cpp
> Author: wood
> Mail: cbcruel@gmail.com
> Created Time: 2018年08月26日 星期日 08时49分26秒
************************************************************************/

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<LL,LL> prir;
#define fii first
#define see second
#define mp make_pair

LL mod;

template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if(c = getchar(),c == EOF) return 0; //EOF
while(c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c-'0');
while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline void out(LL x) {
if(x > 9) out( x / 10);
putchar( x % 10 + '0');
}

struct mat{
LL a[7][7];
mat (){memset(a, 0, sizeof(a)); }
mat operator *(const mat &q){
mat c;
for(int i = 1; i <= 2; ++i)
for(int k = 1; k <= 2; ++k)
if (a[i][k])
for(int j = 1; j <= 2; ++j){
c.a[i][j] += a[i][k] * q.a[k][j];
c.a[i][j] = c.a[i][j] % mod;
}return c;
}
};

mat qpow(mat x, LL n){
mat ans;
for(int i = 1; i <= 2; ++i)
ans.a[i][i] = 1;
while(n){
if (n&1) ans = ans * x;
x = x * x;
n >>= 1;
}return ans;
}

void Debug(mat t){
printf("----------Debug-----------\n");
for(int i = 1; i <= 2; ++i){
for(int j = 1; j <= 2; ++j)
printf("%lld ", t.a[i][j]);
printf("\n");
}printf("-------------------------\n");
}

int main(){
int tt;
LL A,B,n;
mat t;
scanf("%d", &tt);
while(tt--){
scanf("%lld%lld%lld%lld", &A,&B,&n,&mod);
t.a[1][4] = A;
t.a[1][5] = B;
t.a[2][6] = 1;
t.a[2][7] = A;
mat ans = qpow(t,n-1);
LL tmp = ans.a[2][8] + ans.a[2][9] * A;
//Debug(ans);
//printf("x=== %lld\n",tmp);
tmp %= mod;
for(LL i = 2; i * i <= B; ++i){
if(B %(i*i) == 0){
while(B %(i*i) == 0){
B /= (i*i);
tmp = tmp * i % mod;
}
}
}printf("1 %lld %lld\n", tmp, B);
}return 0;
}