UVA 10655 Contemplation! Algebra

Given the value of a+b and ab you will have to find the value of an+bn

Input

The input file contains several lines of inputs. Each line except the last line contains 3 non-negative integers pq 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 file fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.

Output

For each line of input except the last one produce one line of output. This line contains the value of an+bn.  You can always assume that an+bnfits in a signed 64-bit integer.

可以推出a ^ n + b ^ n = (a + b)[a ^ ( n - 1) + b ^ (n - 1)] - ab[a ^ (n - 2) + b ^ (n - 2)]

    #include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
struct matrix
{
    LL f[2][2];
};
matrix mul(matrix a,matrix b)
{
    LL i,j,k;
    matrix c;
    memset(c.f,0,sizeof(c.f));
    for(i=0;i<2;i++)
        for(j=0;j<2;j++)
            for(k=0;k<2;k++)
            c.f[i][j]+=a.f[i][k]*b.f[k][j];
    return c;
}
matrix ksm(matrix e,LL n)
{
    matrix s;
    s.f[0][0]=s.f[1][1]=1;
    s.f[0][1]=s.f[1][0]=0;
    while(n)
    {
        if(n&1)
            s=mul(s,e);
        e=mul(e,e);
        n=n>>1;
    }
    return s;
}
matrix e;
int main()
{
    LL p,q,n;
    while(cin>>p>>q>>n)
    {
        if(n==0)
        {
            cout<<2<<endl;
            continue;
        }
        e.f[0][0]=p;e.f[0][1]=1;
        e.f[1][0]=-q;e.f[1][1]=0;
        e=ksm(e,n-1);
        LL ans;
        ans=p*e.f[0][0]+2*e.f[1][0];
        cout<<ans<<endl;
    }
    return 0;
}

  

时间: 2024-10-10 03:25:11

UVA 10655 Contemplation! Algebra的相关文章

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

UVA 10655 - Contemplation! Algebra 题目链接 题意:给定p, q, n代表p=a+b,q=ab求an+bn 思路:矩阵快速幂,公式变换一下得到(an+bn)(a+b)=an+1+bn+1+ab(an?1+bn?1),移项一下得到an+1+bn+1=(an+bn)p?q(an?1+bn?1) 这样就可以用矩阵快速幂求解了 代码: #include <stdio.h> #include <string.h> long long p, q, n; str

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

Given the value of a+b and ab you will have to find the value of an+bn Input The input file 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 andq denotes the

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

题目链接:传送门 题意: 给定你三个数,p,q,n, p代表的是 a + b, q代表的是a*b; 然后求a^n + b^n 设f[i] = a^i +b^i; f[0]=2,f[1]=p; f[i]*(a+b) = a^(i+1) + b^(i+1) +a^i*b + b^i*a; f[i]*p = f[i+1] + a*b*[ a^(i-1) + b^(i-1) ] f[i+1] = f[i]*p + q*f[i-1]; 然后用矩阵加速一下就可以了(ps.这个题的输入非常坑....) 代码如

Contemplation! Algebra(矩阵快速幂,uva10655)

Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Given the value of a+b and ab you will have to find the value of an+bn Input The input file contains several lines of inputs. Each line except the last

Contemplation! Algebra

Contemplation! Algebra Given the value of a+b and ab you will have to find the value of an + bnInputThe input file contains several lines of inputs. Each line except the last line contains 3 non-negativeintegers p, q and n. Here p denotes the value o

【UVA10655】 Contemplation! Algebra

题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) 较小的情况,可以很容易的想到递推: \[ \begin{array}{} \text{令} F(i) & = a ^ n + b ^ n \ & = (a + b)(a ^ {n - 1} + b ^ {n - 1}) - (ab ^ {n - 1} + a^{n - 1}b) \ &

Contemplation! Algebra 矩阵快速幂

Given the value of a+b and ab you will have to find the value of a n + b n Input The input file 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 denote

UVA-10655 Contemplation! Algebra (矩阵)

题目大意:给出a+b的值和ab的值,求a^n+b^n的值. 题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出a和b,进而求出答案.这种方法之所以错,是因为这种方法有局限性.联立之后会得到一个二元一次方程,只有当该方程有实数解确切的说是当某个数据满足该方程有实数解时,这种方法得到的结果才有可能正确.显然,题中数据不可能这么片面.正确的方法是这样的: 令a+b=A,ab=B,S(n)=an+bn.则S(n)=an+bn=(a+b)(an-1+bn-1)-abn-1-an-1b=(a+

矩阵专题(续)

哈哈哈,失踪人员回归.最近总是在忙一些乱七八糟的事情,终于有了一段空闲时间可以刷刷题了.学姐给我准备了kuangbin先生的矩阵专题,作为我的矩阵终结专题.(初学者请先看我的矩阵专题前三篇) 第一题 CodeForces 450B 分析:这道题看似是一道矩阵题,但是实际上是一道模拟题.看题目给出的公式 f(i)=f(i-1)+f(i+1),i>=2,那么转换一下得到了f(i)=f(i-1)-f(i-2),i>=3,当然这题可以使用矩阵快速幂来算,但是多此一举.不妨将f(1)=x  f(2)=y