您的位置:首页 > 其它

ZSTU 3194 复数的幂 二分幂

2012-12-16 14:38 225 查看
递归

View Code

#include <cstdio>
#include <cstring>
#define LL __int64
LL a, b, n, mod;
struct complex
{
LL a, b;
complex(){}
complex(LL a, LL b):a(a%mod),b(b%mod){}
complex operator *(const complex &p)
{
return complex((a*p.a-b*p.b)%mod, (a*p.b+b*p.a)%mod);
}
};
void gao(LL n, complex &p)
{
if(n == 1)
{
p = complex(a%mod, b%mod);
return;
}
complex q;
gao(n>>1, q);
p = q * q;
if(n & 1)
{
complex t = complex(a%mod, b%mod);
p = p * t;
}
}
int main()
{
int i, j;
while( ~scanf("%I64d%I64d%I64d%I64d", &a, &b, &n, &mod))
{
if(!n)
{
printf("1 0\n");
continue;
}
complex p;
gao(n, p);
if(p.a < 0) p.a += mod;
if(p.b < 0) p.b += mod;
printf("%I64d %I64d\n", p.a, p.b);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: