Problem Description
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.

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 )

 
Input
An integer T (1<T≤1000) , indicating there are T test cases.
Following are T lines, each containing two integers x and M , as introduced above.
 
Output
The output contains exactly T lines.
Each line contains an integer representing [y]%M .
 
Sample Input
7
0 46337
1 46337
3 46337
1 46337
21 46337
321 46337
4321 46337
 
Sample Output
Case #1: 97
Case #2: 969
Case #3: 16537
Case #4: 969
Case #5: 40453
Case #6: 10211
Case #7: 17947

题目大意就是求那个式子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题)的更多相关文章

  1. 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 ...

  2. ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

    Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...

  3. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  4. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  5. 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 ...

  6. hdu 5455 (2015沈阳网赛 简单题) Fang Fang

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5455 题意就是找出所给字符串有多少个满足题目所给条件的子串,重复的也算,坑点是如果有c,f以外的字符也是不 ...

  7. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  8. HDU 5451 Best Solver 数论 快速幂 2015沈阳icpc

    Best Solver Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Tota ...

  9. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

随机推荐

  1. 【React Native开发】React Native配置执行官方样例-刚開始学习的人的福音(8)

    ),React Native技术交流4群(458982758),请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...

  2. django定时任务python调度框架APScheduler使用详解

    # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a ...

  3. Biorhythms

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 135099   Accepted: 43146 Description So ...

  4. iOS 符号化崩溃日志

    1.获取一下三个文件   1. crash报告(.crash文件) 2. 符号文件 (.dsymb文件) 3. 应用程序文件 (appName.app文件,把IPA文件后缀改为zip,然后解压,Pay ...

  5. Notepad++ Tidy2 插件的核心配置

    在已有配置的基础上加上这四行: 以免符号被转换成HTML实体了 preserve-entities: yes quote-ampersand: yes quote-marks: no quote-nb ...

  6. jquery点击一组按钮中的一个,跳转至对应页面处理策略。(如点击订单列表中的一个订单,跳转至该订单的详情)

    将改组按钮的数据设置一个相同的属性(如class),然后每个按钮设置不同的id 当用户点击属性为class的按钮,根据id属性来判断点击的是哪个按钮,然后进行相关操作. 代码示例: <scrip ...

  7. 【python】-- 函数非固定参数,返回值(return)

    函数非固定参数 1.默认参数: 代码如下: def information_register(name,age,country,sex): print("----注册信息------&quo ...

  8. java中枚举类型的使用

    Java 枚举(enum) 详解7种常见的用法 JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. web项目里实体类使用枚举类型: 一般在该实体 ...

  9. [luogu4315]月下“毛景树”

    [luogu4315]月下"毛景树" luogu 联赛前复习一发树剖.不会告诉你WA了4发 #define ls x<<1,l,mid #define rs x< ...

  10. mydql练习答案

    .查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[学号]连接两个临时表: 学号 ...