求调,拜托了
查看原帖
求调,拜托了
934977
lichengxi1楼主2025/1/18 20:59
#include <vector>
#include <iostream>
#include <algorithm> 
using namespace std;

const int MAXN = 1e4 + 10;
struct Big_Int {
	int p[MAXN];
	int& operator[](int i) {return p[i]; }
	void scan() {
		string s;
		cin >> s;
		p[0] = s.size();
		s = " " + s;
		for(int i = 1; i <= p[0]; ++i) p[i] = s[s.size() - i] - '0';
	}
	void print() {
		for(int i = p[0]; i >= 1; --i) cout << p[i];
		cout << endl;
	}
	Big_Int operator-(Big_Int x) {
		Big_Int ans;
		ans[0] = max(p[0], x[0]);
		for(int i = 1; i <= ans[0]; ++i) {
			while(p[i] < x[i]) p[i] += 10, x[i + 1]--;
			ans[i] = p[i] - x[i];
		}
		while(ans[ans[0]] == 0 && ans[0] != 1) ans[0]--;
		return ans;
	}
	void mul2() {
		for(int i = 1; i <= p[0]; ++i) p[i] = p[i] * 2;
		for(int i = 1; i <= p[0]; ++i) {
			p[i + 1] += p[i] / 10;
			p[i] %= 10;
		}
		while(p[p[0]] > 9) {
			p[p[0] + 1] = p[p[0]] / 10;
			p[p[0]] %= 10;
			++p[0];
		}
	}
	void div2() {
		for(int i = p[0]; i >= 1; --i) {
			p[i - 1] += 10 * (p[i] % 2);
			p[i] /= 2;
		}
		while(p[0] && p[p[0]] == 0) --p[0];
	}
} a, b, ans;

bool cmp(Big_Int a, Big_Int b) {
	if(a[0] != b[0]) return a[0] < b[0];
	for(int i = a[0]; i >= 1; --i)
		if(a[i] < b[i]) return 1;
	return 0;
}

void copy(Big_Int &a, Big_Int &b) {
	Big_Int tmp = a;
	a = b, b = tmp;
}

Big_Int gcd(Big_Int a, Big_Int b) {
	Big_Int ans; int cnt = 0;
	if(cmp(a, b)) copy(a, b);
	while(b[0]) {
		if(a[1] % 2 == 0 && b[1] % 2 == 0) a.div2(), b.div2(), ++cnt;
		else if(a[1] % 2 == 0) a.div2();
		else if(b[1] % 2 == 0) b.div2();
		else a = a - b;
		if(cmp(a, b)) copy(a, b);
	}
	ans = a;
	while(cnt) ans.mul2(), --cnt;
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	a.scan(), b.scan(); 
	ans = gcd(a, b);
	ans.print();
	return 0;
}

WA + TLE 0pts

谢谢大佬

2025/1/18 20:59
加载中...