hdu 4565 So Easy!
2013-09-26 14:25
197 查看
数学。看了Siriuslzx大牛的解释才理解的:http://www.cnblogs.com/lzxskjo/archive/2013/05/27/3100828.html。
对于式子(a + sqrt(b))^ n 二项式分解可得 (a + sqrt ( b ))^ n = x + y * sqrt( b ) 。且有 (a - sqrt ( b ))^ n = x - y * sqrt( b ) ,(二项分解即可知)。
因为(a-1)2< b < a2 所以
0 < (a - sqrt ( b ))^ n = x - y * sqrt( b ) < 1, 于是有 : x - 1 < y * sqrt( b ) < x;所以答案就等于x + y * sqrt( b ) = 2 * x;求 x 的时候可以直接对(a + sqrt(b))^ n 进行快速幂求解。因为存在 (x + y * sqrt( b )) * (x + y * sqrt( b ) ) = (x * x +
y * y * b + (x * y * 2) * sqrt( b ));仔细理解一下快速幂就可以自己推出快速幂了。
对于式子(a + sqrt(b))^ n 二项式分解可得 (a + sqrt ( b ))^ n = x + y * sqrt( b ) 。且有 (a - sqrt ( b ))^ n = x - y * sqrt( b ) ,(二项分解即可知)。
因为(a-1)2< b < a2 所以
0 < (a - sqrt ( b ))^ n = x - y * sqrt( b ) < 1, 于是有 : x - 1 < y * sqrt( b ) < x;所以答案就等于x + y * sqrt( b ) = 2 * x;求 x 的时候可以直接对(a + sqrt(b))^ n 进行快速幂求解。因为存在 (x + y * sqrt( b )) * (x + y * sqrt( b ) ) = (x * x +
y * y * b + (x * y * 2) * sqrt( b ));仔细理解一下快速幂就可以自己推出快速幂了。
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<cmath> #define LL long long #define CLR(a, b) memset(a, b, sizeof(a)) using namespace std; const int N = 110; int main() { LL a, b, n, m, ansx, ansy, tx, ty, tmpx, tmpy; while(scanf("%I64d%I64d%I64d%I64d", &a, &b, &n, &m) != EOF) { ansx = 1;ansy = 0; tx = a;ty = 1; while(n) { if(n & 1) { tmpx = (ansx * tx + ansy * ty * b) % m; tmpy = (ansx * ty + ansy * tx) % m; ansx = tmpx;ansy = tmpy; } tmpx = (tx * tx + ty * ty * b) % m; tmpy = (tx * ty * 2) % m; tx = tmpx, ty = tmpy; n >>= 1; } printf("%I64d\n", ansx * 2 % m); } }
相关文章推荐
- 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
- HDU 4565 So Easy!(公式化简+矩阵)
- HDU 4565 SO easy!
- HDU - 4565 So Easy! 矩阵快速幂
- HDU 4565 -- So Easy! 数学 && 2013 ACM-ICPC 长沙赛区全国邀请赛 A题
- 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! 矩阵快速幂+共轭数