[hdu 4959]Poor Akagi 数论(卢卡斯数,二次域运算,等比数列求和)
Poor Akagi
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 131 Accepted Submission(s): 29
$$\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.
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.
3
3 1
2 2
4 3
10
14
443
则求和公式为
/**
**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 long
#define INF 0x3f3f3f3f
#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((a.first+b.first)%mod,(a.second+b.second)%mod);
}
qf operator * (qf a,qf b)
{
// if ((((LL)a.first*(LL)b.first)%mod+((LL)a.second*(LL)b.second)%mod*5ll)%mod<0)
// printf("%d %d %d %d\n",a.first,a.second,b.first,b.second);
if (a.first<0) a.first+=mod;
if (b.first<0) b.first+=mod;
if (a.second<0) a.second+=mod;
if (b.second<0) b.second+=mod;
return make_pair((((LL)a.first*(LL)b.first)%mod+((LL)a.second*(LL)b.second)%mod*5ll)%mod,
(((LL)a.first*(LL)b.second)%mod+((LL)a.second*(LL)b.first)%mod)%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+1,0);//这条语句去掉后AC了。可是n+1不取模将会造成后面的结果爆掉
qf powe=a;
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(1,0);
r2[0]=make_pair(1,0);
for (int i=1;i<=100000;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 %d \n",ans.first,ans.second);
// printf("%d\n",cnt);
}
return 0;
}
[hdu 4959]Poor Akagi 数论(卢卡斯数,二次域运算,等比数列求和)的更多相关文章
- hdu 1568 (log取对数 / Fib数通项公式)
hdu 1568 (log取对数 / Fib数通项公式) 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]= ...
- Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】
Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...
- 七夕节 (HDU - 1215) 【简单数论】【找因数】
七夕节 (HDU - 1215) [简单数论][找因数] 标签: 入门讲座题解 数论 题目描述 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们 ...
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- HDU 1005 Number Sequence(数论)
HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...
- hdu 4803 Poor Warehouse Keeper(贪心+数学)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26005267 题目链接:hdu 4803 P ...
- HDU 4952 Poor Mitsui(贪心)
HDU 4957 Poor Mitsui pid=4957" style="">题目链接 思路:利用相邻交换法去贪心就可以.注意容积为0的情况,这是个坑点 代码: ...
- HDU 1074 Doing Homework (动态规划,位运算)
HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...
- C语言 · 数的运算
算法提高 数的运算 时间限制:1.0s 内存限制:512.0MB 输入一个正整数(范围[1..10000]),打印其平方(不保留小数位).平方根.倒数.(用指针实现,保留2位小数,输 ...
随机推荐
- noip200806火柴棒等式
试题描述: 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1)加号与等 ...
- Java集合--HashMap分析
HashMap在Java开发中有着非常重要的角色地位,每一个Java程序员都应该了解HashMap. 本文主要从源码角度来解析HashMap的设计思路,并且详细地阐述HashMap中的几个概念,并深入 ...
- activemq消息重发机制[转]
大家知道,JMS规范中,Message消息头接口中有setJMSRedelivered(boolean redelivered)和getJMSRedelivered()方法,用于设置和获取消息的重发标 ...
- 不用call和apply方法模拟实现ES5的bind方法
本文首发我的个人博客:前端小密圈,评论交流送1024邀请码,嘿嘿嘿
- 阻止picker.js插件弹出键盘
<input id="focus" type="text" value="" placeholder="手机号" ...
- 如何使用花生壳 发布WCF服务 进行外网访问 z
http://www.cnblogs.com/wanglg/p/5375230.html 当我们发布WCF服务的时候,可以直接通过服务器的域名或者IP进行. 但是如果仅仅是通过花生壳进行域名解析,需要 ...
- Code First 数据库的表中属性的配置
数据类型的约定配置 默认规则 列的数据类型是由数据库决定的,SqlServer的默认规则如下 String: nvarchar(MAX) Int:int Bool:bit Decimal:deci ...
- inno setup检查是否已经安装
[Registry] Root: HKLM; Subkey: "Software\MCS"; ValueType: string; ValueName: "MCSVers ...
- 使用Jacob与Word文件交互
转自:http://www.blogjava.net/lonsy/archive/2009/01/09/250713.html Jacob项目的官方地址: Http://sourceforge.net ...
- iOS开源项目:UIDevice-with-UniqueIdentifier-for-iOS-5
用于替代系统UDID的方法. https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5 1.使用方法: UILabel *l ...