ZOJ 3774 二次剩余
题意:简单粗暴,求菲波那契数列每个数的m次的前n项和模1e9+7
思路:斐波那契通项式, 注意到有很多根号5,求二次剩余为5模1e9+7的解,显然我们可以直接找一个(383008016),然后拿来替代根号5,然后优化下,把中括号中共轭的两部分预处理下,然后由于是外部的一个指数m,从1枚举到m,来求二项式定理的每项系数,再用个逆元就好了。人家的校赛题(
/** @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<int ,int>
#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 二次剩余的更多相关文章
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
- ZOJ 3774 Fibonacci的K次方和
In mathematics, Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers of the f ...
- Fibonacci数列的幂和 zoj 3774
题目大意: 求斐波那契数列前n项的k次幂和 Mod 1000000009. n<=1e18, k<=1e5 这题的k比较大,所以不能用矩阵乘法来递推.学到了新姿势... http ...
- [hdu 4959]Poor Akagi 数论(卢卡斯数,二次域运算,等比数列求和)
Poor Akagi Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂
原题:ZOJ 3774 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
随机推荐
- 缓存-MemoryCache Class
这是使用MemoryCache缓存的一个例子. private void btnGet_Click(object sender, EventArgs e) { ObjectCache cache = ...
- C++ Primer Plus学习:第二章
C++入门第二章:开始学习C++ 进入C++ 首先,以下是一个C++程序: //myfirst.cpp 显示一行文字 #include<iostream> //预处理器编译指令 int m ...
- vs2015关于_CRT_SECURE_NO_WARNINGS警告说明
vs2015关于_CRT_SECURE_NO_WARNINGS警告说明 在VS中调用 strcpy.strcat 等函数时会提示 _CRT_SECURE_NO_WARNINGS 警告,原因是这些函数不 ...
- Java compiler level does not match the version of the installed Java project facet. map解决方法
右键项目"Properties",在弹出的"Properties"窗口左侧,单击"Project Facets",打开"Proje ...
- json 和 pickle
用于序列化的两个模块 json:用于字符串和python数据类型间进行转换 pickle:用于python特有的类型和python的数据类型间进行转换 json模块提供了四个功能:dumps dump ...
- 第190天:js---String常用属性和方法(最全)
String常用属性和方法 一.string对象构造函数 /*string对象构造函数*/ console.log('字符串即对象');//字符串即对象 //传统方式 - 背后会自动将其转换成对象 / ...
- HDU5266-pog loves szh III
题目 给出一棵\(n\)个点的树,从1到\(n\)编号,\(m\)次询问\({LCA} _{v\in[L,R]}\). \(n,m\le 3\times 10^5\) 分析 我的做法是直接对LCA进 ...
- OSPF虚连接简单配置
实验实例:<华为路由器学习指南>P715 本实例的拓扑结构如图:Area 2没有直接与骨干区域直接相连,Area 1被用作传输区域来连接Area 0与Area2.为了使Area2与骨干区域 ...
- Qt消息机制和事件
Qt消息机制和事件 1 事件 事件(event)是由系统或者 Qt 本身在不同的时刻发出的.当用户按下鼠标.敲下键盘,或者是窗口需要重新绘制的时候,都会发出一个相应的事件.一些事件在对用户操作做出响应 ...
- oAuth2.0理解
转自http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html 理解OAuth 2.0 作者: 阮一峰 日期: 2014年5月12日 OAuth是一个关 ...