hdu5451Best Solver=矩阵快速幂+广义斐波拉契
来源:互联网 发布:知乎温酒作品集 编辑:程序博客网 时间:2024/06/04 18:55
Problem Description
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.
It is known that
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
Source
2015 ACM/ICPC Asia Regional Shenyang Online
参考:http://blog.csdn.net/xtulollipop/article/details/52382791
同样的做法:然后就可以去找矩阵的循环节,可以暴力扫,也可以用结论:
http://blog.csdn.net/xtulollipop/article/details/52373948
然后就简单了。。
#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<cstdlib>#include<queue>#include<vector>#include<map>#include<stack>#include<set>using namespace std;#define pi acos(-1.0)#define EPS 1e-6 //log(x)#define e exp(1.0); //2.718281828//#define mod 1000000007#define INF 0x7fffffff#define inf 0x3f3f3f3f#pragma comment(linker,"/STACK:102400000,102400000")typedef long long LL;#define debug(x) cout<<x<<endl;#define debug2(x) cout<<x<<" ";//#define MOD 10000007LL MOD;struct Mat{ int n,m; LL mat[9][9];};Mat operator *(Mat a,Mat b){ Mat c; memset(c.mat,0,sizeof(c.mat)); c.n = a.n,c.m = b.m; for(int i=1;i<=a.n;i++){ for(int j=1;j<=b.m;j++){ for(int k=1;k<=a.m;k++){ c.mat[i][j] += (a.mat[i][k]*b.mat[k][j])%MOD; c.mat[i][j] %= MOD; } } } return c;}Mat operator +(Mat a,Mat b){ Mat c; memset(c.mat,0,sizeof(c.mat)); c.n = a.n,c.m = a.m; for(int i=1;i<=a.n;i++){ for(int j=1;j<=a.m;j++){ c.mat[i][j] = (a.mat[i][j]+b.mat[i][j])%MOD; } } return c;}Mat operator ^(Mat a,LL k){ Mat c; memset(c.mat,0,sizeof(c.mat)); c.n = a.n,c.m = a.n; for(int i=1;i<=a.n;i++)c.mat[i][i] = 1; while(k){ if(k&1){ c = c*a; } a = a*a; k>>=1; } return c;}void out(Mat a){ for(int i=1;i<=a.n;i++){ for(int j=1;j<=a.m;j++){ printf(j==a.m? "%I64d\n":"%I64d ",a.mat[i][j]); } }}LL quickPow(LL x, LL n, LL mm){ LL a = 1; while (n) { a *= n&1 ? x : 1; a %= mm; n >>= 1 ; x *= x; x %= mm; } return a;}int main(){ int T_T; scanf("%d",&T_T); LL x,m; int cas=0; while(T_T--){ scanf("%I64d %I64d",&x,&m); printf("Case #%d: ",++cas); MOD=m; LL tempmod=m*m-1; LL n=quickPow(2,x,tempmod)+1; if(n==0){ printf("1\n"); continue; } else if(n==1){ printf("9\n"); continue; } Mat pp; pp.n=pp.m=2; pp.mat[1][1]=5%MOD; pp.mat[1][2]=12%MOD; pp.mat[2][1]=2; pp.mat[2][2]=5%MOD; Mat A0; A0.n=2,A0.m=1; A0.mat[1][1]=5%MOD; A0.mat[2][1]=2; Mat ans=pp^(n-1); ans=ans*A0; printf("%I64d\n",(2*ans.mat[1][1]-1+MOD)%MOD); } return 0;}/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / /======`-.____`-.___\_____/___.-`____.-'====== `=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I have a dream!A AC deram!! orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz*/
- hdu5451Best Solver=矩阵快速幂+广义斐波拉契
- [HDU 5451] Best Solver (向下取整+矩阵快速幂+广义Fibonacci降幂)
- hdu 5451 Best Solver -广义斐波那契+矩阵快速幂+共轭构造+循环节
- hdu4565So Easy!+矩阵快速幂+广义斐波拉契
- hdu5451(矩阵快速幂+广义斐波拉契)
- hdu2256Problem of Precision+矩阵快速幂+广义斐波拉契
- hdoj 5451 Best Solver 【求循环节 + 矩阵快速幂】
- 矩阵的特征值 => 矩阵的幂 => 广义斐波拉契数列的通项公式
- HDU 5451 Best Solver(矩阵快速幂+ 共轭复数 + 循环节 数论啊 )
- 5451 Best Solver 构造共轭复根求递推矩阵&广义斐波那契循环节降幂
- hdu4565 So Easy! 广义斐波那契+矩阵快速幂+共轭构造
- 斐波拉契快速求法:矩阵快速幂
- 快速矩阵快速幂
- 广义逆矩阵
- 3 广义逆矩阵
- 广义逆矩阵
- 矩阵的广义逆
- hdu 5451 Best Solver 快速矩阵乘法 Fibonacci数列的循环节
- docker零基础学习
- Vector
- python中List的sort方法
- C++指针学习
- 【框架】[Spring3]下载安装、开源框架与IoC控制反转详解
- hdu5451Best Solver=矩阵快速幂+广义斐波拉契
- c++构造函数成员初始化中赋值和初始化列表两种方式的区别
- QT开发笔记
- Shell命令总结
- github创建个人网页(xxx.github.io)
- orcle学习之路第一天
- Python print 语句(Python 2 与 Python 3)
- Codeforces Div2 2016.08.29 C题
- JavaScript 也可以搞定嵌入式开发? | 硬创公开课