HDU - 4565 So Easy! 矩阵快速幂
2015-05-19 22:49
429 查看
题目大意:求
![](http://acm.hdu.edu.cn/data/images/C468-1001-1.jpg)
解题思路:这题跟HDU - 2256 Problem of Precision类似,只不过这题是向上取整
有一个隐藏的条件:(a-1)^2 < b < a ^ 2
表明a - 1 < b < a
也就是(a - sqrt(b) )^n是小于1的
附上HDU - 2256 Problem of Precision的题解链接/article/1993088.html
注意int的范围,有可能会溢出
![](http://acm.hdu.edu.cn/data/images/C468-1001-1.jpg)
解题思路:这题跟HDU - 2256 Problem of Precision类似,只不过这题是向上取整
有一个隐藏的条件:(a-1)^2 < b < a ^ 2
表明a - 1 < b < a
也就是(a - sqrt(b) )^n是小于1的
附上HDU - 2256 Problem of Precision的题解链接/article/1993088.html
注意int的范围,有可能会溢出
[code]#include<cstdio> typedef long long ll; const int N = 2; struct Matrix{ ll mat ; }A, B, tmp; int a, b, n, m; void init() { B.mat[0][0] = B.mat[1][1] = 1; B.mat[0][1] = B.mat[1][0] = 0; A.mat[0][0] = A.mat[1][1] = a; A.mat[0][1] = b; A.mat[1][0] = 1; } Matrix matrixMul(Matrix x, Matrix y) { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) { tmp.mat[i][j] = 0; for(int k = 0; k < N; k++) tmp.mat[i][j] += (x.mat[i][k] * y.mat[k][j]) % m; } return tmp; } void solve(){ while(n) { if(n & 1) B = matrixMul(B,A); A = matrixMul(A,A); n >>= 1; } } int main() { while(scanf("%d%d%d%d", &a, &b, &n, &m) != EOF) { init(); solve(); printf("%I64d\n", (2 * B.mat[0][0]) % m); } return 0; }
相关文章推荐
- hdu 4565So Easy!(巧妙的矩阵快速幂)
- So Easy! HDU - 4565 矩阵快速幂
- HDU 4565 So Easy! 矩阵快速幂 + 共轭数
- hdu 4565 So Easy!(矩阵快速幂)
- HDU 4565 So Easy! 矩阵快速幂
- 2013长沙邀请赛 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!(矩阵快速幂+数学)
- hdu - 4565 - So Easy!(矩阵快速幂)
- hdu 4565 So Easy! 矩阵快速幂
- 【矩阵快速幂】HDU_4565_So Easy!