Recursive formulas are obtained by calculation
The direct recursion calculation should time out (not try), the better method is to convert to a matrix and then use the matrix to optimize the power quickly.
That is, N>=1,
Also, be aware of the end of the input, "input is Terminatedby a line containing only two zeroes. ”
1#include <iostream>2#include <algorithm>3#include <map>4#include <vector>5#include <functional>6#include <string>7#include <cstring>8#include <queue>9#include <Set>Ten#include <cmath> One#include <cstdio> A using namespacestd; - #defineIOS Ios_base::sync_with_stdio (false)] -typedefLong LongLL; the Const intinf=0x3f3f3f3f; - - Const intmaxn=2; -typedefstructmatrix{ + LL V[MAXN][MAXN]; - voidInit () {memset (V,0,sizeof(v));} + }m; AM Mul (ConstM &a,ConstM &b,intLintMintN) at { - M C; c.init (); - for(intI=0; i<l;i++){ - for(intj=0; j<n;j++){ - for(intk=0; k<m;k++)//Note J,k Range -c.v[i][j]+=a.v[i][k]*B.v[k][j]; in } - } to returnC; + } -M Power (M x,intl,ll p) the { * M tmp; Tmp.init (); $ for(intI=0; i<l;i++)Panax Notoginsengtmp.v[i][i]=1; - while(p) { the if(p&1) tmp=Mul (x,tmp,l,l,l); +x=Mul (x,x,l,l,l); Ap>>=1; the } + returntmp; - } $ intMain () $ { - LL p,q,n; - M s,t; the while(SCANF ("%lld%lld%lld", &p,&q,&n) = =3){ - if(n==0) {puts ("2");Continue;};Wuyi s.init (); thes.v[0][0]=p; -s.v[1][0]=2; Wu t.init (); -t.v[0][0]=p; Aboutt.v[0][1]=-Q; $t.v[1][0]=1; -T=power (T,2, N-1); -T=mul (T,s,2,2,1); -printf"%lld\n", t.v[0][0]); A } +}
UVA 10655 contemplation! Algebra