ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 && 快速幂)(2015沈阳网赛1002题)
It is known that y=(5+2√6)^(1+2^x).
For a given integer x (0≤x<2^32) and a given prime number M (M≤46337) , print [y]%M . ([y] means the integer part of y )
Following are T lines, each containing two integers x and M , as introduced above.
Each line contains an integer representing [y]%M .
题目大意就是求那个式子y=(5+2√6)^(1+2^x)。
考虑(5+2√6)^n和(5-2√6)^n;
分别设为A和B,
自然A*B=1
考虑A+B,发现奇数次幂的根号消掉了,于是是个整数。
由因为A>1,自然B<1所以说A的小数部分就是1-B,所以A的整数部分就是A+B-1。
于是就是求A+B,便能得到结果。
而这个式子跟特征根求解的一次线性递推式的结果很像。
于是考虑x^2+bx+c=0这个特征方程,跟为5+2√6和5-2√6。
得b=-10,c=1。
于是递推式为f(n+2)=10f(n+1)-f(n)。
然后本地打表发现,不管模范围内的任何素数,这个序列的循环节都不是很大。
于是考虑直接暴力循环节,不过记忆化下来。
然后就是考虑2^x模循环节的结果即可,这个用快速幂。
复杂度是O(r+logx),其中r为循环节大小。
题解中通过结论 (p^2- p)(p^2-1)为循环节使用矩阵快速幂.
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; const int maxM = ;
int x, m, r[maxM], f[maxM]; void init()
{
if (r[m] != -)
return;
f[] = %m;
f[] = %m;
for (int i = ;; ++i)
{
f[i] = ((*f[i-]-f[i-])%m+m)%m;
if (f[i-] == f[] && f[i] == f[])
{
r[m] = i-;
break;
}
}
} //快速幂m^n
int quickPow(LL x, int n, int mm)
{
int a = ;
while (n)
{
a *= n& ? x : ;
a %= mm;
n >>= ;
x *= x;
x %= mm;
}
return a;
} void work()
{
int k, ans;
k = quickPow(, x, r[m]);
k = (k+)%r[m];
f[] = %m;
f[] = %m;
for (int i = ; i <= k; ++i)
f[i] = ((*f[i-]-f[i-])%m+m)%m;
ans = (f[k]-+m)%m;
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
memset(r, -, sizeof(r));
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
printf("Case #%d: ", times+);
scanf("%d%d", &x, &m);
init();
work();
}
return ;
}
ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 && 快速幂)(2015沈阳网赛1002题)的更多相关文章
- ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)
Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
Description There is a special number sequence which has n+1 integers. For each number in sequence, ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—HDU5490 Simple Matrix (数学 && 逆元 && 快速幂) (2015合肥网赛07)
Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progressio ...
- hdu 5455 (2015沈阳网赛 简单题) Fang Fang
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不 ...
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc
Best Solver Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Tota ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
随机推荐
- eclipse tasks
tasks可以在代码里增加标识,通过tasks view可以快速的找到这些标识的地方,有助于提高开发效率和代码管理. 通过Eclipse的 Window==>Show View==>Tas ...
- js的new到底干了啥 -
javascript通过new操作符构建一个对象的步骤 <Javascript高级程序设计>的解释: 创建一个对象 将构造函数的作用域赋给新对象(把新对象作为构造函数的调用上下文,也就是t ...
- DNN优势
- 洛谷 P2051 [SDOI2009]学校食堂
传送门- 题目分析:首先,我们先看看做菜时间的运算机制.$(A~\texttt{or}~B)-(A~\texttt{and}~B)$这个试子看起来有点复杂(因为我太菜了),仔细想想,是不是可以转化为$ ...
- SAP 定价
近几天做门店团购销售订单上传SAP接口程序,SO创建测试过程中, 遇到定价问题,同事在定价过程的增强过不了. VOFM 了解到定价过程是个非常复杂的环节,此处出现程序处理过程中ZMP0定价条件下的价格 ...
- C#调用大漠插件
大漠插件是一个很不错的东西,在按键精灵和易语言里面用得很多,可以后台找图找字,写游戏自动脚本用得特别多.前面写一个微信的自动脚本,查了一些资料,易语言不太熟悉,按键精灵功能上可能不好实现,就找了些资料 ...
- PAT 天梯赛 【】 L3-015. 球队“食物链” 【BFS+剪枝】
题目链接 https://www.patest.cn/contests/gplt/L3-015 思路 用一个 数组标记 胜负 每次输入一行字符串 然后遍历 如果 碰到 W 那么 vis[i][j] = ...
- 培训笔记——ubuntu安装
1.选择安装位置,如果是做双系统提前准备一个分区,如果覆盖安装就无所谓了2.下载iso镜像文件,制作启动盘,Windows或linux环境下分别有相应的软件可以制作启动光盘或U盘3.开始安装一 设置开 ...
- Data Structure Array: Move all zeroes to end of array
http://www.geeksforgeeks.org/move-zeroes-end-array/ #include <iostream> #include <vector> ...
- CSS3图片悬停放大动画
在线演示 本地下载