Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <int MAXSIZE>
- class HugeN: public bitset<MAXSIZE>{
- private:
- int nr;
- void negate(){
- (*this).flip();
- (*this)+=1;
- }
- public:
- HugeN(int nr){
- if (nr<0){
- bitset<MAXSIZE>(-nr);
- (*this).negate();
- }
- else{
- bitset<MAXSIZE>(nr);
- }
- }
- HugeN(HugeN& other){
- (*this)=other;
- }
- HugeN(string S, int base){
- if (base==2)
- bitset<MAXSIZE>(S);
- else{
- int size=0;
- while(S!="0"){
- if (S[0]=='0') S=S.substr(1);
- int carry=0;
- for (auto &x : S){
- carry=carry*10+x-'0';
- x='0'+carry/2;
- carry&=1;
- }
- (*this)[size++]=carry;
- }
- }
- }
- HugeN(){}
- void setnr(int nr){
- this->nr=nr;
- }
- string print(){
- string toadd="1";
- string ans="";
- int i;
- goto exec;
- forward:{
- int carry=0;
- for(auto& x: toadd){
- carry=carry+2*(x-'0');
- x=carry%10+'0';
- carry/=10;
- }
- if (carry!=0)
- toadd.push_back(carry+'0');
- }goto back;
- exec:
- for (int i=1; i<=MAXSIZE/8*3; i++) ans+='0';
- for (i=0; i<MAXSIZE; i++){
- if ((*this)[i]){
- int carry=0;
- for (int i=0; i<ans.length(); i++){
- carry+=ans[i]-'0';
- if (i<toadd.length()) carry+=toadd[i]-'0';
- ans[i]=carry%10+'0';
- carry/=10;
- }
- }
- goto forward;
- back:;
- }
- while (ans.back()=='0') ans.pop_back();
- reverse(ans.begin(), ans.end());
- return ans;
- }
- HugeN& operator-(){
- this->negate();
- return *this;
- }
- HugeN operator+(HugeN& other){
- HugeN<MAXSIZE> ans;
- bool carry=0;
- bool rez=0;
- for (int i=0; i<MAXSIZE; i++){
- rez=carry^(*this)[i]^other[i];
- carry=(carry&other[i])|(*this)[i]&(carry|other[i]);
- ans[i]=rez;
- }
- return ans;
- }
- HugeN& operator+=(HugeN& other){
- bool carry=0;
- bool rez=0;
- for (int i=0; i<MAXSIZE; i++){
- rez=carry^(*this)[i]^other[i];
- carry=(carry&other[i])|(*this)[i]&(carry|other[i]);
- (*this)[i]=rez;
- }
- return *this;
- }
- HugeN operator+(int other){
- HugeN ans(other);
- ans+=(*this);
- return ans;
- }
- void operator+=(int other){
- HugeN otherH(other);
- (*this)+=otherH;
- }
- HugeN operator-(HugeN& other){
- other=-other;
- return (*this)+other;
- }
- HugeN operator-(int other){
- HugeN ans(other);
- return ans+(*this);
- }
- void operator-=(HugeN& other){
- (*this)+=-other;
- }
- void operator -=(int other){
- (*this)+=-other;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement