UVA 10655 Contemplation! Algebra(构造矩阵)

2019-04-14 20:49发布

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

 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #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 菜鸟成长记