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 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 an+bn. You can always assume that an+bn fits in a signed 64-bit integer.
Sample Input Output for Sample Input
10 16 2
7 12 3
0 0
68
91
Problem setter: Shahriar Manzoor, Member of Elite Problemsetters' Panel
Special Thanks: Mohammad Sajjad Hossain
題意很明顯就是求a^n+b^n;
我們發現f0=2,f1=a+b,f2=a^2+b^2=(a+b)*f1-a*b*f2....
依次遞推,令p=a+b,q=a*b;
所以fi=fi-1*p-fi-2*q;
構造出矩陣後就可以求解了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
HDU 4565
So Easy!
Problem Description A sequence S
n is defined as:
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S
n.
You, a top coder, say: So easy!
Input There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 2
15, (a-1)
2< b < a
2, 0 < b, n < 2
31.The input will finish with the end of file.
Output For each the case, output an integer S
n.
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
Sample Output
4
14
4
題目要求這個式子答案,我們發現(a-1)^2<b<a^2這就意味著a-1<sqrt(b)<a; 所以又(a+sqrt(b))^n+(a-sqrt(b))^n為整數,所以式子答案即為(a+sqrt(b))^n+(a-sqrt(b))^n="" 簡化下,就是上面的a'="a+sqrt(b),b'=a-sqrt(b);所以p=2*a,q=a*a-b;" 問題轉化為上題做法。="" #include #include
#include #include #include #include #include #include #include