【LOJ】#2106. 「JLOI2015」有意義的字符串
阿新 • • 發佈:2018-09-02
inline == memset make ans rac end get 由於
題解
點一個技能點叫特征方程
就是
\(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; }
【LOJ】#2106. 「JLOI2015」有意義的字符串