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. 基于jquery的bootstrap在线文本编辑器插件Summernote (转)

    Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...

  2. 【BZOJ5020】[THUWC 2017]在美妙的数学王国中畅游 泰勒展开+LCT

    [BZOJ5020][THUWC 2017]在美妙的数学王国中畅游 Description 数字和数学规律主宰着这个世界. 机器的运转, 生命的消长, 宇宙的进程, 这些神秘而又美妙的过程无不可以用数 ...

  3. How to avoid Over-fitting using Regularization?

    http://www.mit.edu/~9.520/scribe-notes/cl7.pdf https://en.wikipedia.org/wiki/Bayesian_interpretation ...

  4. Python菜鸟之路:Python基础-类(1)——概念

    什么是类? 在python中,把具有相同属性和方法的对象归为一个类(class).类是对象的模板或蓝图,类是对象的抽象化,对象是类的实例化.类不代表具体的事物,而对象表示具体的事物. 类的创建 cla ...

  5. JavaScript点击事件-一个按钮触发另一个按钮

    <input type="button" value="Click" id="C" onclick="Go();" ...

  6. STM32 HAL库 UART使用printf

    // 添加这个函数 int fputc(int ch,FILE *f) { uint8_t temp[]={ch}; HAL_UART_Transmit(&UartHandle,temp,,) ...

  7. Python问题解决记录

    Python如何进行中文注释:网址 解决Python UnicodeEncodeError: 'ascii' codec can't encode: 网址1.网址2.网址3 Python 字符串转换为 ...

  8. JDK8新特性:接口的静态方法和默认方法

    在jdk8之前,interface之中可以定义变量和方法,变量必须是public.static.final的,方法必须是public.abstract的.由于这些修饰符都是默认的,所以在JDK8之前, ...

  9. SpringCloud-服务的消费者(Feign)

    Feign简介 Feign是一个声明式的伪Http客户端,它是的写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign注解和JAX-RS注解.F ...

  10. Hibernate技术

    Hibernate中3个重要的类: 配置类(configuration) 负责管理Hibernate的配置信息,包含数据库连接URL.数据库用户.数据库密麻麻.数据库驱动等. 会话工厂类(Sessio ...