[UVA][循] 12464 - Professor Lazy, Ph.D.@Morris' Blog|PChome Online 人新台
2013-06-05 10:38:47| 人527| 回0 | 上一篇 | 下一篇

[UVA][循] 12464 - Professor Lazy, Ph.D.

0 收藏 0 0 站台

Problem D. Professor Lazy, Ph.D.

Professors are very motivated when they have to travel abroad for a conference (of course, if fees are paid by the university), but they don’t have the same attitude when the moment to grade exams arrives.

Professor Lazy, Ph.D., has a particular way to grade exams (and very unfair, by the way). He puts all his exams in a box and then starts getting them out one by one in a totally random fashion. He assigns grade α to the first exam that he gets out of the box, and grade β to the second exam that he gets out of the box. From that point on, he assigns a grade to each of the exams based on the grades of the previous two exams. What he does is that he takes the grade of the immediately previous exam, adds 1 and divides by the grade of the exam before the previous one.

For example, let’s imagine that α = 2 and β = 3. This is what happens:

  • The first exam gets α = 2.
  • The second exam gets β = 3.
  • The previous two grades are α and β, so the third exam gets (1 + β) ------- α = (1 + 3) ------- 2 = 2.
  • The previous two grades are β and (1+-β)- α, so the fourth exam gets 1-+-(1+αβ) β = 1+-2- 3 = 1.
  • The procedure continues until he’s done with all exams.

More formally, we can define the grade Qn of the nth exam with a recurrence like this:

Qn = ( || α if n = 0, |{ β if n = 1, |||( 1-+-Qn--1 Qn -2 if n ≥ 2.

Even this simple procedure is a lot of work for Professor Lazy, Ph.D., so he asks you to write a program to do it for him. He wants to spend all day long drinking coffee in the cafeteria with other professors. Given α, β and n find the value of Qn.

Note that the grades do notnecessarily lie inside a fixed range. They are just arbitrary integers.

Input

The input contains several test cases (at most 1000). Each test case is described by three integer numbers α, β and n on a single line (1 α, β 109 and 0 n 1015).

The last line of the input contains three zeros and should not be processed.

Output

For each test case, write the value of Qn in a single line. The input will be such that the value of Qn is always an integer. Furthermore, Qi will never be zero for 0 i n (in other words, division by zero will never arise when evaluating the recurrence).

Sample input and output

standard input
standard output


1 1 0

1 2 1

5 9 2

2 3 3

7 4 4

2109 650790 344341899059516

45861686 57 594261603792314

2309734 21045930 808597262407955

0 0 0

1

2

2

1

2

650790

804591

2309734

台: Morris
人(527) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][greedy] 12261 - High Score
此分上一篇:[UVA][循] 11053 - Flavius Josephus Reloaded

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86