LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂

http://www.lightoj.com/volume_showproblem.php?problem=1070

思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \((ab)C_{n}^{r}a^{n-r}b{r} = C_{n+2}^{r}a^{n-r+2}b{r} - a^{n+2} - b^{n+2} \)

综上\( f(n) = (a+b)f(n-1)-(ab)f(n-2) \)

/** @Date    : 2016-12-19-19.53
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version :
  */
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
typedef unsigned long long ull;

struct matrix
{
    ull mt[2][2];
    void init()
    {
        for(int i = 0; i < 2; i++)
            for(int j = 0; j < 2; j++)
                mt[i][j] = 0;
    }
    void cig()
    {
        for(int i = 0; i < 2; i++)
            mt[i][i] = 1;
    }
};

matrix mul(matrix a, matrix b)
{
    matrix c;
    c.init();
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < 2; j++)
            for(int k = 0; k < 2; k++)
            {
                c.mt[i][j] += a.mt[i][k] * b.mt[k][j];
            }
    return c;
}

matrix fpow(matrix a, LL n)
{
    matrix r;
    r.init();
    r.cig();
    while(n > 0)
    {
        if(n & 1)
            r = mul(r, a);
        a = mul(a, a);
        n >>= 1;
    }
    return r;
}

ull fun(LL p, LL q, LL n)
{
    if(n < 1)
    {
        return 2;
    }
    matrix base;
    base.mt[0][0] = p;
    base.mt[0][1] = -q;
    base.mt[1][0] = 1;
    base.mt[1][1] = 0;
    base = fpow(base, n - 1);
    ull ans = base.mt[0][0] * p + base.mt[0][1] * 2;
    return ans;
}

int main()
{
    int T;
    int cnt = 0;
    cin >> T;
    while(T--)
    {
        LL n, p , q;
        scanf("%lld%lld%lld", &p, &q, &n);
        ull ans = fun(p, q, n);
        printf("Case %d: %llu\n", ++cnt, ans);
    }
    return 0;
}
//f(n) = (a+b)*f(n-1) - (ab)*f(n-2)
时间: 2024-11-11 02:19:39

LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂的相关文章

LOJ 1070 - Algebraic Problem(矩阵快速幂啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessarily have to be real numbers. Input Input starts with an integer T (≤ 10000), denoting the number o

LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^64,定义成unsigned long long 类型,因为无符号类型超过最大范围的数与该数%最大范围 的效果是一样的. AC代码: #include<stdio.h> #include<string.h> #define LL unsigned long long struct Ma

LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)

题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 得到递推式之后就是矩阵高速幂了 注意:模2^64,定

LightOJ 1070 - Algebraic Problem 矩阵快速幂

题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessar

dutacm.club Water Problem(矩阵快速幂)

Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:1228   Accepted:121 [Submit][Status][Discuss] Description 函数 f:Z+→Z .已知 f(1),f(2) 的值,且对于任意 x>1,有 f(x+1)=f(x)+f(x?1)+sin(πx2) . 求 f(n) 的

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

[ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了一发没过 上网看了一下才知道是快速幂 而且特征方程的推导简直精妙 尤其是共轭相抵消的构造 真的是太看能力了 (下图转自某大神博客) 特征方程是C^2=-2*a*C+(a*a-b) 然后用快速幂求解 临时学了下矩阵快速幂 从这道题能看出来 弄ACM真的要数学好 这不是学校认知的高数 线代 概率分数 而

HDU——4291A Short problem(矩阵快速幂+循环节)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2461    Accepted Submission(s): 864 Problem Description According to a research, VIM users tend to have shorter fingers, compared

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