UVA 10655 Contemplation! Algebra(构造矩阵)

本文详细解析了UVA10655代数问题,通过矩阵构造的方法解决给定a+b及ab值求特定表达式的值的问题。介绍了递推式和矩阵求解的具体步骤。

此文章可以使用目录功能哟↑(点击上方[+])

 UVA Problem 10655 Contemplation! Algebra

Accept: 0    Submit: 0
Time Limit: 3000 MS

 Problem Description

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

 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 .


 Output

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

 Sample Input

10 16 2
7 12 3
0 0

 Sample Output

68
91

 Problem Idea

解题思路:

【题意】
给你a+b,ab,n的值,求


【类型】
矩阵构造

【分析】
首先,给你a+b,ab,可能第一想法是a,b为方程的两个根(韦达定理根深蒂固)

但是此题若是把a,b求解出来显然是不可取的

①方程不一定有实根

②就算是实根,精度不高

那么,换个思路,对进行化简,令f(n)=,则


就这样,递推式出来了,那么就可以转换成矩阵来求解


p=a+b;q=ab

特判n=0,n=1,n=2的情况,貌似n=2可以不用特判

n=0时,

n=1时,a+b=p

n=2时,(a+b)*(a+b)-2ab=p*p-2*q

另外,题目要求当且仅当输入的一行仅有0 0时,输入结束

故只判断p!=0&&q!=0是不对的,因为数据0 0 n(n≠0)是合法的

【时间复杂度&&优化】
O(logn)

题目链接→UVA 10655 Contemplation! Algebra

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-8
#define LL long long
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 2;
const int M = 100005;
const int inf = 1000000007;
const int mod = 10;
typedef struct node
{
	long long a[N][N];
	void Init()
	{
		memset(a,0,sizeof(a));
		for(int i=0;i<N;i++)
			a[i][i]=1;
	}
}matrix;
matrix mul(matrix a,matrix b)//矩阵乘法
{
	matrix ans;
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
		{
			ans.a[i][j]=0;
			for(int k=0;k<N;k++)
				ans.a[i][j]+=a.a[i][k]*b.a[k][j];
		}
	return ans;
}
matrix add(matrix a,matrix b)//矩阵加法
{
	matrix ans;
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			ans.a[i][j]=a.a[i][j]+b.a[i][j];
	return ans;
}
matrix pow(matrix a,int n)//求a^n
{
	matrix ans;
	ans.Init();
	while(n)
	{
		if(n%2)
			ans=mul(ans,a);
		n/=2;
		a=mul(a,a);
	}
	return ans;
}
int main()
{
    long long p,q,n;
    matrix ans,s;
    while(scanf("%lld%lld%lld",&p,&q,&n)==3)
    {
        if(n==0)
        {
            puts("2");
            continue;
        }
        if(n==1)
        {
            printf("%lld\n",p);
            continue;
        }
        if(n==2)
        {
            printf("%lld\n",p*p-2*q);
            continue;
        }
        s.a[0][0]=0;s.a[0][1]=1;
        s.a[1][0]=-q;s.a[1][1]=p;
        ans.a[0][0]=p;ans.a[0][1]=0;
        ans.a[1][0]=p*p-2*q;ans.a[1][1]=0;
        ans=mul(pow(s,n-1),ans);
        printf("%lld\n",ans.a[0][0]);
    }
    return 0;
}

菜鸟成长记

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值