URAL 1133. Fibonacci Sequence
1 #include <cstdio> 2 #include <string> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 #define LL long long 7 #define INF 2000000000 8 LL p[3001]; 9 int main() 10 { 11 int i; 12 LL a,fa,fb,b,n; 13 LL str,mid,end; 14 cin>>a>>fa>>b>>fb>>n; 15 if(a > b) 16 { 17 swap(a,b); 18 swap(fa,fb); 19 } 20 str = -INF; 21 end = INF; 22 while(str <= end) 23 { 24 mid = (str+end)/2; 25 int f1,f2; 26 f1 = f2 = 0; 27 memset(p,-1,sizeof(p)); 28 p[a+1000] = fa; 29 p[a+1+1000] = mid; 30 for(i = a+2;i <= b;i ++) 31 { 32 p[i+1000] = p[i-1+1000] + p[i-2+1000]; 33 if(p[i+1000] > INF) 34 { 35 f1 = 1; 36 break; 37 } 38 if(p[i+1000] < -INF) 39 { 40 f2 = 1; 41 break; 42 } 43 } 44 if(f1) 45 end = mid - 1; 46 else if(f2) 47 str = mid + 1; 48 else if(p[b+1000] == fb) 49 break; 50 else if(p[b+1000] > fb) 51 end = mid - 1; 52 else if(p[b+1000] < fb) 53 str = mid + 1; 54 } 55 if(n <= b&&n >= a) 56 ; 57 else if(n < a) 58 { 59 p[a+1000] = fa; 60 p[a+1+1000] = mid; 61 for(i = a-1;i >= n;i --) 62 { 63 p[i+1000] = p[i+2+1000] + p[i+1+1000]; 64 } 65 } 66 else 67 { 68 p[a+1000] = fa; 69 p[a+1+1000] = mid; 70 for(i = a+2;i <= n;i ++) 71 { 72 p[i+1000] = p[i-1+1000] + p[i-2+1000]; 73 } 74 } 75 cout<<p[n+1000]<<endl; 76 return 0; 77 }