Test instructions: There is a non-empty string S1 only contains the character "a", "B". Si can be changed to S (i+1) by transforming A in SI to B and all B to a. The lengths of SN and SM are now given, respectively, L1, L2, and know N, M, ask if there is a reasonable S1, and if so, what is the length of the SK (mod 1e9 + 7). All variables are within the range of (0, 1e9).
Solution: Set S1 contains N1 A and N2 B (n1 + n2 > 0), it is not difficult to find Si N1 and N2 to meet the Fibonacci sequence nature, si.n1 = fibo[i-1] * n1 + fibo[i] * n2,si.n2 = fibo[i] * n 1 + fibo[i+1] * n2; where N1, N2 is the number of a and B in S1, Fibo is Fibonacci series, and fibo[0] = 1, fibo[1] = 0, fibo[i] = Fibo[i-2] + fibo[i-1] (i > 1).. Thus, you can get two two yuan a linear equation A * n1 + b * N2 = c, d * n1 + E * n2 = f, where a = fibo[n-1] + fibo[n],b = fibo[n] + fibo[n + 1],c = fibo[ M-1] + fibo[m], d = fibo[m] + fibo[m + 1], c = l1,f = L2. This can be solved N1, n2, then ans = (fibo[k-1] + fibo[k]) * n1 + (Fibo[k] + fibo[k+1]) * n2. Since k reaches the 1e9 level, the ANS is solved with a matrix fast power (Fibonacci number satisfies this property). The premise is that there is a solution, if there is no solution, need to judge, a lot of judgment point, first of all, consider the quadratic equation, can exist without solution, and then consider N and m to too large may lead to no solution, because L1, L2 is only 1e9 levels, N and M can not be too large, it is possible to judge if N, Case, L1, L2 cannot be more than 1e9. Look at the details again. Look at the code
My Code
#include <cstdio>using namespace STD;typedef Long LongllConst intMoD =1e9+7; ll fibo[ the];intGetans (ll A, ll B, LL C, LL D, LL E, LL F, LL &x, ll &y) {if(b * d-a * e = =0)return-1;if((a >= c && b >= c) | | (d >= F && e >= f))return-1;if((c * d-a * f)% (b * d-a * e)! =0)return-1; y = (c * d-a * f)/(b * d-a * e);if((c-b * y)% A! =0)return-1; x = (C-b * y)/A;if(X <0|| Y <0|| x + y <=0)return-1; x = x% mod; y = y% mod;return 1;}structdata{ll a[2][2];} D0,d1;dataoperator* (data x, data y) {data ans; for(inti =0; I <2; i++) { for(intj =0; J <2; J + +) {Ans.a[i][j] =0; for(intK =0; K <2; k++) {Ans.a[i][j] + = x.a[i][k] * Y.a[k][j];if(Ans.a[i][j] >= MoD) ans.a[i][j]%= mod; } } }returnAns;} Data GD (ll K) {if(k = =0)returnD1; Data d = GD (k >>1); D = d * D;if(K &1) d = d * D0;returnD;} ll Fibo2 (ll x) {if(x <=Ten)returnFIBO[X]% MoD; Data d = GD (X-1);returnd.a[1][0];}intMain () {fibo[0] =1, fibo[1] =0; for(inti =2; I < -; i++) Fibo[i] = fibo[i-2] + fibo[i-1]; d0.a[0][0] =0, d0.a[0][1] =1; d0.a[1][0] =1, d0.a[1][1] =1; d1.a[0][0] =1, d1.a[0][1] =0; d1.a[1][0] =0, d1.a[1][1] =1;intTscanf("%d", &t); for(intKase =1; Kase <= T; kase++) {ll n, x, M, Y, K;scanf("%lld%lld%lld%lld%lld", &n,&x,&m,&y,&k);if(N >= the|| M >= the){printf("impossible\n");Continue; } ll N1, N2;intres = Getans (fibo[n-1] + fibo[n], Fibo[n] + fibo[n+1], X, fibo[m-1] + fibo[m], Fibo[m] + fibo[m+1], Y, N1, N2);if(res = =-1)printf("impossible\n");Else{ll ans = (Fibo2 (k-1) + Fibo2 (k))% mod * N1% mod + (Fibo2 (k) + Fibo2 (+1))% mod * n2% MoD;printf("%lld\n", ans% mod); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-12045 Fun with Strings