[Hdu 4959] Poor Akagi number theory (Lucas number, quadratic field operation, proportional sequence sum)

Source: Internet
Author: User

[Hdu 4959] Poor Akagi number theory (Lucas number, quadratic field operation, proportional sequence sum)
Poor AkagiTime Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 131 Accepted Submission (s): 29


Problem DescriptionAkagi is not only good at basketball but also good at math. Recently, he got a sequence Ln from his teacher. Ln is defined as follow:
$ \ Large L (n) = \ begin {cases}
2 & \ text {if} n = 0 \\
1 & \ text {if} n = 1 \\
L (n-1) + L (n-2) & \ text {if} n> 1
\ End {cases} $
And Akagi's teacher cherishes Agaki's talent in mathematic. so he wants Agaki to spend more time studying math rather than playing basketball. so he decided to ask Agaki to solve a problem about Ln and promised that as soon as he solves this problem, he can go to play basketball. and this problem is:
Given N and K, you need to find \ (\ Large \ sum \ limits _ {0} ^ {N} L_ I ^ K \)

And Agaki needs your help.
InputThis problem contains multiple tests.
In the first line there's one number T (1 ≤ T ≤ 20) which tells the total number of test cases. for each test case, there an integer N (0 ≤ N ≤ 10 ^ 18) and an integer K (1 ≤ K ≤ 100000) in a line.
OutputFor each test case, you need to output the answer mod 1000000007 in a line.
Sample Input

33 12 24 3

Sample Output
1014443

SourceBestCoder Round #5
Theme
Calculate the first n items of the k power of the Lucas number and the number of Lucas is L [0] = 2, L [1] = 1, L [n] = L [N-2] + L [n-1] (n> = 2)
At the time, I thought that I could find the second surplus based on zoj 3774 ...... The result shows that 1e9 + 7 does not have a quadratic surplus. Finally, a clever method is found, which is based on the general formula of the Lucas number.
Set the sum formula


Define a quadratic field

In this case, the quadratic field can be directly added and multiplied (the final result is an integer, so root number 5 will not exist in the result)
Reload the plus and multiplication signs of the quadratic field, and define the Fast Power Operation of the quadratic field. Just enter the formula. =. = It seems that the data of hangdian in this question has not been corrected. For a moment, the public ratio is directly returned n + 1 (may cause overflow). Even if it is AC, then the positive solution is still WA ......
Here, only the code is corrected.
/***** Author: ahm001 ***** source: hdu 4959 ***** time: 08/21/14 ***** type: math *****/# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include
            # Include
             
               # Define sqr (x) * (x) # define LL long # define INF 0x3f3f3f3f # define PI acos (-1.0) # define eps 1e-10 # define mod 1000000007 using namespace std; int cnt = 0; typedef pair
              
                Qf; qf operator + (qf a, qf B) {return make_pair (. first + B. first) % mod, (. second + B. second) % mod);} qf operator * (qf a, qf B) {// if (LL). first * (LL) B. first) % mod + (LL). second * (LL) B. second) % mod * 5ll) % mod <0) // printf ("% d \ n",. first,. second, B. first, B. second); if (. first <0). first + = mod; if (B. first <0) B. first + = mod; if (. second <0). second + = mod; if (B. second <0) B. second + = mod; return make_pair (LL). first * (LL) B. first) % mod + (LL). second * (LL) B. second) % mod * 5ll) % mod, (LL). first * (LL) B. second) % mod + (LL). second * (LL) B. first) % mod);} qf pow (qf a, LL x) {qf res (1, 0); qf multi = a; while (x) {if (x & 1) {res = res * multi;} multi = multi * multi; x/= 2;} return res;} LL pow (LL a, LL B) {LL res = 1; LL multi = a; while (B) {if (B & 1) {res = res * multi % mod;} multi = multi * multi % mod; b/= 2;} return res;} qf acce (qf a, LL B) {qf ans = make_pair (1, 0); // if (a = ans) return make_pair (B +); // after this statement is removed, the AC is removed. However, if n + 1 is not moduled, qf powe = a will be cracked in the subsequent results; qf sum = a; qf multi = make_pair (1, 0); while (B) {if (B & 1) {ans = ans + (multi * sum ); multi = multi * powe;} sum = sum * (powe + make_pair (1, 0); powe = powe * powe; B/= 2;} return ans ;} LL inv [100005]; qf r1 [100005], r2 [100005]; void egcd (LL a, LL B, LL & x, LL & y) {if (B = 0) {x = 1, y = 0; return;} egcd (B, a % B, x, y); LL t = x; x = y; y = t-a/B * y;} int main () {LL x, y; for (LL I = 1; I <= 100000; I ++) {egcd (I, mod, x, y); x = (x + mod) % mod; inv [I] = x ;} r1 [0] = make_pair (100000); r2 [0] = make_pair (); for (int I = 1; I <=; I ++) {r1 [I] = r1 [I-1] * make_pair (1,1); r2 [I] = r2 [I-1] * make_pair (1,-1);} int T; scanf ("% d", & T); while (T --) {cnt = 0; LL n, m; scanf ("% I64d % I64d", & n, & m); // n = 1e18; // m = 1e5; qf ans = make_pair (0, 0); LL Ca = 1; LL v = pow (inv [2], m); for (LL I = 0; I <= m; I ++) {// printf ("% lld \ n", Ca); qf p (Ca, 0); qf tmp = r1 [I] * r2 [m-I] * make_pair (v, 0); tmp = acce (tmp, n); tmp = tmp * p; ans = ans + tmp; Ca = Ca * (m-I) % mod; Ca = Ca * inv [I + 1] % mod;} LL aa = (LL) ans. first; printf ("% I64d \ n", aa); // printf ("% d \ n", ans. first, ans. second); // printf ("% d \ n", cnt);} return 0 ;}
              
             
           
          
         
        
       
      
     
    
   
  
 


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.