其他分享
首页 > 其他分享> > P2152 [SDOI2009]SuperGCD

P2152 [SDOI2009]SuperGCD

作者:互联网

在这里插入图片描述
在这里插入图片描述
九位运算一次

#include<cstdio>
#include<cstring>
#include<algorithm>
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define _for(i,a,b) for(int i=(a);i<=(b);i++)
using namespace std;
const int MAXN=1200;
const int base=1e9;
struct bignum{
	int len,s[MAXN];
	bignum(){
		len=0;
		memset(s,0,sizeof(s));
	}
}; 
bignum operator - (bignum a,const bignum &b)
{
	for(int i=a.len;i>=1;i--)
	{
		a.s[i]-=b.s[i];
		if(a.s[i]<0) a.s[i+1]--,a.s[i]+=base;
	}
	while(!a.s[a.len]&&a.len>0) a.len--;//去掉前导零 
	return a;
 } 
bool judge(bignum a,bignum b)
{
	if(a.len!=b.len) return a.len>b.len;
	for(int i=a.len;i>=1;i--)
	 if(a.s[i]!=b.s[i]) return a.s[i]>b.s[i];
	return true;
}
bignum operator % (bignum a,bignum b)
{
	while(judge(a,b)) a=a-b;
	return a;
}

char str[10000+5];
void read(bignum &a)
{
	scanf("%s",str);
	reverse(str,str+strlen(str));
	int &len=a.len=0;
	for(int i=0,w;i<strlen(str);i++,w*=10)
	{
		if(i%9==0) len++,w=1;
		a.s[len]+=w*(str[i]-'0');
	}
}
void print(bignum a)
{
	printf("%d",a.s[a.len]);
	for(int i=a.len-1;i>=1;i--)
	 printf("%09d",a.s[i]);
	puts("");
}
int main()
{
	bignum a,b,c;
	read(a);read(b);
	while(b.len)
	{
		c=a%b;
		a=b;
		b=c;
	}
	print(a);
	return 0;
}

标签:return,P2152,int,len,bignum,str,SuperGCD,SDOI2009,include
来源: https://blog.csdn.net/weixin_42859413/article/details/94964345