In mathematics, Fibonacci numbers or Fibonacci series or Fibonacci sequence 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 sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn - 1 + Fn - 2 with seed values F1 = 1 and F2 = 1.

And your task is to find ΣFiK, the sum of the K-th power of the first N terms 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 integer T indicates the number of test cases. For each test case:

There are two integers N and K (0 <= N <= 1018, 1 <= K <= 100000).

Output

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

Sample Input

5
10 1
4 20
20 2
9999 99
987654321987654321 98765

Sample Output

143
487832952
74049690
113297124
108672406

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.

题意:求前n项k次的斐波那契数列和 并模1e9+9 其中 \(n <= 10^{18} \), \(k <= 10^5 \)

思路:这题拿到手很快就想到使用矩阵进行快速幂取模求解,但问题是如果k的数值小一些——那就可以直接构造矩阵储存各个幂次并进行求解了。但1e5级别的高次幂显然要求用另外的方法。事实上这是道数论题。首先从斐波那契数列的通项公式上,\(fib(n)=\frac{1}{\sqrt{5}} [(\frac{1+\sqrt{5}}{2})^n-(\frac{1-\sqrt{5}}{2})^n] \),由于模的是素数,而且5恰好是模1e9+9的二次剩余,故我们使用\(x^2\equiv{5}\pmod{1000000009}\)的解代替其中的\(\sqrt{5}\),对中括号中的对称式子先进行k次幂的运算,再使用二次项展开,合并相同系数的项后可以发现就是求0到k次等比数列和,注意其中的分母全部要转换成逆元。

具体题目上我们先预处理所有的阶乘和对称式子,而逆元的计算直接使用费马小定理即可。

这题是zju的校赛题,看记录5个小时里还是有挺多人写出来的。

感觉好难orz,自己好菜,但是还是学到挺多的。

/** @Date    : 2017-03-18-15.39
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
const LL mod = 1e9 + 9;
const LL trem = 383008016;
LL fac[N], le[N], ri[N]; LL fpow(LL a, LL n)
{
LL res = 1;
while(n > 0)
{
if(n & 1)
res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
} LL Inv(LL x)
{
return fpow(x, mod - 2);
} void init()
{
LL tinv = Inv(2); fac[0] = 1;
le[0] = ri[0] = 1;
LL l = ((1 + trem + mod)%mod) * tinv % mod;
LL r = ((1 - trem + mod)%mod) * tinv % mod;
for(LL i = 1; i < N; i++)
fac[i] = fac[i - 1] * i % mod;
for(int i = 1; i < N; i++)
{
le[i] = le[i - 1] * l % mod;
ri[i] = ri[i - 1] * r % mod;
//cout << le[i] <<" " << ri[i] << endl;
}
}
int T;
LL n, k;
int main()
{
init();
cin >> T;
while(T--)
{
scanf("%lld%lld", &n, &k);
LL ans = 0;
for(int i = 0; i <= k; i++)
{
LL flag = 1;
if((k - i) % 2)
flag = -1;
LL t = le[i] * ri[k - i] % mod;
LL d = fac[k - i] * fac[i] % mod;
LL c = fac[k] * Inv(d) % mod;
/*--*/
LL x = (t * (1 - fpow(t, n)) % mod) * Inv(1 - t) % mod;
if(t == 1)
x = n % mod;
ans = (ans + flag * c * x ) % mod;
ans = (ans + mod) % mod;
//cout << t << endl;
}
ans = (ans * fpow(Inv(trem) % mod, k) + mod) % mod;
printf("%lld\n", ans);
}
return 0;
}

ZOJ 3774 Fibonacci的K次方和的更多相关文章

  1. 计算1至n的k次方的和

    package com.ywx.count; import java.util.Scanner; /** * @author Vashon * date:20150410 * 题目:计算1至n的k次方 ...

  2. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...

  3. 用积分方法求K次方和数列公式

    这是我很早以前在高中时发现的一个通用计算K次方和数列公式的方法,很特别的地方是用了微积分中的积分方法.目前我还没有发现有谁提出和我一样的方法,如果哪位读者有相关发现,麻烦告知我. 大家很多人都知道高斯 ...

  4. [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)

    Power of Fibonacci Time Limit: 5 Seconds      Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...

  5. Fibonacci数列的幂和 zoj 3774

    题目大意: 求斐波那契数列前n项的k次幂和  Mod 1000000009.    n<=1e18, k<=1e5 这题的k比较大,所以不能用矩阵乘法来递推.学到了新姿势...  http ...

  6. ZOJ 2672 Fibonacci Subsequence(动态规划+hash)

    题意:在给定的数组里,寻找一个最长的序列,满足ai-2+ai-1=ai.并输出这个序列. 很容易想到一个DP方程 dp[i][j]=max(dp[k][i])+1. (a[k]+a[i]==a[j], ...

  7. ZOJ 3702 Fibonacci

    解题思路: 找规律,不难的,打表 坑的地方在于题目限定条件 and the seed value for G(1) is a random integer t, (t>=1) 虽然都用粗体表示出 ...

  8. zoj 2060 Fibonacci Again(fibonacci数列规律、整除3的数学特性)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2060 题目描述: There are another kind ...

  9. zoj 1828 Fibonacci Numbers

    A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the firs ...

随机推荐

  1. FIsherman丶Team

    小组成员:郝恒杰,洪佳兴,张子祥 组长:郝恒杰 项目:Fisher Job(渔夫兼职) 简介: 我们的产品渔夫兼职是为了解决大学生兼职群体 的痛苦,他们需要一个好的渠道去找一个让自己满意的兼职,但是现 ...

  2. 对其中的一个特点将NABC的分析结果

    一.题目要求 每一个组员针对其中的一个特点将NABC的分析结果发表博客上(截止日期4月8日晚24:00前). 二.分析结果 特点之一:通讯方便 <渴了么>这个安卓APP特点之一就是通讯方便 ...

  3. java 中的 i=i++

    记得大学刚开始学C语言时,老师就说:自增有两种形式,分别是i++和++i,i++表示的是先赋值后加1,++i是先加1后赋值,这样理解了很多年也没出现问题,直到遇到如下代码,我才怀疑我的理解是不是错了: ...

  4. Requests库常用方法及其详解

    request库七个方法详解 1. request方法 所有方法的的基础方法,三个参数:method,url,**kwargs. 1.1 method:请求方式 method参数共有七个可选的值,分别 ...

  5. C++ Primer Plus学习:第六章

    C++入门第六章:分支语句和逻辑运算符 if语句 语法: if (test-condition) statement if else语句 if (test-condition) statement1 ...

  6. iOS- Swift:如何使用iOS8中的UIAlertController

    1.前言 在前段时间手机QQ:升级iOS8.3后,发图就崩的情况, 就是因为iOS8更新UIAlertController后,仍然使用UIAlertview导致的 具体原因分析 这个可以看腾讯团队发出 ...

  7. String、Date、Calendar之间的转换

    1.String.Date.Calendar之间的转换 要用到格式化类SimpleDateFormat package com.rong.se; import java.text.ParseExcep ...

  8. 使用fabric1.14.0和fabric2.4.0

    fabric1.14.0(支持Python2.5-2.7版本): from  fabric.api import * env.gateway = '192.168.181.2'            ...

  9. win8平板APP开发的教程文章

    http://blog.csdn.net/tcjiaan/article/details/7866595 基于C#的Metro工程如何引用C++的动态库——FIleNotFound解决办法: http ...

  10. HTTP协议 结构,get post 区别(阿里面试)

    如果需要想了解相关的TCP的协议结构,底层架构,以及每次面试必问的三次握手,四次挥手可以 参考:TCP协议详解7层和4层解析(美团面试,阿里面试) 尤其是三次握手,四次挥手 具体发送的报文和状态都要掌 ...