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 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 a n + b n. You can always assume that a n + b n fits in a signed 64-bit integer.

Sample Input

10 16 2 7 12 3 0 0

Sample Output

68 91

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<string>
using namespace std;
typedef long long  LL;
typedef unsigned long long ULL;
#define MAXN 49
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-9;
/*
矩阵快速幂!
an = p an - q an-1
*/
LL p, q, n;
struct Mat
{
    Mat()
    {
        memset(a, 0, sizeof(a));
    }
    LL a[2][2];
    Mat operator * (const Mat& rhs)
    {
        Mat ret;
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                if (a[i][j])
                {
                    for (int k = 0; k < 2; k++)
                        ret.a[i][k] += a[i][j] * rhs.a[j][k];
                }
            }
        }
        return ret;
    }
};
Mat fpow(Mat m, LL b)
{
    Mat tmp = m, ans;
    ans.a[1][1] = ans.a[0][0] = 1;
    while (b != 0)
    {
        if (b & 1)
            ans = tmp * ans;
        tmp = tmp * tmp;
        b /= 2;
    }
    return ans;
}
int main()
{
    while (scanf("%lld%lld%lld", &p, &q, &n) == 3)
    {

        LL a1 = p, a2 = p*p - 2 * q;
        if (n == 0)
            printf("2\n");
        else if (n == 1)
            printf("%lld\n", a1);
        else if (n == 2)
            printf("%lld\n", a2);
        else
        {
            n = n - 2;
            Mat M;
            M.a[0][0] = p, M.a[0][1] = -q, M.a[1][0] = 1;
            M = fpow(M, n);
            printf("%lld\n", M.a[0][0] * a2 + M.a[0][1] * a1);
        }
    }
}
时间: 2024-08-05 11:14:53

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(矩阵快速幂,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

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21