【LOJ】#2106. 「JLOI2015」有意义的字符串

时间:2023-03-09 16:48:55
【LOJ】#2106. 「JLOI2015」有意义的字符串

题解

点一个技能点叫特征方程

就是

\(a_{n + 2} = c_1 a_{n + 1} + c_2 a_{n}\)

\(x^2 = c_1 x + c_2\)

解出两根来是\(x_1,x_2\)

通项就是

\(Ax_1^{n} + Bx_2^{n}\)把第一项和第二项带入可以解出来A和B

然后为了得到通项是

\((\frac{b + \sqrt{d}}{2})^n + (\frac{b - \sqrt{d}}{2})^{n}\)的数列

那么我们让

\(c_1 = b\)

\(c_2 = \frac{d - b^2}{4}\)

矩乘算出来\(a_n\)

\((\frac{b + \sqrt{d}}{2})^n = a_n - (\frac{b - \sqrt{d}}{2})^{n}\)

由于题面里少打了四个字,【整数部分】取模,那么我们观察一下后面那部分,如果\(n\)是偶数而且\(b^2\)和\(d\)不等,那么会减1

代码

#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned long long u64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 - '0' + c;
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
const u64 MOD = 7528443412579576937;
u64 inc(u64 a,u64 b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
u64 mul(u64 a,u64 b) {
u64 res = 0,t = a;
while(b) {
if(b & 1) res = inc(res,t);
t = inc(t,t);
b >>= 1;
}
return res;
}
void update(u64 &x,u64 y) {
x = inc(x,y);
}
struct Matrix {
u64 f[2][2];
Matrix(){memset(f,0,sizeof(f));}
friend Matrix operator * (const Matrix &a,const Matrix &b) {
Matrix c;
for(int i = 0 ; i < 2 ; ++i) {
for(int j = 0 ; j < 2 ; ++j) {
for(int k = 0 ; k < 2 ; ++k) {
update(c.f[i][j],mul(a.f[i][k],b.f[k][j]));
}
}
}
return c;
}
}A,ans;
u64 d,b,n,Inv4,an;
void fpow(Matrix &res,Matrix &a,int64 c) {
res = a;--c;Matrix t = a;
while(c) {
if(c & 1) res = res * t;
t = t * t;
c >>= 1;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(b);read(d);read(n);
Inv4 = mul((MOD + 1) / 2,(MOD + 1) / 2);
A.f[0][0] = b;A.f[0][1] = mul(inc(d,MOD - mul(b,b)),Inv4);
A.f[1][0] = 1;
if(n == 0) an = 2;
else if(n == 1) an = b;
else {
fpow(ans,A,n - 1);
an = inc(mul(b,ans.f[0][0]),mul(2,ans.f[0][1]));
}
if((d % b == 0 && d / b == b) || n & 1) ;
else update(an,MOD - 1);
out(an);enter;
}