题意:
给出p=a+b ,q=a*b ,n
求 a^n+b^n
思路:
由于在矩阵快速幂的专题里
一开始就想
a2+b2 =(a+b)(a+b)-2ab
又往下推
发现
a3+b3=(a2+b2)(a+b)-a2b-ab2
=(a2+b2)(a+b)-(a+b)ab
又继续往下推
#include<bits/stdc++.h>
#define N 1000000007
#define M 2
struct node
{
long long a[M][M];
};
node cheng(node a,node b)
{
node c;
memset(c.a,0,sizeof(c.a));
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
for(int k=0;k<M;k++)
c.a[i][j]=(c.a[i][j]+(a.a[i][k]*b.a[k][j]));
return c;
}
long long f(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1) ans=(ans*a)%N;
a=(a*a)%N;
b>>=1;
}
return ans;
}
int main()
{
long long p,q,n;
node x,y;
while(~scanf("%lld%lld%lld",&p,&q,&n),p+q+n) //p=a+b q=a*b;
{
memset(x.a,0,sizeof(x.a));
memset(y.a,0,sizeof(y.a));
x.a[0][0]=p;
x.a[0][1]=2;
y.a[0][0]=p;
y.a[1][0]=-q;
y.a[0][1]=1;
if(n==0)
{
printf("2\n");
continue;
}
if(n==1)
{
printf("%lld\n",p);
continue;
}
n--;
while(n)
{
if(n&1) x=cheng(x,y);
y=cheng(y,y);
n>>=1;
}
printf("%lld\n",x.a[0][0]);
}
}