gjd
2024-04-14 19:46:03
发布于:浙江
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
constexpr int B = 9;
class Long{
private:
int *a;
int len = 0;
bool f=0;
const int MAXLEN = 10000;
//无符号相加
Long add(const Long& a,const Long& b)const{
Long c;
int len = max(a.len,b.len);
c.a[0] = 0;
for(int i = 0;i <= len;i++){
c.a[i] = a.a[i] + b.a[i] + c.a[i];
c.a[i+1] += c.a[i] / B;
c.a[i] %= B;
}
if(c.a[len+1])len++;
c.len = len;
c.f = a.f;
return c;
}
Long __reduce(const Long& a,const Long& b)const{
Long c;
int len = max(a.len,b.len);
for(int i = 0;i <= len;i++){
c[i] += a[i] - b[i];
if(c[i] < 0){
c[i+1]--;
c[i]+=B;
}
}
c.len = len;
while (c[c.len] == 0 && c.len!=0)c.len--;
return c;
}
Long reduce(const Long& a,const Long& b)const{
Long c;
if(a.gt(b,a)){
c = a.__reduce(b,a);
c.f = 1;
return c;
}
c = a.__reduce(a,b);
return c;
}
bool gt(const Long& a,const Long& b)const{ //无符号大于
if(a.len > b.len)return true;
if(b.len > a.len || a.eq(a,b))return false;
for(int i = a.len;i>=0;i--) {
if(a.a[i] < b.a[i])return false;
}
return true;
}
bool lt(const Long& a,const Long& b)const{ //无符号小于
if(a.len < b.len)return true;
if(b.len < a.len || a.eq(a,b))return false;
for(int i = a.len;i>=0;i--) {
if(a.a[i] > b.a[i])return false;
}
return true;
}
bool eq(const Long& a,const Long& b)const{ //无符号等于
int len = max(a.len,b.len);
for(int i=0;i<=len;i++){
if(a[i] != b[i]){
return false;
}
}
return true;
}
public:
Long(){
this->a = new int [this->MAXLEN];
memset(this->a,0,sizeof(int) * this->MAXLEN);
}
Long(int a){
this->a = new int [this->MAXLEN];
memset(this->a,0,sizeof(int) * this->MAXLEN);
int len = 0;
if(a < 0){
a = -a;
this->f = true;
}
for(int y = a;y > 0;y /= B){
this->a[len++] = y % B;
}
if(len == 0){
len++;
}
this->len = len - 1;
}
Long(const Long& other) {
len = other.len;
f = other.f;
a = new int[len + 1];
for (int i = 0; i <= len; i++) {
a[i] = other.a[i];
}
}
Long(Long&& other) noexcept {
len = other.len;
f = other.f;
a = other.a;
delete [] other.a;
other.a = nullptr;
}
Long& operator=(const Long& other) {
if (this == &other) {
return *this;
}
delete[] a;
len = other.len;
f = other.f;
a = new int[len + 1];
for (int i = 0; i <= len; i++) {
a[i] = other.a[i];
}
return *this;
}
Long& operator=(Long&& other) noexcept {
if (this == &other) {
return *this;
}
delete[] a;
len = other.len;
f = other.f;
a = other.a;
other.a = nullptr;
return *this;
}
~Long() {
delete[] this->a;
this->a = nullptr;
}
//取值
int& operator[](const int n){
return this->a[n];
}
const int& operator[](const int n) const{
return this->a[n];
}
//输入
friend istream& operator >>(istream &input,Long &a){
string arr;
input >> arr;
int len = arr.length() - 1;
if(arr[0] == '-'){
len--;
a.f = true;
}
a.len = len;
for(int i = 0;i <= len;i++){
a.a[len - i] = arr[a.f+i] - '0';
}
return input;
}
//输出
friend ostream& operator <<(ostream &output,const Long& a){
if(a.f)output << '-';
for(int i = a.len;i >= 0;i--){
output << (int)a.a[i];
}
return output;
}
//相加
friend Long operator +(const Long& a,const Long& b){
if(a == Long(0)){
return b;
}
if(b == Long(0)){
return a;
}
if((a > Long(0) && b < Long(0)) || (b > Long(0) && a < Long(0))){
return a.reduce(a,b);
}
return a.add(a,b);
}
//相减
friend Long operator -(const Long& a,const Long& b){
if(a > Long(0) && b < Long(0)){
return a.add(a,b);
}
return a.reduce(a,b);
}
//大于
friend bool operator >(const Long& a,const Long& b){
if(a.f == 0&&b.f == 1){
return true;
}else if(a.f == 1&&b.f == 0){
return false;
}
if(a.f == 1&&b.f == 1){
return !a.gt(a,b);
}
return a.gt(a,b);
}
//小于
friend bool operator <(const Long& a,const Long& b){
if(a.f == 0&&b.f == 1){
return false;
}else if(a.f == 1&&b.f == 0){
return true;
}
if(a.f == 1&&b.f == 1){
return !a.lt(a,b);
}
return a.lt(a,b);
}
//等于
friend bool operator==(const Long& a,const Long& b){
if(a.f != b.f){
return false;
}
int len = max(a.len,b.len);
for(int i=0;i<=len;i++){
if(a[i] != b[i]){
return false;
}
}
return true;
}
//不等
friend bool operator!=(const Long& a,const Long& b){
return !(a == b);
}
//相乘
friend Long operator*(const Long& a,const Long& b){
Long c;
if(!(a.f && b.f))c.f = a.f ^ b.f;
int len = a.len+b.len+1;
c.len = len;
for(int i=0;i<=a.len;i++){
for(int j=0;j<=b.len;j++){
c[j+i] += a[i] * b[j];
c[j+i+1] += c[j+i] / B;
c[j+i] %= B;
}
}
while (c[c.len] == 0 && c.len!=0)c.len--;
return c;
}
};
Long a,b;
int main(){
cin >> a >> b;
cout << (a*b);
return 0;
}
全部评论 1
hi,感谢分享。为了让他人更好理解,请补充对应的题目地址或题面信息,建议参考参考这篇如何写好题解帖子:https://www.acgo.cn/discuss/16535
2024-04-17 来自 浙江
0
有帮助,赞一个