Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include "stdio.h"
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <algorithm>
- #include <string>
- #include <iomanip>
- using namespace std;
- class Int
- {
- public:
- Int ()//Стандартный конструктор
- {
- octo = false;// Без знака
- size = 0;
- }
- Int (__int64 a)// Переопределение типа в Int
- {
- if (a<0)
- {
- a*=-1;
- octo = true;
- }
- else octo = false;
- size = 0;
- __int64 r;
- r = a;
- while (r)
- {
- numb[size]=r%sys;
- r/=sys;
- size++;
- }
- if (!size)
- {
- numb[0]=0; size=1;
- }
- }
- Int operator^(Int t);
- Int operator%(Int t);
- Int operator/(Int t);
- Int operator*(Int t);
- Int operator+(Int t);
- Int operator=(string s);
- Int operator=(Int t);
- Int operator-(Int t);
- bool operator>(Int t);
- bool operator<(Int t);
- bool operator==(Int t);
- Int sqrt();
- void out();//Метод самовывода
- private:
- char cmp(Int l, Int r);//Сравнение
- /* Для повышение скорости выполнения деления и извлечения корня */
- Int mul(Int l, Int r, int zerol, int zeror);
- Int add(Int l, Int r, int zerol, int zeror);
- bool more(Int l, Int r, int zero);
- int numb[100];// Число
- int size;// Количество чисел
- bool octo;// Знак числа
- static const int sys = (int) 1e9;//Система счисления, в которой происходят операции
- };
- char Int::cmp(Int l, Int r)
- {
- if (l.size>r.size) return 1;
- if (l.size<r.size) return -1;
- for (int i = l.size-1; i>=0; i--)
- {
- if (l.numb[i]>r.numb[i]) return 1;
- if (l.numb[i]<r.numb[i]) return -1;
- }
- return 0;
- }
- Int Int::sqrt()// Реализация бинарного поиска по разрядам
- {
- Int a, b, t, c, d, g, h;
- int i,r,l,k,j;
- a.size = (size+1)/2;
- c.size = a.size;
- t.size=size;
- c.size=a.size;
- h = "2";
- for (i=a.size-1;i>=0;i--,a.size--)
- {
- r=sys;
- l=0;
- while (l+1!=r)
- {
- k = (r-l)/2;
- a.numb[i] = l+k;
- b = mul(a,a,a.size-1,a.size-1);
- b = add(t,b,2*a.size,2*a.size-2);
- d = mul(g,a,a.size,a.size-1);
- b = add(d,b,2*a.size-1,2*a.size-2);
- if (more(b,*this,a.size-1)) r = l+k;
- else l=l+k;
- }
- a.numb[i] = l;
- c.numb[i] = l;
- g = mul(h,c,0,a.size-1);
- t= mul(c,c,a.size-1,a.size-1);
- }
- return c;
- }
- void Int::out()//Метод самовывода
- {
- if (octo) cout<<"-";
- int i =size-1;
- cout<<numb[i];
- for (i--;i>=0;i--)
- {
- cout<<setfill('0')<<setw(9)<<numb[i];
- }
- cout<<endl;
- }
- bool Int::operator>(Int t)
- {
- if (cmp(*this, t)==1) return true;
- return false;
- }
- bool Int::operator<(Int t)
- {
- if (cmp(*this, t)==-1) return true;
- return false;
- }
- bool Int::operator==(Int t)
- {
- if (!cmp(*this, t)) return true;
- return false;
- }
- Int Int::operator-(Int t)
- {
- if ((t.size!=0) || (t.numb[0]!=0)) t.octo = !t.octo;
- return (*this+t);
- }
- bool Int::more(Int l, Int r, int zero)
- {
- if (l.size>r.size) return true;
- if (l.size<r.size) return false;
- for (int i = l.size-1; i>=zero; i--)
- {
- if (l.numb[i]>r.numb[i]) return true;
- if (l.numb[i]<r.numb[i]) return false;
- }
- return false;
- }
- Int Int::operator/(Int t)// Реализация бинарного поиска по разрядам + Отброс конечных нулей
- {
- Int a, b;
- int i,r,l,k,zero;
- if (size<t.size) return 0;
- a.size = size - t.size+1;
- zero = a.size-1;
- for (i=a.size-1;i>=0;i--,zero--)
- {
- r=sys;
- l=0;
- while (l+1!=r)
- {
- k = (r-l)/2;
- a.numb[i] = l+k;
- b = mul(a,t,zero,0);
- if (more(b,*this,zero)) r = l+k;
- else l=l+k;
- }
- if ((!l) && (a.size==i+1) && (a.size!=1)) a.size--;
- else a.numb[i] = l;
- }
- return a;
- }
- Int Int::operator^(Int t) // xor не нужен
- {
- if (t==0) return 1;
- if (t==1) return *this;
- Int a;
- a = *this^(t/2);
- a = a*a;
- if (t%2==1) return (*this)*a;
- else return a;
- }
- Int Int::operator%(Int t)
- {
- Int a;
- a = *this/t;
- a = t*a;
- return *this-a;
- }
- Int Int::mul(Int l, Int r, int zerol, int zeror)
- {
- Int a;
- a.size = l.size+r.size;
- a.octo = (l.octo==r.octo)?false:true;
- __int64 u;
- int i,j,d;
- for (i=zerol+zeror;i<a.size;i++) a.numb[i]=0;
- for (j=zeror;j<r.size;j++)
- {
- for (i=zerol,u=0;i<l.size;i++)
- {
- a.numb[i+j] += u;
- u = __int64(r.numb[j])*l.numb[i];
- d = a.numb[i+j]/sys;
- a.numb[i+j]%=sys;
- a.numb[i+j] += u%sys;
- u/=sys;
- u +=d;
- u+=a.numb[i+j]/sys;
- a.numb[i+j]%=sys;
- }
- a.numb[i+j] +=u;
- }
- while (!a.numb[a.size-1] && (a.size>1+max(zerol,zeror))) a.size--;
- return a;
- }
- Int Int::operator*(Int t)
- {
- return mul(*this,t,0,0);
- }
- Int Int::add(Int l, Int r, int zerol, int zeror)// Только плюс
- {
- Int a;
- int i, mi, ma, d=0;
- if (zerol<zeror)
- {
- mi=zerol;
- ma=zeror;
- for (i=mi;i<ma;i++) r.numb[i]=0;
- }
- else
- {
- mi=zeror;
- ma=zerol;
- for (i=mi;i<ma;i++) l.numb[i]=0;
- }
- a.octo = l.octo;
- if (l.size<r.size)
- {
- a.size = r.size;
- for (i=mi;i<l.size;i++)
- {
- a.numb[i] = l.numb[i]+r.numb[i]+d;
- d = a.numb[i]/sys;
- if (d) a.numb[i] %= sys;
- }
- for (;i<a.size;i++)
- {
- a.numb[i] = r.numb[i]+d;
- d = a.numb[i]/sys;
- if (d) a.numb[i] %= sys;
- }
- }
- else
- {
- a.size = size;
- for (i=mi;i<r.size;i++)
- {
- a.numb[i] = l.numb[i]+r.numb[i]+d;
- d = a.numb[i]/sys;
- if (d) a.numb[i] %= sys;
- }
- for (;i<a.size;i++)
- {
- a.numb[i] = l.numb[i]+d;
- d = a.numb[i]/sys;
- if (d) a.numb[i] %= sys;
- }
- }
- if (d)
- {
- a.size++;
- a.numb[i] = d;
- }
- return a;
- }
- Int Int::operator+(Int t)
- {
- Int a;
- int i, r = 0;
- if (octo==t.octo)
- {
- return add(*this, t, 0, 0);
- }
- else
- {
- Int b,c;
- int l;
- if (octo)
- {
- b = *this;
- a = t;
- }
- else
- {
- b = t;
- a = *this;
- }
- l = max(a.size,b.size);
- for (i=0;i<b.size;i++)
- {
- b.numb[i] = sys-b.numb[i]-1;
- }
- for (;i<l;i++) b.numb[i] = sys - 1;
- for (i=a.size;i<l;i++) a.numb[i] = 0;
- b.size = l;
- b.octo = false;
- c = "1";
- b = b + c;
- a = a + b;
- if (l<a.size)
- {
- a.size = l;
- while (!a.numb[a.size-1] && (a.size>1)) a.size--;
- }
- else
- {
- for (i=0;i<l;i++) a.numb[i] = sys-a.numb[i]-1;
- while (!a.numb[a.size-1]) a.size--;
- a = a + c;
- a.octo = true;
- }
- }
- return a;
- }
- Int Int::operator=(string s)
- {
- int i,j,r=0;
- __int64 k;
- size = 0;
- if (s[0]=='-') {octo = true;r=1;}
- for (i=s.length();i>r;i-=9,size++)
- {
- for (j=max(i-9,r),k=0;j<i;j++)
- {
- k*=10;k+=s[j]-48;
- }
- numb[size]=k;
- }
- return *this;
- }
- Int Int::operator=(Int t)
- {
- octo = t.octo;
- int i;
- for (i=0;i<t.size;i++) numb[i] = t.numb[i];
- size = t.size;
- return *this;
- }
- Int s[301]; // Динамика
- int k,n,i;
- int main()
- {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- cin>>k>>n;
- s[0] = "0";
- s[1] = "1";
- for (i=2;i<=n;i++)
- {
- s[i] = s[i-1]*2-s[max(i-k-1,0)];
- }
- (s[n]-s[max(n-k,0)]).out();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement