#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std ;
typedef long long llong ;
const int size = 50000 ;
const int base = 10000 ; //高精度数字中每一个元素表示数字的几位,100就是2位。1000就是4位。
const int cap = 50000 ;
struct bignum {
int len ; int data[cap] ;
bignum():len(0) {}
bignum( int x ):len(0) {//小整数赋值//
for( ; x>0; x/=base) data[len++] = x % base ;
}
bignum( const bignum &x ):len(x.len) {//bignum拷贝赋值//
memcpy( data, x.data, len*sizeof(data[0]) ) ;
}
int &operator[]( int posi ) { //直接定义bignum
return data[posi] ;
}
int operator[]( int posi )const {
return data[posi] ;
}
};
int cmp( const bignum &A, const bignum &B ) //比较 两个高精度数的大小 1是A大,-1是B大 0是相等。
{
if( A.len != B.len ) return A.len > B.len ? 1 : - 1 ;
int i ; for( i=A.len-1; i>=0&&A[i]==B[i]; i-- ) ;
if( i < 0 ) return 0 ;//相等//
return A[i] > B[i] ? 1 : -1 ;
}
bignum operator+( const bignum &A, const bignum &B )
{
int i ; int carry = 0 ; bignum R ;
for( i=0; i<A.len||i<B.len||carry>0; i++ )
{
if( i < A.len ) carry += A[i] ;
if( i < B.len ) carry += B[i] ;
R[i] = carry % base ; carry = carry / base ;
}
R.len = i ; return R ;
}
bignum operator-( const bignum &A, const bignum &B )
{
int i ; int carry = 0 ; bignum R ;
R.len = A.len ;
for( i=0; i<R.len; i++ )
{
R[i] = A[i] - carry ; if( i < B.len ) R[i] -= B[i] ;
if( R[i] < 0 ) { carry = 1 ; R[i] += base ; }
else carry = 0 ;
}
while( R.len>0&&0==R[R.len-1] ) R.len-- ;//消除相减导致的前缀0
return R ;
}
//高精度乘单精度,单精度必须放在右边
bignum operator*( const bignum &A, const int &B )
{
int i ; llong carry = 0 ; bignum R ;
for( i=0; i<A.len||carry>0; i++ )
{
if( i<A.len ) carry += (llong)(A[i])*B ;
R[i] = carry % base ; carry = carry / base ;
}
R.len = i ; return R ;
}
bignum operator*( const bignum &A, const bignum &B )
{
int i,j ; llong carry = 0 ;
bignum R ;
for( i=0; i<A.len; i++ )
for (j=0;j<B.len;j++)
{
carry = (llong)(A[i])*B[j] +R[i+j];
R[i+j] = carry % base ; //这两步运算可以防止爆掉。
R[i+j+1] += carry / base ; ;
}
R.len = i+j-1 ;
while (R[R.len]) R.len++;
while (!R[R.len-1]) R.len--;
return R ;
}
bignum operator/( const bignum &A, const int &B )
{
int i ; llong carry = 0 ; bignum R ;
for( i=A.len-1; i>=0; i-- )
{
carry += A[i] ;
R[i] = carry / B ;
carry = carry - R[i]*B ; carry = carry * base ;
}
R.len = A.len ; while( R.len>0&&0==R[R.len-1] ) R.len-- ;//消除前缀0
return R ;
}
llong mod( const bignum &A, const int &B )
{
int i ; llong carry = 0 ;
for( i=A.len-1; i>=1; i-- )
{
carry += A[i] ; carry = carry % B ; carry *= base ;
}
carry = carry + A[i] ; carry = carry % B ;
return carry ;
}
istream &operator>>( istream &in, bignum &x )
{
char ch ;
for( x=0; in>>ch; )
{
x = x*10 + (ch-'0') ;
if( in.peek() <= ' ' ) break ;//注意是空格,表明以空格作为数的间隔。//
}
return in ;
}
ostream &operator<<( ostream &out, const bignum &x )
{
out << ( 0==x.len ? 0 : x[x.len-1] ) ; //首位不需要补0
for( int i=x.len-2; i>=0; i-- ) //
for( int j=base/10; j>0; j/=10 ) //如果不够base位,需要补0.所以一位一位输出。
out << x[i]/j%10 ;
return out ;
}
void mytostr( string &str, const bignum &x )
{
str.clear() ; char temp[15] ;
if( 0 == x.len ) str += '0' ;
else
{
sprintf( temp, "%d", x[x.len-1] ) ;
str += temp ;
}
for( int i=x.len-2; i>=0; i-- )
for( int j=base/10; j>0; j/=10 )
str += char(x[i]/j%10+'0') ;
str.reserve() ;
}
int main()
{
bignum a,b,c;
cin>>a;
cin>>b;
c=a*b;
cout<<c<<endl;
return 0 ;
}
全能万进制高精度模板
最新推荐文章于 2019-10-02 07:27:59 发布