[Zoj 3774] power of Fibonacci number theory (quadratic residue Extended Euclidean proportional series summation)

Source: Internet
Author: User
Tags mul
Power of Fibonacci Time Limit: 5 seconds memory limit: 65536 KB

In mathematics, maid are the numbers of the following integer sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144,233,377 ,...

By definition, the first two numbers in the Fibonacci sequence are 1 and 1, and each subsequent number is the sum of the previous two. in mathematical terms, the sequenceFNOf maid is defined by the Recurrence RelationFN=FN-1+FN-2With seed valuesF1= 1 andF2= 1.

And your task is to find ΣFik, The sum ofK-Th power of the firstNTerms in the Fibonacci sequence. Because the answer can be very large, you should output the remainder of the answer divided by 1000000009.

Input

There are multiple test cases. The first line of input is an integerTIndicates the number of test cases. For each test case:

There are two integersNAndK(0 <=N& Lt; = 1018, 1 & lt; =K<= 100000 ).

Output

For each test case, output the remainder of the answer divided by 1000000009.

Sample Input
510 14 2020 29999 99987654321987654321 98765
Sample output
14348783295274049690113297124108672406
Hint

The first test case. 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 = 143.

The second test case, 120 + 120 + 220 + 320 = 3487832979, and 3487832979 = 3*1000000009 + 487832952, so the output is 487832952.


Theme

It is clear that the sum of the First N values of each number in the Fibonacci series is obtained from the power of M.


Ideas

At that time, I didn't dare to write down the value range of M until CF 255 (446 C) had a line segment tree similar to the idea. The official editorial gave an explanation, is to find its quadratic surplus, and then the series can be expressed by power difference.


(Solving quadratic residue)

(The above three formulas are introduced with Extended Euclidean, and sqrt5 is used as a whole for reverse element solving)

(Then we can see a formula for the remaining series)

The reason for doing so may be similar to the reverse element principle, because we can find an integer operation in the remainder of each step until the result is an integer.

Next, Set


Therefore, the M-Level Attribute of any number is


Summation of the First n items after the spread of the binary


Enumerate K to sum

Accelerate the sum of an equi-ratio series in the middle. Otherwise, the time-out will still occur.


Calculate the quadratic residue and the code of each coefficient: (because the quadratic residue has two different roots, different results may not affect the calculation result)

Template from: blog.csdn.net/acdreamers/article/details/10182281

#include <cstdio>#include <cstring>#include <algorithm>#define mod 1000000009using namespace std;long long sqrt5,s,r1,r2;long long ts,w;struct D{    long long p,d;};void egcd(long long a,long long b,long long &x,long long &y){    if (b==0)    {        x=1;        y=0;        return;    }    egcd(b,a%b,x,y);    int t=x;    x=y,y=t-a/b*y;    return;}long long mypow(long long x,long long y,long long p){    long long res=1,mul=x;    while (y)    {        if (y & 1)            res=res * mul % p;        mul=mul * mul % p;        y/=2;    }    return res;}D mul(D a,D b,long long m){    D ans;    ans.p=(a.p * b.p % m +a.d * b.d %m *w % m)%m;    ans.d=(a.p * b.d % m +a.d * b.p% m)%m;    return ans;}D power(D a,long long b,long long m){    D ans;    ans.p = 1;    ans.d = 0;    while (b)    {        if (b & 1)        {            ans=mul(ans,a,m);        }        b/=2;        a=mul(a,a,m);    }    return ans;}long long sqre(long long x,long long y){    if (y==2) return 1;    if (mypow(x,(y-1)>>1,y)+1 == y)        return -1;    long long a,t;    for (a=1;a<y;a++)    {        t= a * a - x;        w= (t + y) % y;        if (mypow(w,(y-1)>>1,y)+1 == y) break;    }    D tmp;    tmp.p=a;    tmp.d=1;    D ans = power(tmp,(y+1)>>1,y);    return ans.p;}int main(){    sqrt5=sqre(5,mod);    printf("%I64d\n",sqrt5);    long long x,y;    egcd(5,mod,x,y);    x=(x+mod)%mod;    s=(sqrt5*x)%mod;    printf("%I64d\n",s);    egcd(2,mod,x,y);    x=(x+mod)%mod;    r1=((sqrt5+1)*x)%mod;    r2=((-sqrt5+1+mod)*x)%mod;    printf("%I64d\n",r1);    printf("%I64d\n",r2);    int T;    return 0;}

Code of this question after the coefficient is introduced

# Include <cstdio> # include <cstring> # include <algorithm> long s = 723398404, R1 = 308495997, R2 = 691504013; long mod = 1000000009; long long C [100005]; typedef long ma [2] [2]; void egcd (long a, long B, long & X, long & Y) {If (B = 0) {x = 1; y = 0; return;} egcd (B, A % B, x, y); int T = X; X = Y, y = T-A/B * Y; return;} long MYPOW (long X, long y) {long res = 1; while (y) {If (Y % 2) RES = res * x % mod; X = x * x % MOD; y/= 2;} return res ;} long long ACCE (long X, long y) // returns the sum of the proportional series. {long ans = 0; long powe = x; long sum = X; long long Mul = 1; while (y) {If (Y & 1) {ans + = Mul * sum; ans % = MOD; Mul * = powe; mul % = MOD;} sum * = (powe + 1); sum % = MOD; powe * = powe; powe % = MOD; y/= 2;} return ans ;} int main () {int t; long n, m; scanf ("% d", & T); While (t --) {long ans = 0; scanf ("% LLD", & N, & M); C [0] = 1; long X, Y; For (long I = 1; I <= m; I ++) {egcd (I, Mod, x, y); X = (x + mod) % MOD; c [I] = (C [I-1] * X) % MOD; C [I] = (C [I] * (M-I + 1) % MOD ;} for (long I = 0; I <= m; I ++) {x = C [I]; X = x * ACCE (MYPOW (D1, I) * MYPOW (D2, M-I) % mod, n) % MOD; X = (x + mod) % MOD; If (M-I) % 2) ans = (ANS-x + mod) % MOD; else ans = (ANS + x + mod) % MOD;} ans = ans * MYPOW (S, m) % MOD; printf ("% LLD \ n", ANS);} system ("pause"); Return 0 ;}



[Zoj 3774] power of Fibonacci number theory (quadratic residue Extended Euclidean proportional series summation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.