UVA10655 Contemplation! Algebra
Posted zhylj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10655 Contemplation! Algebra相关的知识,希望对你有一定的参考价值。
题目
给定 (p = a + b) 和 (q = ab) 和 (n),求 (a ^ n + b ^ n)。
$0le nlt 2^{63} $
分析
大水题。
先考虑 (n) 较小的情况,可以很容易的想到递推:
[
egin{array}{}
ext{令} F(i) & = a ^ n + b ^ n & = (a + b)(a ^ {n - 1} + b ^ {n - 1}) - (ab ^ {n - 1} + a^{n - 1}b) & = (a + b)(a ^ {n - 1} + b ^ {n - 1}) - ab(a ^ {n - 2} + b ^ {n - 2}) & = p imes F(i - 1) - q imes F(i - 2)
end{array}
]
然后发现这个递推式可以用矩阵优化:
[
left[egin{matrix}
p & - q 1 & 0
end{matrix}
ight]
imes
left[egin{matrix}
F[i] F[i - 1]
end{matrix}
ight] =
left[egin{matrix}
F[i] imes p & + & F[i - 1] imes (-q) F[i] imes 1 & + & F[i - 1] imes 0
end{matrix}
ight] =
left[egin{matrix}
F[i + 1] F[i]
end{matrix}
ight]
]
即:
[
left[egin{matrix}
p & - q 1 & 0
end{matrix}
ight]^n
imes
left[egin{matrix}
F[1] F[0]
end{matrix}
ight] =
left[egin{matrix}
F[n + 1] F[n]
end{matrix}
ight]
]
显然,(F[1] = p, F[0] = 2)。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 10;
struct matrix {
ll a[MAXN][MAXN]; int rowSize, lineSize;
matrix(int x, int y) {
rowSize = x; lineSize = y;
}
ll *operator [](const unsigned &i) {return a[i];}
matrix operator *(matrix y) {
matrix ans(rowSize, y.lineSize);
for(int i = 0; i < ans.rowSize; i++)
for(int j = 0; j < ans.lineSize; j++) {
ans[i][j] = 0;
for(int k = 0; k < lineSize; k++)
ans[i][j] += a[i][k] * y[k][j];
}
return ans;
}
} u(2, 2);
matrix qPow(matrix x, ll b) {
matrix ans = u, base = x;
while(b) {
if(b & 1)
ans = ans * base;
base = base * base;
b >>= 1;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
u[0][0] = 1; u[0][1] = 0;
u[1][0] = 0; u[1][1] = 1;
ll p, q, n;
while(scanf("%lld%lld%lld", &p, &q, &n) == 3) {
matrix a(2, 2), b(2, 2), st(2, 1);
a[0][0] = p; a[0][1] = -q;
a[1][0] = 1; a[1][1] = 0;
st[0][0] = p; st[1][0] = 2;
b = qPow(a, n) * st;
printf("%lld
", b[1][0]);
}
return 0;
}
UVA10655 Contemplation! Algebra
以上是关于UVA10655 Contemplation! Algebra的主要内容,如果未能解决你的问题,请参考以下文章
UVa新汉诺塔问题(A Different Task,Uva 10795)