Advertisement
Guest User

Длинная арифметика

a guest
May 27th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.30 KB | None | 0 0
  1. #include <fstream>
  2. #include "stdio.h"
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <string>
  8. #include <iomanip>
  9.  
  10. using namespace std;
  11.  
  12. class Int
  13. {  
  14.     public:
  15.     Int ()//Стандартный конструктор
  16.     {
  17.         octo = false;// Без знака
  18.         size = 0;
  19.     }
  20.  
  21.     Int (__int64 a)// Переопределение типа в Int
  22.     {
  23.         if (a<0)
  24.         {
  25.             a*=-1;
  26.             octo = true;
  27.         }
  28.         else octo = false;
  29.         size = 0;
  30.         __int64 r;
  31.         r = a;
  32.         while (r)
  33.         {
  34.             numb[size]=r%sys;
  35.             r/=sys;
  36.             size++;
  37.         }
  38.         if (!size)
  39.         {
  40.             numb[0]=0; size=1;
  41.         }
  42.     }
  43.      
  44.     Int operator^(Int t);
  45.     Int operator%(Int t);
  46.     Int operator/(Int t);
  47.     Int operator*(Int t);
  48.     Int operator+(Int t);
  49.     Int operator=(string s);
  50.     Int operator=(Int t);
  51.     Int operator-(Int t);
  52.     bool operator>(Int t);
  53.     bool operator<(Int t);
  54.     bool operator==(Int t);
  55.  
  56.     Int sqrt();
  57.     void out();//Метод самовывода
  58.  
  59.     private:
  60.     char cmp(Int l, Int r);//Сравнение
  61.     /* Для повышение скорости выполнения деления и извлечения корня */
  62.     Int mul(Int l, Int r, int zerol, int zeror);
  63.     Int add(Int l, Int r, int zerol, int zeror);
  64.     bool more(Int l, Int r, int zero);
  65.  
  66.     int numb[100];// Число
  67.     int size;// Количество чисел
  68.     bool octo;// Знак числа
  69.     static const int sys = (int) 1e9;//Система счисления, в которой происходят операции
  70. };
  71.  
  72. char Int::cmp(Int l, Int r)
  73. {
  74.     if (l.size>r.size) return 1;
  75.     if (l.size<r.size) return -1;
  76.     for (int i = l.size-1; i>=0; i--)
  77.     {
  78.         if (l.numb[i]>r.numb[i]) return 1;
  79.         if (l.numb[i]<r.numb[i]) return -1;
  80.     }
  81.     return 0;
  82. }
  83.  
  84. Int Int::sqrt()// Реализация бинарного поиска по разрядам
  85. {
  86.     Int a, b, t, c, d, g, h;
  87.     int i,r,l,k,j;
  88.     a.size = (size+1)/2;
  89.     c.size = a.size;
  90.     t.size=size;
  91.     c.size=a.size;
  92.     h = "2";
  93.     for (i=a.size-1;i>=0;i--,a.size--)
  94.     {
  95.         r=sys;
  96.         l=0;
  97.         while (l+1!=r)
  98.         {
  99.             k = (r-l)/2;
  100.             a.numb[i] = l+k;
  101.             b = mul(a,a,a.size-1,a.size-1);
  102.             b = add(t,b,2*a.size,2*a.size-2);
  103.             d = mul(g,a,a.size,a.size-1);
  104.             b = add(d,b,2*a.size-1,2*a.size-2);
  105.             if (more(b,*this,a.size-1)) r = l+k;
  106.             else l=l+k;
  107.         }
  108.         a.numb[i] = l;
  109.         c.numb[i] = l;
  110.         g = mul(h,c,0,a.size-1);
  111.         t=  mul(c,c,a.size-1,a.size-1);
  112.     }
  113.     return c;
  114. }
  115.  
  116. void Int::out()//Метод самовывода
  117. {
  118.     if (octo) cout<<"-";
  119.     int i =size-1;
  120.     cout<<numb[i];
  121.     for (i--;i>=0;i--)
  122.     {
  123.         cout<<setfill('0')<<setw(9)<<numb[i];
  124.     }
  125.     cout<<endl;
  126. }
  127.  
  128. bool Int::operator>(Int t)
  129. {
  130.     if (cmp(*this, t)==1) return true;
  131.     return false;
  132. }
  133.  
  134. bool Int::operator<(Int t)
  135. {
  136.     if (cmp(*this, t)==-1) return true;
  137.     return false;
  138. }
  139.  
  140. bool Int::operator==(Int t)
  141. {
  142.     if (!cmp(*this, t)) return true;
  143.     return false;
  144. }
  145.  
  146. Int Int::operator-(Int t)
  147. {
  148.     if ((t.size!=0) || (t.numb[0]!=0)) t.octo = !t.octo;
  149.     return (*this+t);
  150. }
  151.  
  152. bool Int::more(Int l, Int r, int zero)
  153. {
  154.     if (l.size>r.size) return true;
  155.     if (l.size<r.size) return false;
  156.     for (int i = l.size-1; i>=zero; i--)
  157.     {
  158.         if (l.numb[i]>r.numb[i]) return true;
  159.         if (l.numb[i]<r.numb[i]) return false;
  160.     }
  161.     return false;
  162. }
  163.  
  164. Int Int::operator/(Int t)// Реализация бинарного поиска по разрядам + Отброс конечных нулей
  165. {
  166.     Int a, b;
  167.     int i,r,l,k,zero;
  168.     if (size<t.size) return 0;
  169.     a.size = size - t.size+1;
  170.     zero = a.size-1;
  171.     for (i=a.size-1;i>=0;i--,zero--)
  172.     {
  173.         r=sys;
  174.         l=0;
  175.         while (l+1!=r)
  176.         {
  177.             k = (r-l)/2;
  178.             a.numb[i] = l+k;
  179.             b = mul(a,t,zero,0);
  180.             if (more(b,*this,zero)) r = l+k;
  181.             else l=l+k;
  182.         }
  183.         if ((!l) && (a.size==i+1) && (a.size!=1)) a.size--;
  184.         else a.numb[i] = l;
  185.     }
  186.     return a;
  187. }
  188.  
  189. Int Int::operator^(Int t) // xor не нужен
  190. {
  191.     if (t==0) return 1;
  192.     if (t==1) return *this;
  193.     Int a;
  194.     a = *this^(t/2);
  195.     a = a*a;
  196.     if (t%2==1) return (*this)*a;
  197.     else return a;
  198. }
  199.  
  200. Int Int::operator%(Int t)
  201. {
  202.     Int a;
  203.     a = *this/t;
  204.     a = t*a;
  205.     return *this-a;
  206. }
  207.  
  208. Int Int::mul(Int l, Int r, int zerol, int zeror)
  209. {
  210.     Int a;
  211.     a.size = l.size+r.size;
  212.     a.octo = (l.octo==r.octo)?false:true;
  213.     __int64 u;
  214.     int i,j,d;
  215.     for (i=zerol+zeror;i<a.size;i++) a.numb[i]=0;
  216.     for (j=zeror;j<r.size;j++)
  217.     {
  218.         for (i=zerol,u=0;i<l.size;i++)
  219.         {
  220.             a.numb[i+j] += u;
  221.             u = __int64(r.numb[j])*l.numb[i];
  222.             d = a.numb[i+j]/sys;
  223.             a.numb[i+j]%=sys;
  224.             a.numb[i+j] += u%sys;
  225.             u/=sys;
  226.             u +=d;
  227.             u+=a.numb[i+j]/sys;
  228.             a.numb[i+j]%=sys;
  229.         }
  230.         a.numb[i+j] +=u;
  231.     }
  232.     while (!a.numb[a.size-1] && (a.size>1+max(zerol,zeror))) a.size--;
  233.     return a;
  234. }
  235.  
  236. Int Int::operator*(Int t)
  237. {
  238.     return mul(*this,t,0,0);
  239. }
  240.  
  241. Int Int::add(Int l, Int r, int zerol, int zeror)// Только плюс
  242. {
  243.     Int a;
  244.     int i, mi, ma, d=0;
  245.     if (zerol<zeror)
  246.     {
  247.         mi=zerol;
  248.         ma=zeror;
  249.         for (i=mi;i<ma;i++) r.numb[i]=0;
  250.     }
  251.     else
  252.     {
  253.         mi=zeror;
  254.         ma=zerol;
  255.         for (i=mi;i<ma;i++) l.numb[i]=0;
  256.     }
  257.     a.octo = l.octo;
  258.     if (l.size<r.size)
  259.     {
  260.         a.size = r.size;
  261.         for (i=mi;i<l.size;i++)
  262.         {
  263.             a.numb[i] = l.numb[i]+r.numb[i]+d;
  264.             d = a.numb[i]/sys;
  265.             if (d)  a.numb[i] %= sys;
  266.         }
  267.         for (;i<a.size;i++)
  268.         {
  269.             a.numb[i] = r.numb[i]+d;
  270.             d = a.numb[i]/sys;
  271.             if (d)  a.numb[i] %= sys;
  272.         }
  273.     }
  274.     else
  275.     {
  276.         a.size = size;
  277.         for (i=mi;i<r.size;i++)
  278.         {
  279.             a.numb[i] = l.numb[i]+r.numb[i]+d;
  280.             d = a.numb[i]/sys;
  281.             if (d)  a.numb[i] %= sys;
  282.         }
  283.         for (;i<a.size;i++)
  284.         {
  285.             a.numb[i] = l.numb[i]+d;
  286.             d = a.numb[i]/sys;
  287.             if (d)  a.numb[i] %= sys;
  288.         }
  289.     }
  290.     if (d)
  291.     {
  292.         a.size++;
  293.         a.numb[i] = d;
  294.     }
  295.     return a;
  296. }
  297.  
  298. Int Int::operator+(Int t)
  299. {
  300.     Int a;
  301.     int i, r = 0;
  302.     if (octo==t.octo)
  303.     {
  304.         return add(*this, t, 0, 0);
  305.     }
  306.     else
  307.     {
  308.         Int b,c;
  309.         int l;
  310.         if (octo)
  311.         {
  312.             b = *this;
  313.             a = t;
  314.         }
  315.         else
  316.         {
  317.             b = t;
  318.             a = *this;
  319.         }
  320.         l = max(a.size,b.size);
  321.         for (i=0;i<b.size;i++)
  322.         {
  323.             b.numb[i] = sys-b.numb[i]-1;
  324.         }
  325.         for (;i<l;i++) b.numb[i] = sys - 1;
  326.         for (i=a.size;i<l;i++) a.numb[i] = 0;
  327.         b.size = l;
  328.         b.octo = false;
  329.         c = "1";
  330.         b = b + c;
  331.         a = a + b;
  332.         if (l<a.size)
  333.         {
  334.             a.size = l;
  335.             while (!a.numb[a.size-1] && (a.size>1)) a.size--;
  336.         }
  337.         else
  338.         {
  339.             for (i=0;i<l;i++) a.numb[i] = sys-a.numb[i]-1;
  340.             while (!a.numb[a.size-1]) a.size--;
  341.             a = a + c;
  342.             a.octo = true;
  343.         }
  344.  
  345.     }
  346.     return a;
  347. }
  348.  
  349. Int Int::operator=(string s)
  350. {
  351.     int i,j,r=0;
  352.     __int64 k;
  353.     size = 0;
  354.     if (s[0]=='-') {octo = true;r=1;}
  355.     for (i=s.length();i>r;i-=9,size++)
  356.     {
  357.         for (j=max(i-9,r),k=0;j<i;j++)
  358.         {
  359.             k*=10;k+=s[j]-48;
  360.         }
  361.         numb[size]=k;
  362.     }
  363.     return *this;
  364. }
  365.  
  366. Int Int::operator=(Int t)
  367. {
  368.     octo = t.octo;
  369.     int i;
  370.     for (i=0;i<t.size;i++) numb[i] = t.numb[i];
  371.     size = t.size;
  372.     return *this;
  373. }
  374.  
  375. Int s[301]; // Динамика
  376. int k,n,i;
  377.  
  378. int main()
  379. {
  380.     freopen("input.txt","r",stdin);
  381.     freopen("output.txt","w",stdout);
  382.     cin>>k>>n;
  383.     s[0] = "0";
  384.     s[1] = "1";
  385.     for (i=2;i<=n;i++)
  386.     {
  387.         s[i] = s[i-1]*2-s[max(i-k-1,0)];
  388.     }
  389.     (s[n]-s[max(n-k,0)]).out();
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement