[JLOI2015]有意义的字符串（数学+矩阵乘法）

题解：

$\left(\frac{b+\sqrt{d}}{2}{\right)}^{n}+\left(\frac{b-\sqrt{d}}{2}{\right)}^{n}$$({b+\sqrt d \over2})^n+({b-\sqrt d \over2})^n$

${a}^{n}+{b}^{n}=\left({a}^{n-1}+{b}^{n-1}\right)\ast \left(a+b\right)-\left({a}^{n-2}+{b}^{n-2}\right)\ast ab$$a^n+b^n=(a^{n-1}+b^{n-1})*(a+b)-(a^{n-2}+b^{n-2})*ab$

$\left(\frac{b+\sqrt{d}}{2}{\right)}^{n}=Ans-\left(\frac{b-\sqrt{d}}{2}{\right)}^{n}$$({b+\sqrt d \over2})^n=Ans-({b-\sqrt d \over2})^n$

Code:

#include<cmath>
#include<cstdio>
#define ld long double
#define ul unsigned long long
#define fo(i, x, y) for(int i = x; i <= y; i ++)
using namespace std;

const ul mo = 7528443412579576937;

ul ksj(ul x, ul y) {
ul s = 0;
for(; y; y /= 2, x = x * 2 % mo)
if(y & 1) s = (s + x) % mo;
return s;
}

ul b, d, n, ans;

struct node {
ul a[2][2];
} a, c, e, li;

void mul(node &a, node b) {
fo(i, 0, 1) fo(j, 0, 1) li.a[i][j] = 0;
fo(k, 0, 1) fo(i, 0, 1) fo(j, 0, 1)
li.a[i][j] = (li.a[i][j] +ksj(a.a[i][k], b.a[k][j])) % mo;
a = li;
}

int main() {
freopen("jxamfe.in", "r", stdin);
freopen("jxamfe.out", "w", stdout);
scanf("%llu %llu %llu", &b, &d, &n); ul n0 = n;
if(n == 0) {printf("1"); return 0;}
a.a[0][0] = 2; a.a[0][1] = b;
c.a[0][1] = (d - ksj(b, b) + mo) % mo / 4; c.a[1][1] = b;
c.a[1][0] = 1;
n --;
for(; n; n /= 2) {
if(n & 1) mul(a, c);
e = c; mul(c, e);
}
ans = a.a[0][1];
ul g = sqrt(d * 1.0);
if(!(g == b && (g * g == d)) && n0 % 2 == 0)
ans = (ans - 1 + mo) % mo;
printf("%llu", ans);
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120