HDU 4565So Easy! 矩阵快速幂
来源:互联网 发布:查淘宝数据有哪些插件 编辑:程序博客网 时间:2024/05/22 08:11
设(a+sqrt(b))^n为(Xn + Yn*sqrt(b)),那么显然有(a+sqrt(b))^(n+1) 为 (a*Xn + b*Yn + (aYn+Xn)*sqrt(b))。
那么显然有(a+sqrt(b))的Xn,Yn可以表示为 :
然后又会发现,(a-sqrt(b))^n可以表示为:
那么会发现(a+sqrt(b))^n = (a+sqrt(b))^n + (a-sqrt(b))^n - (a-sqrt(b))^n = Xn+Yn*sqrt(b) +Xn-Yn*sqrt(b) -(a-sqrt(b))^n = 2*Xn - (a-sqrt(b))^n。
又由题意得a-sqrt(b)∈(0,1),切最终答案向上取整,所以可得最终答案为2×Xn。
去年长沙网赛的题,这种题都不能1Y还怎么玩?
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <cmath>#include <stack>#include <map>#pragma comment(linker, "/STACK:1024000000")#define EPS (1e-8)#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3fusing namespace std;const int MAXN = 61;struct MAT{ int row,col; int mat[MAXN][MAXN]; void Init(int R,int C,int val) { row = R,col = C; for(int i = 1;i <= row; ++i) for(int j = 1;j <= col; ++j) mat[i][j] = (i == j ? val : 0); } MAT Multi(MAT c,int MOD) { MAT tmp; tmp.Init(this->row,c.col,0); int i,j,k; for(k = 1;k <= this->col; ++k) for(i = 1;i <= tmp.row; ++i) for(j = 1;j <= tmp.col; ++j) (tmp.mat[i][j] += (this->mat[i][k]*c.mat[k][j])%MOD) %= MOD; return tmp; } MAT Quick(int n,int MOD) { MAT res,tmp = *this; res.Init(row,col,1); while(n) { if(n&1) res = res.Multi(tmp,MOD); tmp = tmp.Multi(tmp,MOD); n >>= 1; } return res; } void Output() { cout<<" **************** "<<endl; int i,j; for(i = 1;i <= row; ++i) { for(j = 1;j <= col; ++j) printf("%3d ",mat[i][j]); puts(""); } cout<<" &&&&&&&&&&&&& "<<endl; }};int main(){ int a,b,n,m; MAT A,B; freopen("data1.in","r",stdin); while(scanf("%d %d %d %d",&a,&b,&n,&m) != EOF) { a %= m,b %= m; A.Init(2,1,0); B.Init(2,2,0); B.mat[1][1] = a; B.mat[1][2] = b; B.mat[2][1] = 1; B.mat[2][2] = a; A.mat[1][1] = a; A.mat[2][1] = 1; B = B.Quick(n-1,m); B = B.Multi(A,m); printf("%d\n",(2*B.mat[1][1])%m); } return 0;}
0 0
- HDU 4565 So Easy! (矩阵快速幂)
- hdu 4565 - So Easy!(矩阵快速幂)
- hdu 4565 So Easy! 矩阵快速幂
- HDU 4565 So Easy!(矩阵快速幂)
- HDU 4565 So Easy!(矩阵快速幂)
- HDU 4565So Easy! 矩阵快速幂
- hdu 4565 So Easy!(矩阵快速幂)
- HDU - 4565 So Easy! 矩阵快速幂
- HDU 4565 so easy ! 矩阵快速幂
- HDU 4565——So Easy!(矩阵快速幂乘法)
- hdu 4565 So Easy!(构造矩阵快速幂)
- 数学公式+矩阵快速幂 hdu-4565 So Easy!
- hdu 4565 So Easy!(矩阵+快速幂)
- hdu - 4565 - So Easy!(矩阵快速幂)
- HDU 4565 So Easy!(数学+矩阵快速幂)
- hdu 4565 So Easy!(推公式&矩阵快速幂)
- HDU 4565 So Easy!(矩阵快速幂)
- hdu 4565 So Easy!(矩阵快速幂)
- 情景分析“C语言的const关键字”
- Struts2 - 常用的constant总结
- 社説 20150304 補助金企業寄付 適正化へ与野党は知恵を絞れ
- TWaver推智能手表挑战华为苹果
- 物联12:电子标签按技术分类
- HDU 4565So Easy! 矩阵快速幂
- 软件项目管理流程总结
- Python写自动化之启动进程并获取进程输出
- 浅谈C++中指针和引用的区别
- Fragment的setUserVisibleHint方法实现懒加载
- php中读取大文件实现方法详解
- Unhandled exception type ParseException
- android 实现服务器连接获取数据和传递数据(1)
- Android:高仿QQ头像截取升级版