Contemplation! Algebra UVA - 10655 (矩阵快速幂)

Given the value of a+b and ab you will have to nd the value of
a
n
+
b
n
Input
The input le contains several lines of inputs. Each line except the last line contains 3 non-negative
integers
p
,
q
and
n
. Here
p
denotes the value of
a
+
b
and
q
denotes the value of
ab
. Input is terminated
by a line containing only two zeroes. This line should not be processed. Each number in the input le
ts in a signed 32-bit integer. There will be no such input so that you have to nd the value of 0
0
.
Output
For each line of input except the last one produce one line of output. This line contains the value of
a
n
+
b
n
. You can always assume that
a
n
+
b
n
ts in a signed 64-bit integer.
Sample Input
10 16 2
7 12 3
0 0
Sample Output
68
91
题意:已知p=a+b;q=a*b;求a^n+b^n=ans? 注意a,b是实数哦!还有题目说不用考虑0^0这种情况!
那么分析一下:n = 0 ,ans0 = 2 ;
       n = 1 ,ans1 = a + b = p ;
       n = 2 ,ans2 = p * p - 2 * q = ans1 * p - ans0 * q;
  同理   n = 3 ,ans3 = ans2 * p - ans1 * q;    
       n = 4 , …….
       ………………
所以矩阵为
| p 1|
|-q 0|

思路借鉴了某大神博客:某大神博客

#include<cstring>//用c++的输入就过了。
#include<cstdio>
#include<iostream>
using namespace std;

#define LL long long

struct matrix
{
    LL mat[2][2];
};
matrix multiply(matrix a,matrix b)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            if(a.mat[i][j]==0)continue;
            for(int k=0;k<2;k++)
            {
                if(b.mat[j][k]==0)continue;
                c.mat[i][k]+=a.mat[i][j]*b.mat[j][k];
            }
        }
    }
    return c;
}

matrix quickmod(matrix a,LL m)
{
    matrix res;
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            res.mat[i][j]=(i==j);
    while(m)
    {
        if(m&1) res=multiply(res,a);
        m>>=1;
        a=multiply(a,a);
    }
    return res;
}

int main()
{
    LL p,q,n;
 //   while(scanf("%lld%lld%lld",&p,&q,&n),p+q+n)
    while(cin>>p>>q>>n)
    {
  //      if(!n&&(!p||!q)) break;

 //       scanf("%lld",&n);
        if(n==0)printf("2\n");//cout<<2<<endl;//
        else if(n==1)printf("%lld\n",p);// cout<<p<<endl;//
        else if(n==2) printf("%lld\n",p*p-2*q);//cout<<p*p-2*q<<endl;//
        else
        {
            //初始矩阵(p*p-2*q,p)
            matrix ans;
            ans.mat[0][0]=p;
            ans.mat[0][1]=1;
            ans.mat[1][0]=-q;
            ans.mat[1][1]=0;

            ans=quickmod(ans,n-1);
            LL ant=p*ans.mat[0][0]+2*ans.mat[1][0];
 //           cout<<ant<<endl;
            printf("%lld\n",ant);
//            printf("%lld\n",(p*ans.mat[0][0]+2*ans.mat[1][0]));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值