HDU 5451 Best Solver(矩阵快速幂+ 共轭复数 + 循环节 数论啊 )
来源:互联网 发布:日本充气娃娃淘宝网 编辑:程序博客网 时间:2024/05/29 00:31
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5451
Problem Description
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.
It is known thaty=(5+26√)1+2x .
For a given integerx (0≤x<232) and a given prime number M (M≤46337) , print [y]%M . ([y] means the integer part of y )
It is known that
For a given integer
Input
An integer T (1<T≤1000) , indicating there are T test cases.
Following areT lines, each containing two integers x and M , as introduced above.
Following are
Output
The output contains exactly T lines.
Each line contains an integer representing[y]%M .
Each line contains an integer representing
Sample Input
70 463371 463373 463371 4633721 46337321 463374321 46337
Sample Output
Case #1: 97Case #2: 969Case #3: 16537Case #4: 969Case #5: 40453Case #6: 10211Case #7: 17947
Source
2015 ACM/ICPC Asia Regional Shenyang Online
PS:
这题和HDU 2256 HDU 4565 类似!
推出公式后再暴力寻找一下循环节!推到见HDU 2256
或者:http://m.blog.csdn.net/blog/u011481752/26291179
代码如下:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define LL long longstruct Matrix{ LL m[4][4];} I,A,B,T;LL mod;LL n;int ssize = 2;Matrix Mul(Matrix a,Matrix b){ int i, j, k; Matrix c; for(i = 1; i <= ssize; i++) { for(j = 1; j <= ssize; j++) { c.m[i][j]=0; for(k = 1; k <= ssize; k++) { c.m[i][j]+=(a.m[i][k]*b.m[k][j]); c.m[i][j]%=mod; } } } return c;}Matrix quickpagow(LL n){ Matrix m = A, b = I; while(n > 0) { if(n & 1) b = Mul(b,m); n = n >> 1; m = Mul(m,m); } return b;}LL quickpow(LL m,LL n,LL k){ LL ans = 1; while (n > 0) { if (n & 1) ans = (ans*m)%k; n = n >> 1 ; m = (m*m)%k; } return ans;}const int maxm = 46337+10;int r[maxm], f[maxm];void init(){ if(!r[mod]) { f[0] = 2; f[1] = 10; for(int i = 2; ; i++) { f[i] = (10LL*f[i-1]-f[i-2]+mod)%mod; if(f[i] == f[1] && f[i-1] == f[0]) { r[mod] = i-1; break; } } } memset(I.m,0,sizeof(I.m)); memset(A.m,0,sizeof(A.m)); for(int i = 0; i <= ssize; i++) { //单位矩阵 I.m[i][i] = 1; }}int main(){ int t; int cas = 0; scanf("%d",&t); while(t--) { scanf("%I64d%I64d",&n,&mod); init(); A.m[1][1] = 10%mod; A.m[1][2] = -1; A.m[2][1] = 1; A.m[2][2] = 0; T = quickpagow((quickpow(2,n,r[mod])+1)%r[mod]-1); B.m[1][1] = 10%mod; B.m[2][1] = 2%mod; T = Mul(T,B); printf("Case #%d: %d\n",++cas,(T.m[1][1]+mod-1)%mod); } return 0;}/*70 463371 463373 463371 4633721 46337321 463374321 46337*/
0 0
- HDU 5451 Best Solver(矩阵快速幂+ 共轭复数 + 循环节 数论啊 )
- hdu 5451 Best Solver -广义斐波那契+矩阵快速幂+共轭构造+循环节
- HDU 4565 So Easy!(共轭复数 + 矩阵快速幂 数论)
- hdoj 5451 Best Solver 【求循环节 + 矩阵快速幂】
- hdu 5451 Best Solver 快速矩阵乘法 Fibonacci数列的循环节
- hdu 5451 Best Solver(矩阵乘法+数论)
- 5451 Best Solver 构造共轭复根求递推矩阵&广义斐波那契循环节降幂
- [HDU 5451] Best Solver (向下取整+矩阵快速幂+广义Fibonacci降幂)
- Hdu 5451 Best Solver(快速幂取模+循环节) -2015 ACM-ICPC沈阳网赛1002
- HDU 5451 Best Solver
- hdu 5451 Best Solver (特征方程求通项+广义Fibonacci数列找循环节)
- HDU 4565So Easy! 共轭构造+矩阵快速幂
- hdu 4565 矩阵快速幂+共轭的应用
- Hdu 4565 So Easy! 矩阵快速幂+共轭数
- HDU-4565 So Easy! 矩阵快速幂 & 共轭构造
- hdu 4291 矩阵快速幂+循环节
- HDU 4291 (矩阵快速幂+循环节)
- hdu 4291(矩阵快速幂 + 循环节)
- 学习ThinkPHP3.2.2:了解并测试与3.1不同之处_自定义函数文件名称
- 【J2EE系列】—Servlet
- Unity 5 中的全局光照技术详解(建议收藏)
- LeetCode题解:Construct Binary Tree from Inorder and Postorder Traversal
- 将博客搬至CSDN
- HDU 5451 Best Solver(矩阵快速幂+ 共轭复数 + 循环节 数论啊 )
- JAVA实现链表的反转(《剑指offer》)
- 学习ThinkPHP3.2.2:模板文件的加载
- 对 Web 应用程序进行性能调优
- Multiply Strings
- 学习ThinkPHP3.2.2:模板文件所需css文件位置
- Codeforces Round #322 (Div. 2)C. Developing Skills
- 统计文件行数
- ==和equal()的区别