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.这个题的输入非常坑。。。。)

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define IFOR(i,h,l,v) for(int i=(h);i<=(l);i+=(v))
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 1000000000
#define LINF 1000000000000000000LL
#define eps 1e-8
#define LL long long
using namespace std;

const int maxn = 2;

struct matrix{
    LL a[maxn][maxn];
    matrix operator *(matrix b){
        matrix ans;
        REP(i,maxn){
            REP(j,maxn){
                ans.a[i][j]=0;
                REP(k,maxn) ans.a[i][j]+=a[i][k]*b.a[k][j];
            }
        }
        return ans;
    }
};

matrix I={1,0,0,1};

matrix pow(matrix A,LL k){
    matrix ans = I;
    while(k){
        if(k&1) ans=ans*A;
        k>>=1;
        A=A*A;
    }
    return ans ;
}

int main()
{
    LL p,q,n;
    while(cin>>p>>q>>n){
        if(n==0){puts("2");continue;}
        LL f0=2,f1=p;
        matrix A={p,-q,1,0};
        matrix tmp = I*A;
        matrix ans = pow(A , n-1);
        cout<<ans.a[0][0]*f1+ans.a[0][1]*f0<<endl;
    }
    return 0;
}
时间: 2024-11-07 19:30:41

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

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

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

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 )

Uva 11551 - Experienced Endeavour ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 50 #define MOD 1000 #define CLR( a, b ) memset( a, b, sizeof(a) ) struct Mat { int n; int mat[MAX

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

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 and q denotes th

UVA - 10229 - Modular Fibonacci (矩阵快速幂 + fibonacci)

题目传送:UVA - 10229 思路:就是简单的矩阵快速幂求fibonacci数列,然后注意可能中间结果会爆int,因为2^19有50多万 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #includ

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

UVA 11651 - Krypton Number System(DP+矩阵快速幂)

UVA 11651 - Krypton Number System 题目链接 题意:给一个进制base,一个分数score求该进制下,有多少数满足一下条件: 1.没有连续数字 2.没有前导零 3.分数为score,分数的计算方式为相邻数字的平方差的和 思路:先从dp入手,dp[i][j]表示组成i,最后一个数字为j的种数,然后进行状态转移,推出前面一步能构成的状态,也就是到dp[(b - 1) * (b - 1)][x]. 然后可以发现后面的状态,都可以由前面这些状态统一转移出来,这样就可以利用