More than 2019 cattle off summer school camp (ninth field) A.The power of Fibonacci

The meaning of problems: given n and m, f (n) is the amount Fibonacci number sequence of n items, requires calculating ans = f (1) ^ m + f (2) ^ m + .... f (n) ^ m. That amount Fibonacci series before the m-th power of n items and.

Solution: This problem seems to have two solutions: one is the cycle festival + CRT, one is the general term formula, in fact, this problem may also be able to directly exBM the AC. Here I learn is the solution circulation section + CRT https://blog.csdn.net/ftx456789/article/details/99684004 . The bloggers write tremendous Well, I summarize.

We all know that simple Fibonacci Amount column mold a prime number is cycling festival, and this cycle section also relatively easy to find, here we suppose fact Fibonacci m power contract volume series and also to cycle section, but modulus is not a prime number 1e9 annoying. We 1e9 prime factor decomposition

1000000000=2959=5121953125

If the modulus is 512 and 1,953,125 words were circulating section, respectively, 768 and 7,812,500, which means that we can solve for the answer, and the answer in the solution in the mold die 512 1953125 quick calculations. Then we can consider these two solutions are obtained a1 and a2. Then we would have the following congruence equation.

ans≡a1 (mod 512)

ans≡a2 (mod 1953125)

So finally we find this CRT ans it.

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL l1=768;  //循环节1 
const LL l2=7812500;  //循环节2 
const int P=1e9;
LL f[l2+10],sum[l2+10];
int n,mi;

LL m[5],a[5];

LL power(LL x,LL p) {
    LL ret=1;
    for (;p;p>>=1) {
        if (p&1) ret=ret*x%P;
        x=x*x%P;
    }
    return ret;
}

LL exgcd(LL a,LL b,LL &x,LL &y) {
    if (b==0) { x=1; y=0; return a; }
        else {
            LL tmp=exgcd(b,a%b,y,x);
            y-=x*(a/b); return tmp;
        }
}

long long work() {
    LL lcm=m[1],X=a[1], T, X, Y;
     for ( int I = 2 ; I <= 2 ; I ++ ) { 
        LL B = (A [I] the -X-% m [I] + m [I])% m [I]; 
        D LL = exgcd (LCM, m [I], X, Y);    // solve the equation out particular solutions t = XT (B / D) * X 
        IF (D% B) return - . 1 ; 
        t = (B / D) * X% m [I]; 
        X- = (T * + X-LCM);    // then the X-(K) = X-(. 1-K) + (TM) 
        LCM LCM * m = [I] / D; 
        
        X- = (X-% + LCM LCM)% LCM; 
    } 
    return X-; 
} 

int main ()  
{
    CIN>>n>>mi;
    f[0]=0; f[1]=1;
    for (int i=2;i<=l2;i++) f[i]=(f[i-1]+f[i-2])%P;
    for (int i=1;i<=l2;i++) sum[i]=(sum[i-1]+power(f[i],mi))%P;
    
    m[1]=512; m[2]=1953125;
    a[1]=((n/l1)*sum[l1]+sum[n%l1])%P;
    a[2]=((n/l2)*sum[l2]+sum[n%l2])%P;
    cout<<work()<<endl;
    return 0;
}

 

1000000000=2959=5121953125

Guess you like

Origin www.cnblogs.com/clno1/p/11566026.html