HDU 2256 & HDU 4565 (矩阵快速幂 + 公式推演)
2015-08-16 20:40
393 查看
HDU 2256
题意:
计算⌊(2√+3√)2n⌋mod1024\lfloor{(\sqrt2 +\sqrt3)^{2n}}\rfloor \mod1024思路:
∵f(n)=(2√+3√)2n=(5+26√)n=An+Bn∗6√\because f(n)={(\sqrt2 +\sqrt3)^{2n}} = {(5 +2\sqrt6)^{n}}= A_n+B_n*\sqrt6∴f(n−1)=An−1+Bn−1∗6√\therefore f(n-1)= A_{n-1}+B_{n-1}*\sqrt6
又∵f(n)=(5+26√)∗f(n−1)又\because f(n)= (5+2\sqrt6)*f(n-1)
∴f(n)=(5∗An−1+12∗Bn−1)+(2∗An−1+5∗Bn−1)∗6√\therefore f(n)= (5*A_{n-1}+12*B_{n-1})+(2*A_{n-1}+5*B_{n-1})*\sqrt6
所以递推矩阵就是:
(52125)∗(An−1Bn−1)=(AnBn)\left(
\begin{array}{cc}
5 & 12 \\
2 & 5
\end{array}
\right)
*
\left(
\begin{array}{cc}
A_{n-1}\\
B_{n-1}
\end{array}
\right)
=\left(
\begin{array}{cc}
A_{n}\\
B_{n}
\end{array}
\right)
A1=5,B1=2.A_1=5,B_1=2.
然后套矩阵快速幂模板即可求出An,BnA_n,B_n.
又∵(5+26√)n=An+Bn∗6√又\because (5+2\sqrt6)^n = A_n+B_n*\sqrt6
∴(5−26√)n=An−Bn∗6√\therefore (5-2\sqrt6)^n = A_n-B_n*\sqrt6
∴(5+26√)n+(5−26√)n=2An\therefore (5+2\sqrt6)^n +(5-2\sqrt6)^n = 2A_n
又∵(5−26√)n<1又\because (5-2\sqrt6)^n <1
∴⌊(5+26√)n⌋=2An−1\therefore \lfloor(5+2\sqrt6)^n\rfloor = 2A_n-1
所以最后答案就是2An−12A_n-1
代码:
[code]/* * @author FreeWifi_novicer * language : C++/C */ #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<string> #include<map> #include<set> #include<vector> #include<queue> using namespace std; #define clr( x , y ) memset(x,y,sizeof(x)) #define cls( x ) memset(x,0,sizeof(x)) #define mp make_pair #define pb push_back typedef long long lint; typedef long long ll; typedef long long LL; const int mod = 1024; const int maxn = 4; struct Matrix{ int n,m; lint a[maxn][maxn]; Matrix(int n , int m){ this->n = n; this->m = m; cls(a); } Matrix operator * (const Matrix &tmp){ Matrix res(n,tmp.m); for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < tmp.m ; j++){ for(int k = 0 ; k < m ; k++){ res.a[i][j] = (res.a[i][j] + (a[i][k] * tmp.a[k][j]) % mod) % mod; } } } return res; } }; void Matrix_print(Matrix x){ for(int i = 0 ; i < x.n ; i++){ for(int j = 0 ; j < x.m ; j++) cout << x.a[i][j] << ' '; cout << endl; } } Matrix fast_pow(Matrix x ,int n){ Matrix res(x.n,x.m); for(int i = 0 ; i < x.n ; i++) res.a[i][i] = 1; while(n){ if(n&1) res = res * x; x = x*x; n >>= 1; //Matrix_print(res); //cout << endl; } return res; } void solve(){ lint n; cin >> n; if(n == 1){ cout << 9 << endl; return; } Matrix base(2,1); Matrix fun(2,2); base.a[0][0] = 5; base.a[1][0] = 2; fun.a[0][0] = 5; fun.a[0][1] = 12; fun.a[1][0] = 2; fun.a[1][1] = 5; fun = fast_pow(fun,n-1); base = fun * base; cout << (2*base.a[0][0] - 1) % mod << endl; } int main(){ int t ; cin >> t; while(t--){ solve(); } return 0; }
附送HDU 4565代码:
[code]/* * @author FreeWifi_novicer * language : C++/C */ #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<string> #include<map> #include<set> #include<vector> #include<queue> using namespace std; #define clr( x , y ) memset(x,y,sizeof(x)) #define cls( x ) memset(x,0,sizeof(x)) #define mp make_pair #define pb push_back typedef long long lint; typedef long long ll; typedef long long LL; lint mod; lint n , a , b ; const int maxn = 4; struct Matrix{ int n,m; lint a[maxn][maxn]; Matrix(int n , int m){ this->n = n; this->m = m; cls(a); } Matrix operator * (const Matrix &tmp){ Matrix res(n,tmp.m); for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < tmp.m ; j++){ for(int k = 0 ; k < m ; k++){ res.a[i][j] = (res.a[i][j] + (a[i][k] * tmp.a[k][j]) % mod) % mod; } } } return res; } }; void Matrix_print(Matrix x){ for(int i = 0 ; i < x.n ; i++){ for(int j = 0 ; j < x.m ; j++) cout << x.a[i][j] << ' '; cout << endl; } } Matrix fast_pow(Matrix x ,int n){ Matrix res(x.n,x.m); for(int i = 0 ; i < x.n ; i++) res.a[i][i] = 1; while(n){ if(n&1) res = res * x; x = x*x; n >>= 1; //Matrix_print(res); //cout << endl; } return res; } void solve(){ if(n == 1){ lint ans = (lint)( a + ceil( sqrt( b * 1.0 ) ) ) % mod; cout << ans << endl; return; } Matrix base(2,1); Matrix fun(2,2); base.a[0][0] = a % mod; base.a[1][0] = 1; fun.a[0][0] = a % mod; fun.a[0][1] = b % mod; fun.a[1][0] = 1; fun.a[1][1] = a % mod; fun = fast_pow(fun,n-1); base = fun * base; cout << (2 * base.a[0][0]) % mod << endl; } int main(){ while(cin >> a >> b >> n >> mod ){ solve(); } return 0; }
相关文章推荐
- 【您的凭证不工作】win7远程桌面提示,请输入新凭据。登录没有成功
- 实模式和保护模式
- NOIP2010 关押罪犯(图论+二分)
- 《C算法》读书笔记 (4):Hello,Joseph!
- JavaScript编程--任意输入一个数判断是不是闰年,数列求和练习
- CodeForces-448C Painting Fence
- 黑马程序员——多线程(上)——第11天
- 每日python(1)
- 06 AppCan入门学习之基本按件(BUTTON/SWITCH/LISTVIEW)
- xcode AutoLayout
- python字典模拟登陆
- LeetCode(230) Kth Smallest Element in a BST
- 多线程访问HashMap容易犯的错误
- Spring配置简化
- POJ 3468 A Simple Problem with Integers (线段树 区间增减 区间求和)
- struts2学习笔记(十三)文件下载
- php性能优化(一)压力测试工具篇
- 从尾到头打印链表
- 再次开始学习python-Python Web实战 第一课
- nginx整合tomcat