TLE求调
查看原帖
TLE求调
680050
liuzhipu2013楼主2025/1/17 17:55
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef long long LL;
/*
第一步:任意给定两个正整数;判断它们是否都是偶数。若是,则用2约简;直到有一个不是偶数为止。 进入第二步。
第二步:如果有一个偶数,则用2约简,直到变成奇数为止。 进入第三步。
第三步:以较大的数减较小的数,此时一定变成一奇一偶。 如果那个偶数不是0,回到第二步。
如果第三步得到的偶数是0,则第一步中约掉的若干个2的积与第三步中最后得到的奇数的乘积就是所求的最大公约数。
*/
const int M=10010;
struct BIG{
	int l;
	int num[M];
	BIG(){
		l=1;
		num[1]=0;
	}
	void set(long long n){
		l=0;
		if(n==0){l=1;num[1]=0;}
		while(n>0){
			l++;
			num[l]=n%10;
			n/=10;
		}
	}
	void set(string s){
		l=s.size();
		for(int i=1;i<=l;i++)
			num[i]=s[l-i]-'0';
	}
	void print(){
		for(int i=l;i>=1;i--) cout<<num[i];
	}
};
bool operator<(const BIG &a,const BIG &b){
	if(a.l!=b.l) return a.l<b.l;
	for(int i=a.l;i>=1;i--)
		if(a.num[i]!=b.num[i]) return a.num[i]<b.num[i];
	return false;
}
bool operator>(const BIG &a,const BIG &b){return b<a;}
bool operator==(const BIG &a,const BIG &b){return !(b<a)&&!(a<b);}
bool operator!=(const BIG &a,const BIG &b){return b<a||a<b;}
bool operator<=(const BIG &a,const BIG &b){return !(b<a);}
bool operator>=(const BIG &a,const BIG &b){return !(a<b);}
BIG operator+(const BIG &a,const BIG &b){
	BIG c;
	c.l=max(a.l,b.l);
	int u=0;
	for(int i=1;i<=c.l;i++){
		int t=u;
		if(i<=a.l) t+=a.num[i];
		if(i<=b.l) t+=b.num[i];
		c.num[i]=t%10;
		u=t/10;
	}
	if(u>0){
		c.l++;
		c.num[c.l]=u;
	}
	return c;
}
BIG operator-(const BIG &a,const BIG &b){
	BIG c;
	c.l=max(a.l,b.l);
	int u=0;
	for(int i=1;i<=c.l;i++){
		int t=a.num[i]-u;
		if(i<=b.l) t-=b.num[i];
		if(t<0){
			c.num[i]=t+10;
			u=1;
		}
		else{
			c.num[i]=t;
			u=0;
		}
	}
	while(u>0){
		c.l++;
		c.num[c.l]=u%10;
		u/=10;
	}
	return c;
}
BIG operator*(const BIG &a,const int &b){
	BIG c;
	c.l=a.l;
	long long u=0;
	for(int i=1;i<=c.l;i++){
		long long t=1ll*a.num[i]*b+u;
		c.num[i]=t%10;
		u=t/10;
	}
	while(u>0){
		c.l++;
		c.num[c.l]=u%10;
		u/=10;
	}
	return c;
}
BIG operator/(const BIG &a,const int &b){
	BIG c;
	c.l=a.l;
	long long r=0;
	for(int i=c.l;i>=1;i--){
		long long t=r*10+a.num[i];
		c.num[i]=t/b;
		r=t%b;
	}
	while(c.num[c.l]==0&&c.l>1) c.l--;
	return c;
}
BIG a,b;
string s1,s2;
int main(){
	cin >> s1 >> s2;
	a.set(s1);
	b.set(s2);
	int cnt = 0;
	while (a.num[1]%2==0 && b.num[1]%2==0){
		cnt++;
		a = a / 2;
		b = b / 2;
	}
	while (true){
		while (a.num[1]%2==0)
			a = a / 2;
		while (b.num[1]%2==0)
			b = b / 2;
		if (a < b)
			swap(a, b);
		a = a - b;
		if (a.num[1]==0&&a.l==1)
			break;
	}
	for(int i=1;i<=cnt;i++) b=b*2;
	b.print();
	return 0;
}
2025/1/17 17:55
加载中...