Advertisement
Saleh127

UVA 1647 / DP - BigInt --- Spoj M00PAIR

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