题目描述
公牛在数学方面比奶牛强很多,他们自称可以计算很大的整数之间的乘法,并得到精确的结果。农夫约翰想知道他们的答案是否正确。请你帮助他检查公牛的答案。读入2个正整数(不大于10^40),计算他们的乘积,输出一个自然数(不能含有多余的零)。
约翰农夫让你自己做这个工作。
输入
第1…2行:每行包含一个十进制数
输出
第1行:输出两个数乘积
Code
#pragma GCC optimize(3)
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int Maxn = 100510;
struct fantastic {int len, s[Maxn];fantastic(){memset( s, 0, sizeof(s) );len = 1;}fantastic( int num ){*this = num;}fantastic( const char* num ){*this = num;}fantastic operator =( int num ){char s[Maxn];sprintf( s, "%d", num );*this = s;return(*this);}string str() const{string res = "";for ( int i = 0; i < len; i++ )res = (char) (s[i] + '0') + res;if ( res == "" )res = "0";return(res);}void clean(){while ( len > 1 && !s[len - 1] )len--;}fantastic operator =( const char * num ){len = strlen( num );for ( int i = 0; i < len; i++ )s[i] = num[len - i - 1] - '0';return(*this);}fantastic operator +( const fantastic &b ) const{fantastic c;c.len = 0;for ( int i = 0, g = 0; g || i < max( len, b.len ); i++ ){int x = g;if ( i < len )x += s[i];if ( i < b.len )x += b.s[i];c.s[c.len++] = x % 10;g = x / 10;}return(c);}fantastic operator *( const fantastic &b ){fantastic c;c.len = len + b.len;for ( int i = 0; i < len; i++ )for ( int j = 0; j < b.len; j++ )c.s[i + j] += s[i] * b.s[j];for ( int i = 0; i < c.len - 1; i++ ){c.s[i + 1] += c.s[i] / 10;c.s[i] %= 10;}c.clean();return(c);}fantastic operator -( const fantastic &b ){fantastic c;c.len = 0;for ( int i = 0, g = 0; i < len; i++ ){int x = s[i] - g;if ( i < b.len )x -= b.s[i];if ( x >= 0 )g = 0;else {g = 1;x += 10;}c.s[c.len++] = x;}c.clean();return(c);}bool operator <( const fantastic &b ) const{if ( len != b.len )return(len < b.len);for ( int i = len - 1; i >= 0; i-- )if ( s[i] != b.s[i] )return(s[i] < b.s[i]);return(false);}bool operator >( const fantastic &b ) const{return(b < *this);}bool operator <=( const fantastic &b ){return(!(b > *this) );}bool operator ==( const fantastic &b ){return(!(b < *this) && !(*this < b) );}fantastic operator +=( const fantastic &b ){*this = *this + b;return(*this);}
};
istream & operator >>( istream & in, fantastic & x )
{string s;in >> s;x = s.c_str();return(in);
}ostream & operator <<( ostream & out, const fantastic &x )
{out << x.str();return(out);
}int main()
{freopen( "bullmath.in", "r", stdin);freopen( "bullmath.out", "w", stdout);fantastic a, b;cin >> a >> b;cout << a * b;return 0;
}