您的位置:首页 > 其它

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 ));仔细理解一下快速幂就可以自己推出快速幂了。

#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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数学