LOJ1070:Algebraic Problem(矩阵快速幂 & 数学 a^n+b^n)

1070 - Algebraic Problem
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+bna and b not necessarily have to be real numbers.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains three non-negative integers, p, q and n. Here p denotes the value of a+b and q denotes the value of ab. 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 test case, print the case number and (an+bn) modulo 264.

Sample Input

Output for Sample Input

2

10 16 2

7 12 3

Case 1: 68

Case 2: 91


PROBLEM SETTER: SHAHRIAR MANZOOR
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)

 题意:给a+b和ab的值,求a^n+b^n的值。

思路:a,b可以非实数,不考虑解他们的值,因为有:a^n+b^n=(a+b)(a^(n-1)+b^(n-1))-(a*b)a^(n-2)+b^(n-2))。


显然可以用矩阵乘法推出来,取模2^64,unsigned long long类型可以做到。

# include <iostream>
# include <algorithm>
# include <cstdio>
using namespace std;
//god & me
typedef unsigned long long ull;
struct Mat{ull mat[2][2];};
Mat operator *(Mat a, Mat b)
{
    Mat c;
    for(int i=0; i<2; ++i)
        for(int j=0; j<2; ++j)
        {
            c.mat[i][j] = 0;
            for(int k=0; k<2; ++k)
                c.mat[i][j] += a.mat[i][k]*b.mat[k][j];
        }
    return c;
}

Mat operator ^(Mat a, ull b)
{
    Mat c;
    for(int i=0; i<2; ++i)
        for(int j=0; j<2; ++j)
        c.mat[i][j] = (i==j);
    for(;b;b>>=1)
    {
        if(b&1) c = c*a;
        a = a*a;
    }
    return c;
}
int main()
{
    ull p, q, n;
    int t, cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%llu%llu%llu",&p,&q,&n);
        if(n == 0)
        {
            printf("Case %d: 2\n",cas++,p);
            continue;
        }
        Mat ret;
        ret.mat[0][0] = p;
        ret.mat[0][1] = -q;
        ret.mat[1][0] = 1;
        ret.mat[1][1] = 0;
        ret = ret^(n-1);
        printf("Case %d: %llu\n",cas++,ret.mat[0][0]*p+ret.mat[0][1]*2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值