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

Source: Internet
Author: User
Poor Akagi Time 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 <cstdio> # include <iostream> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define INF 0x3f3f3f # define PI ACOs (-1.0) # define EPS 1e-10 # define mod 1000000007 using namespace STD; int CNT = 0; typedef pair <LL, ll> 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.