#include<bits/stdc++.h>
using namespace std;
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#ifdef abs
#undef abs
#endif
#ifdef isdigit
#undef isdigit
#endif
#ifdef isalpha
#undef isalpha
#endif
#define min(a,b) ( a < b ? a : b )
#define max(a,b) ( a > b ? a : b )
#define abs(a) ( a < 0 ? ~a - 1 : a )
#define isdigit(ch) ( ch >= 48 && ch <= 57 )
#define isalpha(ch) ( ( ch >= 97 && ch <= 122 ) || ( ch >= 65 && ch <= 90 ) )
#include"BIG_CMP_AND_BYTEOPTIATOR" //逻辑运算符&位运算符
#include"BIG_SWAP_AND_INOUT_OTHER" //输入输出&类型转换&自运算( 如 *=,+=,>>= )
using namespace Mynamespace ;
namespace Mynamespace _GLIBCXX_VISIBILITY(default)
{
class BIG
{
public:
string& A ;
inline BIG operator + ( BIG &B ) ;
inline BIG operator - ( BIG &B ) ;
inline BIG operator * ( BIG &B ) ;
inline BIG operator / ( BIG &B ) ;
inline BIG operator () ( int &B ) ;
inline BIG operator () ( long long &B ) ;
inline BIG operator () ( string &B ) ;
inline BIG operator () ( const char * &B ) ;
inline BIG operator >> ( BIG &B ) ;
inline BIG operator << ( BIG &B ) ;
inline bool operator < ( BIG &B ) ;
inline bool operator > ( BIG &B ) ;
inline bool operator >= ( BIG &B ) ;
inline bool operator <= ( BIG &B ) ;
inline bool operator == ( BIG &B ) ;
inline bool operator != ( BIG &B ) ;
inline void operator = ( BIG &B ) ;
inline void operator = ( int &B ) ;
inline void operator = ( long long &B ) ;
inline void operator = ( __int128 &B ) ;
inline void operator = ( string &B ) ;
inline void operator = ( const char * &B ) ;
inline void operator += ( BIG &B ) ;
inline void operator -= ( BIG &B ) ;
inline void operator *= ( BIG &B ) ;
inline void operator /= ( BIG &B ) ;
inline void operator >>= ( BIG &B ) ;
inline void operator <<= ( BIG &B ) ;
inline void CLear_front_zero( void ) ;
};
}
//#define Mynamespace::BIG BIG
//#define BIG Mynamespace::BIG
inline Mynamespace::BIG& Mynamespace::BIG::operator + ( BIG &B )
{
register string C;
register int t = 0;
for (int i = 0; i < A.size() || i < B.A.size(); ++i) {
if (i < A.size()) t += A[i];
if (i < B.A.size()) t += B.A[i];
C += ( 48 + t % 10 ) ;
t /= 10; //多余进位
}
while (t) C = C + 49 ;
return C;
}
inline Mynamespace::BIG& Mynamespace::BIG::operator - ( BIG &B )
{
bool tmp = (*this) > B ;
if ( tmp )
if ( tmp == -1 ) { //比B小
BIG ret = B - (*this) ;
ret.A = ret.A ;
return ret ;
} else if ( (*this) != B ) { //比B大
while ( A.size() > B.A.size() ) B.A = "0" + B.A ; //补零
Mynamespace::BIG C;
register int i ;
const int SIZE = A.size() ;
for ( i = SIZE - 1 ; i >= 0 ; --i )
if ( B.A[i] <= A[i] )C.A[i] = B.A[i] - A[i] + 48 ;
else {
register int j = i + 1 ;
while ( --A [ j ] )if ( A [j] < 0 )A [j] += 10,--A [++j] ; //退,退,退!
C.A[i] = A[i] - B[i] + 10 ;
}
C.CLear_front_zero() ;
return C ;
} else return 0 ;
}
inline Mynamespace::BIG& Mynamespace::BIG::operator * ( BIG &B )
{
while ( B.A[0] == 48 ) B.A.pop(), A += 48 ;
if ( B.A = "1" ) return A ;
BIG ret = 0 ;
BIG base = *(this) ;
while ( B != 0 ) {
if( ( B.A[B.A.size()-1] - '0' ) % 2 ) ret += base ; //B&1
base <<= 1 ;
B >>= 1 ;
}
return ret ; // mlog₂n => m 是>>= 和 <<= 的时间 n是B
}
inline Mynamespace::BIG& Mynamespace::BIG::operator / ( BIG &B )
{
if ( strcmp ( A.c_str(), B.A.c_str() ) == -1 ) return 0 ;
else if ( A == B.A ) return 1 ;
BIG ret;
ret.A.clear() ;
int lena = A.size() ;
register BIG tmp = 0 ;
register char ch = 48 ;
for ( register int i = 0 ; i < lena ; i++ )
if ( ( tmp = tmp * 10 + ( A[i] - 48 ) ) >= B ) {
ch = 48 ;
do {
++ch;
} while ( tmp -= B >= B );
ret.A += ch ;
}
ret.CLear_front_zero() ;
}
inline Mynamespace::BIG CLear_front_zero( void )
{
s.erase ( 0, s.find_first_not_of ( "0" ) ) ;
}
更多见专栏获取
BIG_CMP_BYTEOPTIATOR
BIG_SWAP_AND_INOUT_OTHER