Contemplation! Algebra

      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

附:debug:

input:

0 0 0
0 0 1
0 0 200000000
1 3 0
999999 999999 0
1 0 200000000
2 0 61
2 1 0
2 1 100
2 1 200000000
0 0 0
0 1 1
0 1 2
0 1 3
0 1 199999999
0 1 200000000
0 1 200000001
0 1 200000002
5 6 7
10 20 10
90 87 3
999 231 2
0 0

output:

2
0
0
2
2
1
2305843009213693952
2
2
2
2
0
-2
0
0
2
0
-2
2315
393600000
705510
997539

题意:输入p,q,n;p=a+b,q=a*b,输出a^n+b^nTIP:令f(n)=a^n+b^n;则f(n)*(a+b)=a^(n+1)+b^(n+1)+a*b^n+b*a^n                    =f(n+1)+a*b*f(n-1)  即:f(n+1)=-(a+b)*f(n)+a*b*f(n-1)好了,矩阵上场,对于形如f(n+1)=p*f(n)+q*f(n-1)的;都可以转换为(p,q)                                  (1,0)…………%…………这是个2*2的矩阵

然后{f(n),f(n-1)}……(此为1*2的矩阵)=(那个矩阵)^(n-2)*{f(2),f(1)};

注意:如果输入0,0,2;你的程序运行不?

  scanf(……………………==3),我的如果不写的话就超时。

代码:

 1 ///a^(n+1)+b^(n+1)=(a^n+b^n)p−(a^(n−1)+b^(n−1))
 2 #include<string.h>
 3 #include<stdio.h>
 4
 5 long long p,q,n;
 6
 7 struct mat
 8 {
 9     long long v[2][2];
10     mat(){memset(v,0,sizeof(v));}
11     mat operator * (mat c)
12     {
13         mat ans;
14         for(int i=0;i<2;i++)///矩阵乘法再加上原有的原数
15         {
16             for(int j=0;j<2;j++)
17             {
18                 for(int k=0;k<2;k++)
19                 {
20                     ans.v[i][j]=ans.v[i][j]+v[i][k]*c.v[k][j];
21                 }
22                }
23           }
24           return ans;
25      }
26 };
27
28 mat pow_mod(mat x,long long k)
29 {
30     mat ans;
31     ans.v[0][0]=ans.v[1][1]=1;
32
33     while(k)///二分求定以后的*
34     {
35         if(k&1)
36             ans=ans*x;
37         x=x*x;
38         k>>=1;
39      }
40
41      return ans;
42 }
43
44 long long solve()
45 {
46     if(n==0)
47         return 2;
48     if(n==1)
49         return p;
50     if(n==2)
51         return p*p-2*q;
52
53     mat a;
54     a.v[0][0]=p;
55     a.v[0][1]=-q;
56     a.v[1][0]=1;
57     a=pow_mod(a,n-2);
58
59     return a.v[0][1]*p+a.v[0][0]*(p*p-2*q);
60 }
61
62 int main()
63 {
64     while(scanf("%lld%lld%lld",&p,&q,&n)==3)
65     {
66         printf("%lld\n",solve());
67     }
68     return 0;
69 }
时间: 2024-09-29 23:05:55

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

【UVA10655】 Contemplation! Algebra

题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) 较小的情况,可以很容易的想到递推: \[ \begin{array}{} \text{令} F(i) & = a ^ n + b ^ n \ & = (a + b)(a ^ {n - 1} + b ^ {n - 1}) - (ab ^ {n - 1} + a^{n - 1}b) \ &

UVA 10655 Contemplation! Algebra(矩阵快速幂)

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 line contains 3 non-negative integers p, q and n. Here p denotes the value of a+b andq denotes the

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 denote

UVA 10655 Contemplation! Algebra

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 line contains 3 non-negative integers p, q and n. Here p denotes the value of a+b and q denotes th

UVA 10655 Contemplation! Algebra (矩阵快速幂)

题目链接:传送门 题意: 给定你三个数,p,q,n, p代表的是 a + b, q代表的是a*b; 然后求a^n + b^n 设f[i] = a^i +b^i; f[0]=2,f[1]=p; f[i]*(a+b) = a^(i+1) + b^(i+1) +a^i*b + b^i*a; f[i]*p = f[i+1] + a*b*[ a^(i-1) + b^(i-1) ] f[i+1] = f[i]*p + q*f[i-1]; 然后用矩阵加速一下就可以了(ps.这个题的输入非常坑....) 代码如

UVA-10655 Contemplation! Algebra (矩阵)

题目大意:给出a+b的值和ab的值,求a^n+b^n的值. 题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出a和b,进而求出答案.这种方法之所以错,是因为这种方法有局限性.联立之后会得到一个二元一次方程,只有当该方程有实数解确切的说是当某个数据满足该方程有实数解时,这种方法得到的结果才有可能正确.显然,题中数据不可能这么片面.正确的方法是这样的: 令a+b=A,ab=B,S(n)=an+bn.则S(n)=an+bn=(a+b)(an-1+bn-1)-abn-1-an-1b=(a+

A Linear Algebra Problem(唯一性的判定)

A Linear Algebra Problem Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status God Kufeng is the God of Math. However, Kufeng is not so skilled with linear algebra, especially when dealing with matrixes. On