Advertisement
Saleh127

UVA 1646 / BigINT

Oct 31st, 2022
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.29 KB | None | 0 0
  1. /***
  2.  created: 2022-10-31-19.18.10
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16. const int MAXD = 10000, DIG = 9, BASE = 1000000000;
  17.  
  18. const unsigned long long BOUND = numeric_limits <unsigned long long> :: max () - (unsigned long long) BASE * BASE;
  19.  
  20. class BigInteger
  21. {
  22. private:
  23.     int digits[MAXD];
  24.     int D;
  25. public:
  26.  
  27.     friend ostream &operator<<(ostream &out,BigInteger &c);
  28.  
  29.     inline void trim()
  30.     {
  31.         while(D > 1 && digits[D-1] == 0 )
  32.             D--;
  33.     }
  34.  
  35.     inline void dealint(long long x)
  36.     {
  37.         memset(digits,0,sizeof(digits));
  38.         D = 0;
  39.         do
  40.         {
  41.             digits[D++] = x % BASE;
  42.             x /= BASE;
  43.         }
  44.         while(x > 0);
  45.     }
  46.  
  47.     inline void dealstr(char *s)
  48.     {
  49.         memset(digits,0,sizeof(digits));
  50.         int len = strlen(s),first = (len + DIG -1)%DIG + 1;
  51.  
  52.         D = (len+DIG-1)/DIG;
  53.  
  54.         for(int i = 0; i < first; i++)
  55.             digits[D-1] = digits[D-1]*10 + s[i] - '0';
  56.  
  57.         for(int i = first, d = D-2; i < len; i+=DIG,d--)
  58.             for(int j = i; j < i+DIG; j++)
  59.                 digits[d] = digits[d]*10 + s[j]-'0';
  60.  
  61.         trim();
  62.     }
  63.  
  64.     inline char *print()
  65.     {
  66.         trim();
  67.  
  68.         char *cdigits = new char[DIG * D + 1];
  69.  
  70.         int pos = 0,d = digits[D-1];
  71.  
  72.         do
  73.         {
  74.             cdigits[pos++] = d % 10 + '0';
  75.             d/=10;
  76.         }
  77.         while(d > 0);
  78.  
  79.         reverse(cdigits,cdigits+pos);
  80.  
  81.         for(int i = D - 2; i >= 0; i--,pos += DIG)
  82.             for(int j = DIG-1,t = digits[i]; j >= 0; j--)
  83.             {
  84.                 cdigits[pos+j] = t%10 + '0';
  85.                 t /= 10;
  86.             }
  87.  
  88.         cdigits[pos] = '\0';
  89.  
  90.         return cdigits;
  91.     }
  92.  
  93.  
  94.     BigInteger()
  95.     {
  96.         dealint(0);
  97.     }
  98.  
  99.     BigInteger(long long x)
  100.     {
  101.         dealint(x);
  102.     }
  103.  
  104.     BigInteger(int x)
  105.     {
  106.         dealint(x);
  107.     }
  108.  
  109.     BigInteger(char *s)
  110.     {
  111.         dealstr(s);
  112.     }
  113.  
  114.  
  115.  
  116.     inline bool operator < (const BigInteger &o) const
  117.     {
  118.         if(D != o.D)
  119.             return D < o.D;
  120.  
  121.         for(int i = D-1; i>=0; i--)
  122.             if(digits[i] != o.digits[i])
  123.                 return digits[i] < o.digits[i];
  124.         return false; //equal
  125.  
  126.     }
  127.  
  128.     bool operator >  (const BigInteger & o)const
  129.     {
  130.         return o < *this;
  131.     }
  132.     bool operator <= (const BigInteger & o)const
  133.     {
  134.         return !(o < *this);
  135.     }
  136.     bool operator >= (const BigInteger & o)const
  137.     {
  138.         return !(*this < o);
  139.     }
  140.     bool operator != (const BigInteger & o)const
  141.     {
  142.         return o < *this || *this < o;
  143.     }
  144.     bool operator == (const BigInteger & o)const
  145.     {
  146.         return !(o < *this) && !(*this < o);
  147.     }
  148.  
  149.  
  150.     BigInteger &operator++()
  151.     {
  152.         *this = *this  + 1;
  153.         return *this;
  154.     }
  155.  
  156.  
  157.     BigInteger operator ++(int)
  158.     {
  159.         BigInteger old = *this;
  160.         ++(*this);
  161.         return old;
  162.     }
  163.  
  164.     inline BigInteger operator << (int p) const
  165.     {
  166.         BigInteger temp;
  167.         temp.D = D + p;
  168.  
  169.         for (int i = 0; i < D; i++)
  170.             temp.digits [i + p] = digits [i];
  171.  
  172.         for (int i = 0; i < p; i++)
  173.             temp.digits [i] = 0;
  174.  
  175.         return temp;
  176.     }
  177.  
  178.     inline BigInteger operator >> (int p) const
  179.     {
  180.         BigInteger temp;
  181.         temp.D = D - p;
  182.  
  183.         for (int i = 0; i < D - p; i++)
  184.             temp.digits [i] = digits [i + p];
  185.  
  186.         for (int i = D - p; i < D; i++)
  187.             temp.digits [i] = 0;
  188.  
  189.         return temp;
  190.     }
  191.  
  192.  
  193.     BigInteger &operator += (const BigInteger &b)
  194.     {
  195.         *this = *this + b;
  196.         return *this;
  197.     }
  198.  
  199.     BigInteger &operator -= (const BigInteger &b)
  200.     {
  201.         *this = *this - b;
  202.         return *this;
  203.     }
  204.  
  205.     BigInteger &operator *= (const BigInteger &b)
  206.     {
  207.         *this = *this * b;
  208.         return *this;
  209.     }
  210.  
  211.     BigInteger &operator /= (const BigInteger &b)
  212.     {
  213.         *this = *this / b;
  214.         return *this;
  215.     }
  216.  
  217.     BigInteger &operator %= (const BigInteger &b)
  218.     {
  219.         *this = *this % b;
  220.         return *this;
  221.     }
  222.  
  223.     inline BigInteger operator + (const BigInteger &o) const
  224.     {
  225.         BigInteger sum = o;
  226.         int carry = 0;
  227.  
  228.         for (sum.D = 0; sum.D < D || carry > 0; sum.D++)
  229.         {
  230.             sum.digits [sum.D] += (sum.D < D ? digits [sum.D] : 0) + carry;
  231.  
  232.             if (sum.digits [sum.D] >= BASE)
  233.             {
  234.                 sum.digits [sum.D] -= BASE;
  235.                 carry = 1;
  236.             }
  237.             else
  238.                 carry = 0;
  239.         }
  240.  
  241.         sum.D = max (sum.D, o.D);
  242.         sum.trim ();
  243.         return sum;
  244.     }
  245.     inline BigInteger operator - (const BigInteger &o) const
  246.     {
  247.         BigInteger diff = *this;
  248.  
  249.         for (int i = 0, carry = 0; i < o.D || carry > 0; i++)
  250.         {
  251.             diff.digits [i] -= (i < o.D ? o.digits [i] : 0) + carry;
  252.  
  253.             if (diff.digits [i] < 0)
  254.             {
  255.                 diff.digits [i] += BASE;
  256.                 carry = 1;
  257.             }
  258.             else
  259.                 carry = 0;
  260.         }
  261.  
  262.         diff.trim ();
  263.         return diff;
  264.     }
  265.     inline BigInteger operator * (const BigInteger &o) const
  266.     {
  267.         BigInteger prod = 0;
  268.         unsigned long long sum = 0, carry = 0;
  269.  
  270.         for (prod.D = 0; prod.D < D + o.D - 1 || carry > 0; prod.D++)
  271.         {
  272.             sum = carry % BASE;
  273.             carry /= BASE;
  274.  
  275.             for (int j = max (prod.D - o.D + 1, 0); j <= min (D - 1, prod.D); j++)
  276.             {
  277.                 sum += (unsigned long long) digits [j] * o.digits [prod.D - j];
  278.  
  279.                 if (sum >= BOUND)
  280.                 {
  281.                     carry += sum / BASE;
  282.                     sum %= BASE;
  283.                 }
  284.             }
  285.  
  286.             carry += sum / BASE;
  287.             prod.digits [prod.D] = sum % BASE;
  288.         }
  289.  
  290.         prod.trim ();
  291.         return prod;
  292.     }
  293.     inline BigInteger range (int a, int b) const
  294.     {
  295.         BigInteger temp = 0;
  296.         temp.D = b - a;
  297.  
  298.         for (int i = 0; i < temp.D; i++)
  299.             temp.digits [i] = digits [i + a];
  300.  
  301.         return temp;
  302.     }
  303.  
  304.  
  305.     inline double double_div (const BigInteger &o) const
  306.     {
  307.         double val = 0, oval = 0;
  308.         int num = 0, onum = 0;
  309.  
  310.         for (int i = D - 1; i >= max (D - 3, 0); i--, num++)
  311.             val = val * BASE + digits [i];
  312.  
  313.         for (int i = o.D - 1; i >= max (o.D - 3, 0); i--, onum++)
  314.             oval = oval * BASE + o.digits [i];
  315.  
  316.         return val / oval * (D - num > o.D - onum ? BASE : 1);
  317.     }
  318.  
  319.     inline pair <BigInteger, BigInteger> divmod (const BigInteger &o) const
  320.     {
  321.         BigInteger quot = 0, rem = *this, temp;
  322.  
  323.         for (int i = D - o.D; i >= 0; i--)
  324.         {
  325.             temp = rem.range (i, rem.D);
  326.             int div = (int) temp.double_div (o);
  327.             BigInteger mult = o * div;
  328.  
  329.             while (div > 0 && temp < mult)
  330.             {
  331.                 mult = mult - o;
  332.                 div--;
  333.             }
  334.  
  335.             while (div + 1 < BASE && !(temp < mult + o))
  336.             {
  337.                 mult = mult + o;
  338.                 div++;
  339.             }
  340.  
  341.             rem = rem - (o * div << i);
  342.  
  343.             if (div > 0)
  344.             {
  345.                 quot.digits [i] = div;
  346.                 quot.D = max (quot.D, i + 1);
  347.             }
  348.         }
  349.  
  350.         quot.trim ();
  351.         rem.trim ();
  352.         return make_pair (quot, rem);
  353.     }
  354.  
  355.     inline BigInteger operator / (const BigInteger &o) const
  356.     {
  357.         return divmod (o).first;
  358.     }
  359.  
  360.     inline BigInteger operator % (const BigInteger &o) const
  361.     {
  362.         return divmod (o).second;
  363.     }
  364.  
  365.  
  366.     inline BigInteger power (int exp) const
  367.     {
  368.         BigInteger p = 1, temp = *this;
  369.  
  370.         while (exp > 0)
  371.         {
  372.             if (exp & 1) p = p * temp;
  373.             if (exp > 1) temp = temp * temp;
  374.             exp >>= 1;
  375.         }
  376.  
  377.         return p;
  378.     }
  379.  
  380.     inline BigInteger factorial() const
  381.     {
  382.         BigInteger ans = 1, num = *this;
  383.  
  384.         if (num == 0 || num == 1)
  385.             return ans;
  386.         while (!(num < 0 || num == 0))
  387.         {
  388.             ans = ans * num;
  389.             num = num - 1;
  390.         }
  391.         return ans;
  392.     }
  393. };
  394.  
  395. ostream &operator<<(ostream &out, BigInteger &c)
  396. {
  397.     out<<c.print();
  398.     return out;
  399. }
  400.  
  401. istream &operator >> (istream &in,BigInteger &c)
  402. {
  403.     char s[10000];
  404.     in>>s;
  405.     c = s;
  406.     return in;
  407. }
  408. BigInteger gcd(BigInteger a,BigInteger b)
  409. {
  410.     if (b==0) return a;
  411.     return gcd(b, a%b);
  412. }
  413.  
  414.  
  415. BigInteger a,b,c,dp[10005];
  416.  
  417.  
  418. int main()
  419. {
  420.     ios_base::sync_with_stdio(0);
  421.     cin.tie(0);
  422.     cout.tie(0);
  423.  
  424.     dp[0]=0;
  425.     dp[1]=1;
  426.     dp[2]=2;
  427.  
  428.     for(ll i=3;i<=10000;i++)
  429.     {
  430.         dp[i]=dp[i-1]+dp[i-2];
  431.     }
  432.  
  433.     ll n;
  434.  
  435.     while(cin>>n)
  436.     {
  437.         a=(dp[n]+dp[n-2]);
  438.         cout<<a<<nl;
  439.     }
  440.  
  441.     get_lost_idiot;
  442. }
  443.  
  444.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement