1 条题解

  • 0
    @ 2023-4-15 18:00:49

    40pt

    def gcd(a,b):
        if b==0:
            return a
        return gcd(b,a%b)
    
    a=int(input())
    b=int(input())
    print(gcd(a,b))
    

    100pt

    def gcd(a,b):
        cnt2 = 0
        while a != b:
            if a < b:
                a, b = b, a
            aEven = (a % 2 == 0)
            bEven = (b % 2 == 0)
            if aEven and not bEven:
                a = a // 2
            elif not aEven and bEven:
                b = b // 2
            elif aEven and bEven:
                cnt2 += 1
                a = a // 2
                b = b // 2
            else:
                a = a - b
        while cnt2 > 0:
            a = a * 2
            cnt2 -= 1
        return a
    a=int(input())
    b=int(input())
    print(gcd(a,b))
    

    C++ 正解

    #include<bits/stdc++.h>
    //#define int long long
    using namespace std;
    int cnt,re;
    
    struct Bignum{
    	int num[10005],len;
    };
    Bignum c;
    inline void write(int N){
    	if (N < 0){
    		putchar('-');
    		N = -N;
    	}
    	if (N >= 10){
    		write(N / 10);
    	}
    	putchar(N % 10 + '0');
    }
    void read(Bignum &a) 
    {
    	string s;
    	cin>>s;
    	int l=s.size();a.len=l;
    	for(int i=0;i<l;i++)a.num[l-i]=s[i]-'0';
    }
    
    int compare(Bignum &a,Bignum &b)
    {
    	if(a.len!=b.len)return a.len<b.len;
    	for(int i=a.len;i>0;i--)
    	{
    		if(a.num[i]!=b.num[i])return a.num[i]<b.num[i];
    	}
    	return 2;
    }
    
    int pd(Bignum &a){return a.num[1]%2;}
    
    void chu(Bignum &a)
    {
    	re=0;
    	for(int i=a.len;i>=1;i--)
    	{
    		a.num[i]+=10*re;
    		re=a.num[i]%2;
    		a.num[i]/=2;
    		if(i==a.len&&a.num[i]==0)a.len--;
    	}
    }
    
    void cut(Bignum &a,Bignum &b)
    {
    	for(int i=a.len;i>=1;i--)
    	{
    		a.num[i]-=b.num[i];
    		if(a.num[i]<0)
    		{
    			for(int j=i+1;a.num[j-1]<0;j++)
    			{
    				a.num[j]--;
    				a.num[j-1]+=10;
    			}
    		}
    	}
    	for(int i=a.len;i>=1;i--)
    	{
    		if(a.num[i]!=0)break;
    		a.len--;
    	}
    }
    
    void mu(Bignum &a)
    {
    	re=0;
    	for(int i=1;i<=a.len;i++)
    	{
    		a.num[i]*=2;
    		a.num[i]+=re;
    		re=0;
    		if(a.num[i]>=10)re=a.num[i]/10,a.num[i]%=10;
    	}
    	if(re)a.num[++a.len]=re;
    }
    
    void out(Bignum &a)
    {
    	for(int i=a.len;i>=1;i--)write(a.num[i]);
    }
    
    void gcd(Bignum &a,Bignum &b)
    {
    	cnt=0;
    	while(compare(a,b)!=2)
    	{
    		if(compare(a,b)==1)
    		{
    			c=a;
    			a=b;
    			b=c;
    		}
    		int a1=pd(a),b1=pd(b);
    		if(!pd(a)&&pd(b))chu(a);
    		else if(pd(a)&&!pd(b))chu(b);
    		else if(!pd(a)&&!pd(b))cnt++,chu(a),chu(b);
    		else cut(a,b);
    	}
    	while(cnt--)mu(a);
    }
    
    signed main()
    {
    	Bignum a,b;
    	read(a);read(b);
        gcd(a,b);
    	out(a);
    	return 0;
    }
    
    • 1

    信息

    ID
    1264
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    37
    已通过
    7
    上传者