Best Solver
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 1115 Accepted Submission(s): 645
Problem Description
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. It is known that y=(5+26√)1+2x.For a given integer x (0≤x<232) 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
Source
Recommend
wange2014
题目描述:
对于,给出x和mod,求y向下取整后取余mod的值为多少?
分析:
这种(a+sqrt(b))^n%mod,向下取整取余的式子有个递推式 S(n) = 2*a*S(n-1)+(b-a*a)*S(n-2) 这个式子得到的结果是向上取整,我们这里是向下取整,所以最后得到的结果还要减一
我这篇博客https://www.cnblogs.com/l609929321/p/9398349.html有这个递推式的推导过程
由上诉递推式不难得到:S(n) = (10*S(n-1)-S(n-2)+mod)%mod
但是这个题目的指数1+2^x(x<2^32)非常大,就算用矩阵快速幂也不行,所以我们得对式子进行一次优化
看到指数很大的类似斐波拉数列问题(都是线性递推式)很容易的想到找循环节
因为mod<=46337,所以我们直接遍历求前面46337项一定会出现循环节,然后记录下循环节的位置就好
(以后找模一个数的周期最大取到他的三倍,证明博客:https://www.xuebuyuan.com/1198038.html)
再对指数按照循环节的位置进行取模就可以了
AC代码
#include