社区讨论

80pts求条!!高精除写不来!!QAQ

P1932A+B A-B A*B A/B A%B Problem参与者 3已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
6 条
当前快照
1 份
快照标识符
@mifpins3
此快照首次捕获于
2025/11/26 15:52
3 个月前
此快照最后确认于
2025/11/26 17:09
3 个月前
查看原帖
CPP
// P1932 A+B A-B A*B A/B A%B Problem
#include <bits/stdc++.h>
#define int long long
#define AC 0
using namespace std;

const int maxn = 100000 + 10;

int a[maxn], b[maxn], c[maxn];

string add(string as, string bs);
string sub(string as, string bs);
string mul(string as, string bs);
string div(string as, string bs);
string mod(string as, string bs);

bool check(int aa[], int bb[]);
bool chick(string as, string bs);

string as, bs;

signed main()
{
	cin >> as >> bs;
	
	printf("%s\n", add(as, bs).c_str());
	printf("%s\n", sub(as, bs).c_str());
	printf("%s\n", mul(as, bs).c_str());
	printf("%s\n", div(as, bs).c_str());
	printf("%s\n", mod(as, bs).c_str());
	
	return AC;
}

string add(string as, string bs){
	string ans = "";
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	
	a[0] = as.length();
	b[0] = bs.length();
	int k(0);
	for (int i = as.length() - 1; i >= 0; i --){
		if (as[i] >= '0' and as[i] <= '9') {a[++ k] = (as[i] - '0');}
	}
	k = 0;
	for (int i = bs.length() - 1; i >= 0; i --){
		if (bs[i] >= '0' and bs[i] <= '9') {b[++ k] = (bs[i] - '0');}
	}
	
	c[0] = max(a[0], b[0]) + 1;
	for (int i = 1; i <= c[0]; i ++){
		c[i] += a[i] + b[i];
		c[i + 1] += c[i] / 10;
		c[i] %= 10;
	}
	if (c[c[0]] == 0) {c[0] -= 1;}
	
	for (int i = c[0]; i >= 1; i --) {ans += (c[i] + '0');}
	return ans;
}

string sub(string as, string bs){
	string ans = "";
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	
	a[0] = as.length();
	b[0] = bs.length();
	int k(0);
	for (int i = as.length() - 1; i >= 0; i --){
		if (as[i] >= '0' and as[i] <= '9') {a[++ k] = (as[i] - '0');}
	}
	k = 0;
	for (int i = bs.length() - 1; i >= 0; i --){
		if (bs[i] >= '0' and bs[i] <= '9') {b[++ k] = (bs[i] - '0');}
	}
	
	if (check(a, b)){
		c[0] = a[0];
		for (int i = 1; i <= c[0]; i ++){
			if (a[i] < b[i]) {a[i + 1] -= 1; a[i] += 10;}
			c[i] = a[i] - b[i];
		}
	}
	else {
		ans += '-';
		c[0] = b[0];
		for (int i = 1; i <= c[0]; i ++){
			if (b[i] < a[i]) {b[i + 1] -= 1; b[i] += 10;}
			c[i] = b[i] - a[i];
		}
	}
	
	while (c[c[0]] == 0 and c[0] > 1) {c[0] -= 1;}
	for (int i = c[0]; i >= 1; i --) {ans += (c[i] + '0');}
	return ans;
}

string mul(string as, string bs){
	string ans = "";
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	
	a[0] = as.length();
	b[0] = bs.length();
	int k(0);
	for (int i = as.length() - 1; i >= 0; i --){
		if (as[i] >= '0' and as[i] <= '9') {a[++ k] = (as[i] - '0');}
	}
	k = 0;
	for (int i = bs.length() - 1; i >= 0; i --){
		if (bs[i] >= '0' and bs[i] <= '9') {b[++ k] = (bs[i] - '0');}
	}
	
	c[0] = a[0] + b[0] + 5;
	for (int i = 1; i <= a[0]; i ++){
		for (int j = 1; j <= b[0]; j ++){
			int x = i + j - 1;
			c[x] += a[i] * b[j];
			c[x + 1] += c[x] / 10;
			c[x] %= 10;
		}
	}
	
	while (c[c[0]] == 0 and c[0] > 1) {c[0] -= 1;}
	for (int i = c[0]; i >= 1; i --) {ans += (c[i] + '0');}
	return ans;
}

string div(string as, string bs){
	string ans = "";
	
	if (!check(a, b)) {ans = "0";}
	
	string s = "0";
	while (chick(as, mul(add(s, "1"), bs))) {s = add(s, "1");}
	
	ans = s;
	
	return ans;
}

string mod(string as, string bs){
	string ans = "";
	
	ans = sub(as, mul(bs, div(as, bs)));
	
	return ans;
}



bool check(int aa[], int bb[]){
	if (aa[0] > bb[0]) {return true;}
	else if (aa[0] < bb[0]) {return false;}
	
	for (int i = aa[0]; i >= 1; i --){
		if (aa[i] > bb[i]) {return true;}
		else if (aa[i] < bb[i]) {return false;}
	}
	
	return true;
}

bool chick(string as, string bs){
	int ax[maxn], bx[maxn];
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	
	ax[0] = as.length();
	bx[0] = bs.length();
	int k(0);
	for (int i = as.length() - 1; i >= 0; i --){
		if (as[i] >= '0' and as[i] <= '9') {ax[++ k] = (as[i] - '0');}
	}
	k = 0;
	for (int i = bs.length() - 1; i >= 0; i --){
		if (bs[i] >= '0' and bs[i] <= '9') {bx[++ k] = (bs[i] - '0');}
	}
	return check(ax, bx);
}

回复

6 条回复,欢迎继续交流。

正在加载回复...