Advertisement
Kaidul

uva - 495

Dec 25th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.12 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <complex>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <ctime>
  10. #include <deque>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <list>
  14. #include <map>
  15. #include <memory>
  16. #include <queue>
  17. #include <set>
  18. #include <sstream>
  19. #include <stack>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include <iomanip>
  24.  
  25. using namespace std;
  26.  
  27. #define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
  28. #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
  29. #define RFOR(i,a,b) for(__typeof(b) i=(a); i>(b); i--)
  30. #define RESET(t,value) memset((t), value, sizeof(t))
  31. typedef long long int64;
  32. typedef long double d64;
  33. #define READ(f) freopen(f, "r", stdin)
  34. #define WRITE(f) freopen(f, "w", stdout)
  35. #define PI acos(-1.0)
  36. #define INF (1<<30)
  37. #define eps 1e-8
  38. #define pb push_back
  39. #define ppb pop_back
  40. #define pii pair<double,double>
  41. #define G struct node
  42.  
  43. template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
  44. template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
  45. template< class T > void setmax(T &a, T b) { if(a < b) a = b; }
  46. template< class T > void setmin(T &a, T b) { if(b < a) a = b; }
  47.  
  48. vector < int > pset;
  49. void initSet(int _size){ pset.resize(_size); FOR(i,0,_size-1) pset[i]=i;}
  50. int findSet(int i){return (pset[i]== i)?i: (pset[i] = findSet(pset[i]));}
  51. void unionSet(int i,int j){ pset[findSet(i)]=findSet(j);}
  52. bool isSameSet(int i,int j){ return findSet(i)==findSet(j);}
  53.  
  54. #define MAX 5000
  55.  
  56. struct Bigint {
  57.     // representations and structures
  58.     string a; // to store the digits
  59.     int sign; // sign = -1 for negative numbers, sign = 1 otherwise
  60.  
  61.     // constructors
  62.     Bigint() {} // default constructor
  63.     Bigint( string b ) { (*this) = b; } // constructor for string
  64.  
  65.     // some helpful methods
  66.     int size() { // returns number of digits
  67.         return a.size();
  68.     }
  69.     Bigint inverseSign() { // changes the sign
  70.         sign *= -1;
  71.         return (*this);
  72.     }
  73.     Bigint normalize( int newSign ) { // removes leading 0, fixes sign
  74.         for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
  75.             a.erase(a.begin() + i);
  76.         sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
  77.         return (*this);
  78.     }
  79.  
  80.     // assignment operator
  81.     void operator = ( string b ) { // assigns a string to Bigint
  82.         a = b[0] == '-' ? b.substr(1) : b;
  83.         reverse( a.begin(), a.end() );
  84.         this->normalize( b[0] == '-' ? -1 : 1 );
  85.     }
  86.  
  87.     // conditional operators
  88.     bool operator < ( const Bigint &b ) const { // less than operator
  89.         if( sign != b.sign ) return sign < b.sign;
  90.         if( a.size() != b.a.size() )
  91.             return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
  92.         for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
  93.             return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
  94.         return false;
  95.     }
  96.     bool operator == ( const Bigint &b ) const { // operator for equality
  97.         return a == b.a && sign == b.sign;
  98.     }
  99.  
  100.  
  101.  
  102.     // mathematical operators
  103.     Bigint operator + ( Bigint b ) { // addition operator overloading
  104.         if( sign != b.sign ) return (*this) - b.inverseSign();
  105.         Bigint c;
  106.         for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
  107.             carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
  108.             c.a += (carry % 10 + 48);
  109.             carry /= 10;
  110.         }
  111.         return c.normalize(sign);
  112.     }
  113.     Bigint operator - ( Bigint b ) { // subtraction operator overloading
  114.         if( sign != b.sign ) return (*this) + b.inverseSign();
  115.         int s = sign; sign = b.sign = 1;
  116.         if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
  117.         Bigint c;
  118.         for( int i = 0, borrow = 0; i < a.size(); i++ ) {
  119.             borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
  120.             c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
  121.             borrow = borrow >= 0 ? 0 : 1;
  122.         }
  123.         return c.normalize(s);
  124.     }
  125.     Bigint operator * ( Bigint b ) { // multiplication operator overloading
  126.         Bigint c("0");
  127.         for( int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48 ) {
  128.             while(k--) c = c + b; // ith digit is k, so, we add k times
  129.             b.a.insert(b.a.begin(), '0'); // multiplied by 10
  130.         }
  131.         return c.normalize(sign * b.sign);
  132.     }
  133.     Bigint operator / ( Bigint b ) { // division operator overloading
  134.         if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  135.         Bigint c("0"), d;
  136.         for( int j = 0; j < a.size(); j++ ) d.a += "0";
  137.         int dSign = sign * b.sign; b.sign = 1;
  138.         for( int i = a.size() - 1; i >= 0; i-- ) {
  139.             c.a.insert( c.a.begin(), '0');
  140.             c = c + a.substr( i, 1 );
  141.             while( !( c < b ) ) c = c - b, d.a[i]++;
  142.         }
  143.         return d.normalize(dSign);
  144.     }
  145.     Bigint operator % ( Bigint b ) { // modulo operator overloading
  146.         if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  147.         Bigint c("0");
  148.         b.sign = 1;
  149.         for( int i = a.size() - 1; i >= 0; i-- ) {
  150.             c.a.insert( c.a.begin(), '0');
  151.             c = c + a.substr( i, 1 );
  152.             while( !( c < b ) ) c = c - b;
  153.         }
  154.         return c.normalize(sign);
  155.     }
  156.  
  157.  
  158.  
  159.     // output method
  160.     void print() {
  161.         if( sign == -1 ) putchar('-');
  162.         for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
  163.     }
  164. };
  165.  
  166. Bigint dp[MAX+1];
  167.  
  168. Bigint fib(int n) {
  169.     if(n == 0) {
  170.         dp[n] = "0";
  171.         return dp[n];
  172.     }
  173.     if(n == 1) {
  174.         dp[n] = "1";
  175.         return dp[n];
  176.     }
  177.     if(dp[n] < Bigint("0")) {
  178.         dp[n] = fib(n-1) + fib(n-2);
  179.         return dp[n];
  180.     } else {
  181.         return dp[n];
  182.     }
  183.  
  184. }
  185.  
  186. int main()
  187. {
  188.     //READ("input.txt");
  189.     Bigint minusOne = Bigint("-1");
  190.     REP(i, MAX) dp[i] = minusOne;
  191.     fib(MAX);
  192.     int n;
  193.     while(cin>>n) {
  194.         cout<<"The Fibonacci number for "<<n<<" is ";
  195.         dp[n].print();
  196.         cout<<"\n";
  197.     }
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement