Luo Gu P3263 [JLOI2015] meaningful string (+ mathematical matrix multiplication)

Write a solution to a problem at Memorial, after all, a question I made so many mistakes SB (sizeof (0), long long pass parameters to pass int, no assignment, etc.) is not easy = =

Consider the number of columns \ (A_N = (\ FRAC {B + \ sqrt {D}} {2}) ^ n-+ (\ FRAC {B- \ sqrt {D}} {2}) ^ \ n-) , the number of columns \ (A_N \ ) the characteristic equation for the \ (x ^ 2-bx- \ frac {db ^ 2} {4} = 0 \)

The \ (\ {a_n \} \ ) Recurrence formula \ (= A_N-n-Ba_. 1} + {\ DB FRAC {^ {2}}. 4-A_ {n-2} \) , the boundary is \ (a_0 2 = \) , \ (A_1 = B \)

As prepared according to the subject condition \ (. 4 | DB ^ 2 \) , so \ (A_N \) is an integer, so \ ((\ frac {b + \ sqrt {n}} {2}) ^ n + (\ frac {b- \ sqrt {n}} {2 }) ^ n \) is an integer

And because \ (B ^ 2 \ Leq D <(B +. 1) ^ 2 \) , so \ (-. 1 <\ FRAC B- {\ sqrt {2} {D}} \ leq0 \) , a small number, so we can quickly direct power matrix obtained \ (a_n \) and then calculate the answer

When \ (n-\) is even and \ (b ^ 2 \ neq d \) when, \ (0 \ Leq (\ FRAC B- {\ sqrt {D}} {2}) ^ n-<. 1 \) , this when \ (ans = a_n-1 \ )

Other cases, \ (-. 1 <(\ FRAC B- {\ sqrt {D}} {2}) ^ n-\ Leq 0 \) , this time \ (ans = a_n \)

Code:

#include <bits/stdc++.h>
#define ll long long
#define mod 7528443412579576937ll
using namespace std;

ll Mar[2][2],E[2][2],tmp[2][2];

inline ll add(ll x,ll y){ return x+y>=mod||x+y<0?x-mod+y:x+y; }
inline ll Minus(ll x,ll y){ return x-y<0?x-y+mod:x-y; }
inline ll qorg(ll x,ll y){
    ll d=(ll)(x*(long double)y/mod+0.5);
    return Minus(x*y,d*mod);
}
void mul(ll a[][2],ll b[][2]){
    memset(tmp,0,sizeof(tmp));
    for(int i=0;i<2;++i)
        for(int k=0;k<2;++k)
            for(int j=0;j<2;++j)
                tmp[i][j]=add(tmp[i][j],qorg(a[i][k],b[k][j]));
    for(int i=0;i<2;++i)
        for(int j=0;j<2;++j)
            a[i][j]=tmp[i][j];
}
void qpow(ll x){ for(;x;x>>=1,mul(E,E)) if(x&1) mul(Mar,E); }

int main(){ 
    ll b,d,n;
    scanf("%lld%lld%lld",&b,&d,&n);
    Mar[0][0]=b,Mar[0][1]=2;
    E[0][0]=b,E[1][0]=(d-b*b)/4ll,E[0][1]=1;
    if(n==0){ puts("1");return 0; }
    qpow(n-1);
    if(n%2==0 && b*b!=d) Mar[0][0]=Minus(Mar[0][0],1);
    printf("%lld",Mar[0][0]%mod);
}

Guess you like

Origin www.cnblogs.com/PsychicBoom/p/11102788.html