Informatics Olympiad 1308: [Example 1.5] High precision divided by high precision divided by high precision

 Title Portal

1308: [Example 1.5] High precision removal


Time limit: 1000 ms Memory limit: 65536 KB
Commits: 6085 Passes: 2685 

【Title description】

Divide high precision by high precision and find their quotient and remainder.

【Enter】

Enter two positive integers below 300 digits.

【Output】

Output quotient and remainder.

【Input example】

1231312318457577687897987642324567864324567876543245671425346756786867867867
1231312318767141738178325678412414124141425346756786867867867

[Sample output]

999999999748590
179780909068307566598992807564736854549985603543237528310337

【source】


No

Submit  statistics 

Baba Comment: We made a problem that we thought was difficult before (the popularization group does not require high precision divided by high precision, and has not tested this knowledge point for many years), there is a lot of progress, I feel very good, but do Questions need to be strengthened in terms of details and time to consider AC.

The child said that the process must be written on paper first, and the algorithm can be simulated through the process on paper, so that it can be done more quickly. Pay attention to it later.

AC code

#include <iostream>
#include <cstdio>
#include <string>
#define SIZE (int)1e3 + 10
using namespace std;
int temp1[SIZE], a[SIZE], b[SIZE] = {1, 4}, temp2[SIZE] = {2, 1, 4}, c[SIZE], d[SIZE];
string s1, s2;

int f1();
int chu(const int &, const int &, int *);
void jian(int *);
void fan(int *);
void print(const int *);
bool pan_duan(const int &, const int &, int *);
//void define(int *, int *);
//void jian(const int &, const int &);
//void jian_fa(int *, int *);

int main() {
	freopen("cpp.in", "r", stdin);
	freopen("cpp.out", "w", stdout);
//	cout << chu(1, temp2[0], temp2) << endl;
//	print(temp1);
//	return 0;
	cin >> s1 >> s2;
	a[0] = s1.size();
	b[0] = s2.size();
	for (int i = 0; i < a[0]; ++i) {
		a[i + 1] = s1[i] - '0';
	}
	for (int i = 0; i < b[0]; ++i) {
		b[i + 1] = s2[i] - '0';
	}
//	for (int i = 1; i <= a[0] / 2; ++i) {
//		swap(a[i], a[a[0] - i + 1]);
//	}
//	for (int i = 1; i <= b[0] / 2; ++i) {
//		swap(b[i], b[b[0] - i + 1]);
//	}
	int n = f1();
	for (int i = 1; i <= c[0]; ++i) {
		printf("%d", c[i]);
	}
	printf("\n");
	for (int i = n; i <= a[0]; ++i) {
		printf("%d", a[i]);
	}
	printf("\n");
	return 0;
}

int f1() {
	int left = 1, right = b[0];
	bool ok = false;
	while (1) {
//		printf("%d ", right);
		while (!pan_duan(left, right, a)) {
			if (ok) {
				printf("0");
			}
			++right;
		}
//		printf("%d\n", right);
		if (right > a[0]) {
			break;
		}
//		c[++c[0]] = chu(left, right, a);
//		printf("%d # ", chu(left, right, a));
		printf("%d", chu(left, right, a));
		int r = right;
		for (int i = temp1[0]; i >= 1; --i) {
			a[r--] = temp1[i];
		}
//		cout << r << " " << right << " ";
//		print(a);
		++right;
		left = (++r);
//		cout << "#" << left << "#" << endl;
		ok = true;
	}
	return left;
}
int chu(const int &left, const int &right, int *a) {
	int ans = 0;
	temp1[0] = right - left + 1;
	for (int i = 0; i < temp1[0]; ++i) {
		temp1[i + 1] = a[i + left];
	}
//	cout << "###";
//	print(temp1);
	while (pan_duan(1, temp1[0], temp1)) {
//		cout << "###";
//		print(temp1);
//		print(b);
		jian(temp1);
		++ans;
	}
	return ans;
}
void jian(int *x) {
	fan(x), fan(b);
//	print(x);
//	print(b);
	for (int i = 1; i <= x[0]; ++i) {
		x[i] -= b[i];
		if (x[i] < 0) {
			x[i] += 10;
			--x[i + 1];
		}
	}
	while (x[x[0]] == 0 && x[0] > 1) {
		--x[0];
	}
	fan(x), fan(b);
}
bool pan_duan(const int &left, const int &right, int *x) {
	int n = right - left + 1;
	if (n > b[0]) {
		return true;
	}
	if (n < b[0]) {
		return false;
	}
	for (int i = 0; i < b[0]; ++i) {
		if (x[left + i] < b[i + 1]) {
			return false;
		}
		if (x[left + i] > b[i + 1]) {
			return true;
		}
	}
	return true;
}
void fan(int *x) {
	for (int i = 1; i <= x[0] / 2; ++i) {
		swap(x[i], x[x[0] - i + 1]);
	}
}
void print(const int *x) {
//	cout << x[0] << " ";
	for (int i = 1; i <= x[0]; ++i) {
		printf("%d", x[i]); 
	}
	printf("\n\n");
}

/*
void jian(const int &left, const int &right) {
	temp1[0] = right - left + 1;
	for (int i = 0; i < temp1[0]; ++i) {
		temp1[i + 1] = a[left + i];
	}
	while (pan_duan(1, temp1[0], temp1)) {
		jian_fa(temp1, b);
	}
}
void jian_fa(int *x, int *y) {
	fan(x), fan(y);
	
	fan(x), fan(y);
}
void define(int *x, int *y) {
	for (int i = 1; i <= y[0]; ++i) {
		x[i] = y[i];
	}
}
void fan(int *x) {
	for (int i = 1; i <= x[0] / 2; ++i) {
		swap(x[i], x[a[0] - i + 1]);
	}
}
*/

 

Posted 33 original articles · liked 0 · visits 167

Guess you like

Origin blog.csdn.net/weixin_42790071/article/details/105519783