下测试点发现没错,疑似UB但关O2无果
查看原帖
下测试点发现没错,疑似UB但关O2无果
1125106
11514zbs楼主2025/1/31 22:28
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;

class _int8192
{
	private:
		static const int BIT = 21400;
		int num[BIT];
		inline void getsize()
		{
			int i = BIT;
			while (i >= 0 && !(num[i]))
			{
				i--;
			}
			if (i == -1)
			{
				size = 1;
			}
			else
			{
				size = i + 1;
			}
			return ;
		}
		int cmp[BIT];
		inline bool PrintBool()
		{
			memset(cmp, 0, BIT << 2);
			if (!(memcmp(num, cmp, BIT << 2)))
			{
				return true;
			}
			return false;
		}
	public:
		int size;
		bool porn = true;
		inline void init()
		{
			size = 0;
			memset(num, 0, BIT << 2);
			return ;
		}
		inline static _int8192 intTO_int8192(int n)
		{
			_int8192 ans;
			ans.init();
			if (n < 0)
			{
				ans.porn = false;
				n = -n;
			}
			else
			{
				ans.porn = true;
			}
			int p = 0;
			while (n)
			{
				ans.num[p++] = n % 10;
				n = (n >> 1) / 5;
			}
			ans.getsize();
			return ans;
		}
		inline void read()
		{
			init();
			stack <int> st;
			size = 0;
			memset(num, 0, BIT << 2);
			char a = getchar();
			bool c = true, fr0 = true;
			while (!(a >= '0' && a <= '9') && a != '-')
			{
				a = getchar();
			}
			while (true)
			{
				if (!((a >= '0' && a <= '9') || a == '-') || (!(c) && a == '-'))
				{
					break;
				}
				if (a == '-')
				{
					size--;
					porn = false;
				}
				else if (!(fr0))
				{
					st.push(a - '0');
				}
				else if (fr0 && a != '0')
				{
					st.push(a - '0');
					fr0 = false;
				}
				else
				{
					size--;
				}
				if (c)
				{
					c = false;
				}
				size++;
				a = getchar();
			}
			for (register int i = 0; i < size; i++)
			{
				num[i] = st.top();
				st.pop();
			}
			if (!(size))
			{
				size = 1;
				num[0] = 0;
			}
			return ;
		}
		inline void print()
		{
			if (num[0] == -8)
			{
				printf("Runtime Error! Zero(0) cannot be a divisor!");
				return;
			}
			if (num[0] == -9)
			{
				printf("Runtime Error! Zero(0) to the power of zero(0) is undefined!");
			}
			if (PrintBool())
			{
				printf("0");
				return;
			}
			if (!(porn))
			{
				printf("-");
			}
			int p = size;
			while (p)
			{
				printf("%d", num[--p]);
			}
			return ;
		}
		inline void swap(_int8192 &a)
		{
			_int8192 t;
			t = *this;
			*this = a;
			a = t;
			return ;
		}
		inline void operator =(const _int8192 a)
		{
			memcpy(num, a.num, 2 * BIT);
			porn = a.porn;
			size = a.size;
			return ;
		}
		inline bool operator !()
		{
            return *this == _int8192::intTO_int8192(0);
        }
		inline _int8192 abs()
		{
			_int8192 ret;
			ret = *this;
			ret.porn = true;
			return ret;
		}
		inline _int8192 opp()
		{
			_int8192 ret;
			ret = *this;
			ret.porn = !(ret.porn);
			return ret;
		}
		inline bool operator >(const _int8192 a) const
		{
			if (porn && !(a.porn))
			{
				return true;
			}
			if (!(porn) && a.porn)
			{
				return false;
			}
			bool ret = false;
			int bit = (size > a.size) ? size : a.size;
			for (register int i = bit - 1; i >= 0; i--)
			{
				if (num[i] > a.num[i])
				{
					ret = true;
					break;
				}
				else if (num[i] < a.num[i])
				{
					ret = false;
					break;
				}
			}
			if (!(porn))
			{
				return !(ret);
			}
			return ret;
		}
		inline bool operator <(const _int8192 a) const
		{
			if (porn && !(a.porn))
			{
				return false;
			}
			if (!(porn) && a.porn)
			{
				return true;
			}
			bool ret = false;
			int bit = (size > a.size) ? size : a.size;
			for (register int i = bit - 1; i >= 0; i--)
			{
				if (num[i] < a.num[i])
				{
					ret = true;
					break;
				}
				else if (num[i] > a.num[i])
				{
					ret = false;
					break;
				}
			}
			if (!(porn))
			{
				return !(ret);
			}
			return ret;
		}
		inline bool operator ==(const _int8192 a) const
		{
			if (porn != a.porn)
			{
				return false;
			}
			int bit = (size > a.size) ? size : a.size;
			for (register int i = bit - 1; i >= 0; i--)
			{
				if (num[i] != a.num[i])
				{
					return false;
				}
			}
			return true;
		}
		inline bool operator !=(const _int8192 a) const
		{
            return !(*this == a);
        }
		inline bool operator >=(const _int8192 a) const
		{
			return *this > a || *this == a;
		}
		inline bool operator <=(const _int8192 a) const
		{
			return *this < a || *this == a;
		}
		inline _int8192 operator +(const _int8192 a) const
		{
			_int8192 ans;
			ans.init();
			if (porn == a.porn)
			{
				ans.porn = porn;
				int bit = (size > a.size) ? size : a.size;
				for (register int i = 0; i < bit; i++)
				{
					ans.num[i] += num[i] + a.num[i];
					ans.num[i + 1] = (ans.num[i] >> 1) / 5;
					ans.num[i] %= 10;
				}
			}
			else
			{
				_int8192 b = a;
				_int8192 _this = *this;
				ans.porn = (b.abs() > _this.abs()) ? a.porn : porn;
				int bit = (size > a.size) ? size : a.size;
			label:
				for (register int i = 0; i < bit; i++)
				{
					ans.num[i] += b.num[i] - _this.num[i];
					if (ans.num[i] < 0)
					{
						ans.num[i] += 10;
						ans.num[i + 1]--;
					}
				}
				if (ans.num[bit] < 0)
				{
					memset(ans.num, 0, 2 * BIT);
					b.swap(_this);
					goto label;
				}
			}
			ans.getsize();
			return ans;
		}
		inline _int8192 operator -(const _int8192 a) const
		{
			_int8192 ans;
			ans.init();
			if (porn == a.porn)
			{
				_int8192 b = a;
				_int8192 _this = *this;
				ans.porn = (b.abs() < _this.abs()) ? porn : !(porn);
				int bit = (size > a.size) ? size : a.size;
				if (ans.porn != porn)
				{
					b.swap(_this);
				}
				for (register int i = 0; i < bit; i++)
				{
					ans.num[i] += _this.num[i] - b.num[i];
					if (ans.num[i] < 0)
					{
						ans.num[i] += 10;
						ans.num[i + 1]--;
					}
				}
			}
			else
			{
				ans.porn = porn;
				_int8192 b = a;
				b = b.opp();
				int bit = (size > b.size) ? size : b.size;
				for (register int i = 0; i < bit; i++)
				{
					ans.num[i] += num[i] + b.num[i];
					ans.num[i + 1] = (ans.num[i] >> 1) / 5;
					ans.num[i] %= 10;
				}
			}
			ans.getsize();
			return ans;
		}
        inline _int8192 operator >>(const _int8192 a) const
		{
            _int8192 _this = *this, b = *this;
            _this = _this.abs();
            b = b.abs();
            bool F;
            for (register _int8192 j = _int8192::intTO_int8192(0); j < a; j++)
            {
            	F = 0;
                if (_this == _int8192::intTO_int8192(0))
                {
                    _this.getsize();
                    return _this;
                }
                for (register int i = _this.size - 1; i >= 0; i--)
                {
                    switch (_this.num[i])
                    {
                        case 0:
                            b.num[i] = F + (F << 2);
                            F = 0;
                            break;
                        case 1:
                            b.num[i] = F + (F << 2);
                            F = 1;
                            break;
                        case 2:
                            b.num[i] = 1 + F + (F << 2);
                            F = 0;
                            break;
                        case 3:
                            b.num[i] = 1 + F + (F << 2);
                            F = 1;
                            break;
                        case 4:
                            b.num[i] = 2 + F + (F << 2);
                            F = 0;
                            break;
                        case 5:
                            b.num[i] = 2 + F + (F << 2);
                            F = 1;
                            break;
                        case 6:
                            b.num[i] = 3 + F + (F << 2);
                            F = 0;
                            break;
                        case 7:
                            b.num[i] = 3 + F + (F << 2);
                            F = 1;
                            break;
                        case 8:
                            b.num[i] = 4 + F + (F << 2);
                            F = 0;
                            break;
                        case 9:
                            b.num[i] = 4 + F + (F << 2);
                            F = 1;
                            break;
                    }
                }
                b.getsize();
                _this = b;
                _this.getsize();
            }
            b.porn = porn;
			return b;
		}
        inline _int8192 operator &(const _int8192 a) const
		{
            if (a == _int8192::intTO_int8192(1))
            {
                return _int8192::intTO_int8192(num[0] & 1);
            }
		}
		inline void operator +=(const _int8192 a)
		{
			*this = *this + a;
			return ;
		}
		inline void operator -=(const _int8192 a)
		{
			*this = *this - a;
			return ;
		}
		inline void operator ++()
		{
			*this = *this + _int8192::intTO_int8192(1);
			return ;
		}
		inline _int8192 operator ++(int)
		{
			_int8192 *ret = this;
			++*this;
			return *ret;
		}
		inline void operator --()
		{
			*this = *this - _int8192::intTO_int8192(1);
			return ;
		}
		inline _int8192 operator --(int)
		{
			_int8192 *ret = this;
			--*this;
			return *ret;
		}
		inline _int8192 operator *(const _int8192 a) const
		{
			_int8192 ans;
			ans.init();
			ans.porn = porn == a.porn;
			for (register int i = 0; i < size; i++)
			{
				for (register int j = 0; j < a.size; j++)
				{
					ans.num[i + j] += num[i] * a.num[j];
				}
			}
			for (register int i = 0; i < BIT; i++)
			{
				ans.num[i + 1] += (ans.num[i] >> 1) / 5;
				ans.num[i] %= 10;
			}
			ans.getsize();
			return ans;
		}
		inline _int8192 operator /(const _int8192 a) const
		{
			if (a == _int8192::intTO_int8192(0))
			{
				_int8192 re;
				re.init();
				re.num[0] = -8;
				return re;
			}
            _int8192 _this = *this;
            _this = _this.abs();
            _int8192 b = a;
            b = b.abs();
			_int8192 l = _int8192::intTO_int8192(0), r = _this, mid;
			while (l <= r)
            {
                mid = (l + r) >> _int8192::intTO_int8192(1);
                if (mid * b < _this)
                {
                    l = mid + 1;
                }
                else
                {
                    r = mid - 1;
                }
            }
            l = (l * a > *this) ? (l - 1) : l;
            l.porn = (a.porn) ? porn : !(porn);
			l.getsize();
			return l;
		}
		inline _int8192 operator %(const _int8192 a) const
		{
			if (a == _int8192::intTO_int8192(0))
			{
                _int8192 re;
                re.init();
                re.num[0] = -8;
                return re;
            }
			_int8192 _this = *this;
			_int8192 b = a;
			_this = _this.abs();
			b = b.abs();
            _int8192 ans = _this / b;
            ans = _this - ans * b;
			ans.porn = porn;
			ans.getsize();
			return ans;
		}
        inline void operator >>=(const _int8192 a)
		{
            *this = *this >> a;
			return ;
		}
        inline void operator &=(const _int8192 a)
		{
            *this = *this & a;
			return ;
		}
        inline void operator *=(const _int8192 a)
		{
            *this = *this * a;
            return ;
        }
        inline void operator /=(const _int8192 a)
		{
            *this = *this / a;
            return ;
        }
        inline void operator %=(const _int8192 a)
		{
            *this = *this % a;
            return ;
        }
        inline _int8192 operator +(const int a) const
		{
            return *this + _int8192::intTO_int8192(a);
        }
        inline _int8192 operator -(const int a) const
		{
            return *this - _int8192::intTO_int8192(a);
        }
        inline _int8192 operator >>(const int a) const
		{
			return *this >> _int8192::intTO_int8192(a);
		}
        inline _int8192 operator &(const int a) const
		{
			return *this & _int8192::intTO_int8192(a);
		}
        inline _int8192 operator *(const int a) const
		{
            return *this * _int8192::intTO_int8192(a);
        }
        inline _int8192 operator /(const int a) const
		{
            return *this / _int8192::intTO_int8192(a);
        }
        inline _int8192 operator %(const int a) const
		{
            return *this % _int8192::intTO_int8192(a);
        }
        inline bool operator <(const int a) const
		{
            return *this < _int8192::intTO_int8192(a);
        }
        inline bool operator >(const int a) const
		{
            return *this > _int8192::intTO_int8192(a);
        }
        inline bool operator ==(const int a) const
		{
            return *this == _int8192::intTO_int8192(a);
        }
        inline bool operator !=(const int a) const
		{
            return *this != _int8192::intTO_int8192(a);
        }
        inline bool operator >=(const int a) const
		{
            return *this >= _int8192::intTO_int8192(a);
        }
        inline bool operator <=(const int a) const
		{
            return *this <= _int8192::intTO_int8192(a);
        }
        inline void operator +=(const int a)
		{
           *this += _int8192::intTO_int8192(a);
           return ;
        }
        inline void operator -=(const int a)
		{
            *this -= _int8192::intTO_int8192(a);
            return ;
        }
        inline void operator >>=(const int a)
		{
            *this >>= _int8192::intTO_int8192(a);
			return ;
		}
        inline void operator &=(const int a)
		{
            *this &= _int8192::intTO_int8192(a);
			return ;
		}
        inline void operator *=(const int a)
		{
            *this *= _int8192::intTO_int8192(a);
            return ;
        }
        inline void operator /=(const int a)
		{
            *this /= _int8192::intTO_int8192(a);
            return ;
        }
        inline void operator %=(const int a)
		{
            *this %= _int8192::intTO_int8192(a);
            return ;
        }
        inline void operator =(int a)
		{
            *this = _int8192::intTO_int8192(a);
            return ;
        }
        inline friend _int8192 operator +(int a, _int8192 b)
		{
            return _int8192::intTO_int8192(a) + b;
        }
        inline friend _int8192 operator -(int a, _int8192 b)
		{
            return _int8192::intTO_int8192(a) - b;
        }
        inline friend _int8192 operator *(int a, _int8192 b)
		{
            return _int8192::intTO_int8192(a) * b;
        }
        inline friend _int8192 operator /(int a, _int8192 b)
		{
            return _int8192::intTO_int8192(a) / b;
        }
        inline friend _int8192 operator %(int a, _int8192 b)
		{
            return _int8192::intTO_int8192(a) % b;
        }
        inline _int8192 pow(_int8192 b)
		{
            if ((!(b) && *this != 0) || (*this == _int8192::intTO_int8192(0)))
			{
				return _int8192::intTO_int8192(0);
			}
            if (!(b) && (*this <= _int8192::intTO_int8192(0)))
			{
                _int8192 re;
                re.init();
                re.num[0] = -9;
                return re;
            }
            if (b < _int8192::intTO_int8192(0))
            {
            	return _int8192::intTO_int8192(0);
			}
            _int8192 a = *this, _b = b, r = _int8192::intTO_int8192(1);
            a = a.abs();
            while (_b > _int8192::intTO_int8192(0))
            {
                if ((_b & _int8192::intTO_int8192(1)) == _int8192::intTO_int8192(1))
                {
                    r *= a;
                }
                _b >>= _int8192::intTO_int8192(1);
                a *= a;
            }
            if (!(a.porn) && ((b & _int8192::intTO_int8192(1)) == _int8192::intTO_int8192(1)))
            {
                r.porn = false;
            }
            return r;
        }
        inline _int8192 pow(int b)
		{
            return pow(_int8192::intTO_int8192(b));
        }
        inline friend ostream& operator <<(ostream &os, const _int8192 &n)
		{
            ios::sync_with_stdio(false);
            if (n.num[0] == -8)
			{
                os << "Runtime Error! Zero(0) cannot be a divisor!";
                return os;
            }
            if (n.num[0] == -9)
			{
                os << "Runtime Error! Zero(0) to the power of zero(0) is undefined!";
                return os;
            }
            _int8192 b = n;
            if (b.PrintBool())
			{
                os << "0";
                return os;
            }
            if (!(n.porn))
			{
				os << "-";
			}
            int p = n.size - 1;
            bool fr = true;
            while (p >= 0)
			{
			    if (fr)
			    {
			        if (n.num[p] != '0')
			        {
			            os << n.num[p];
			        }
			        fr = false;
			    }
			    else
			    {
			        os << n.num[p];
			    }
                p--;
            }
            return os;
        }
        inline friend istream& operator >>(istream &is, _int8192 &n)
		{
            ios::sync_with_stdio(false);
            n.init();
            stack <int> st;
            char a;
            bool c = true, fr0 = true;
            a = getchar();
            while (a == ' ' || a == '\n')
            {
                a = getchar();
            }
            while (true)
			{
                if (!((a >= '0' && a <= '9') || a == '-') || (!(c) && a == '-'))
				{
					break;
				}
                if (a == '-')
				{
                    n.size--;
                    n.porn = false;
                }
                else if (!(fr0))
				{
					st.push(a - '0');
				}
                else if (fr0 && a != '0')
				{
                    st.push(a - '0');
                    fr0 = false;
                }
                else
				{
					n.size--;
				}
                if (c)
				{
					c = false;
				}
                n.size++;
                a = getchar();
            }
            for (register int i = 0; i < n.size; i++)
			{
                n.num[i] = st.top();
                st.pop();
            }
            if (!(n.size))
			{
                n.size = 1;
                n.num[0] = 0;
            }
            return is;
        }
}a, b;

int main()
{
	cin >> a >> b;
    cout << (a + b) << endl << (a - b) << endl << (a * b) << endl << (a / b) << endl << (a % b) << endl;
	return 0;
}

祖传 _int8192 ,,,

2025/1/31 22:28
加载中...