bzoj1876: [SDOI2009]SuperGCD

传送门
哈,不用高精度的做法应该都会吧、
可是要高精度。
于是我们可以将A%B化为A-B^x-……
x为2的幂次。
这样用辗转相除法求出gcd

#include<cstring>  
#include<cmath>   
#include<cstdio>  
#include<iostream>  
#include<cstdlib>   
#include<algorithm>
#define ll long long
#define mo 1000000000
using namespace std;
struct bignum{
    ll a[1200];
    bool operator <= (bignum x){
        if (a[0]!=x.a[0]) return a[0]<x.a[0];
        for (int i=a[0];i>=1;i--)
            if (a[i]!=x.a[i]) return a[i]<x.a[i];
        return 1;
    }
    void operator *= (ll x){
        for (int i=1;i<=a[0];i++) a[i]*=x;
        for (int i=1;i<=a[0];i++){
            a[i+1]+=a[i]/mo;
            a[i]%=mo;
        }
        if (a[a[0]+1]) a[0]++;
    }
    void operator /= (ll x){
        ll y=0;
        for (int i=a[0];i>=1;i--){
            y=y*mo+a[i];
            a[i]=y/x;
            y%=x;
        }
        if (!a[a[0]]) a[0]--;
    }
    void operator -= (bignum x){
        for (int i=1;i<=a[0];i++) a[i]-=x.a[i];
        for (int i=1;i<=a[0];i++)
            if (a[i]<0) a[i]+=mo,a[i+1]--;
        while (!a[a[0]]&&a[0]) a[0]--;
    }
    void yw(char *b){
        ll x=0;
        int len=strlen(b);
        for (int i=len;i>0;i--){
            if (i%9==0){a[i/9+1]=x; x=0;}
            x=x*10+b[len-i]-48;       
        }
        a[1]=x; 
        a[0]=(len-1)/9+1;
    }
    void print(){
        printf("%d",a[a[0]]);
        for (int i=a[0]-1;i>=1;i--) printf("%09d",a[i]);
        printf("\n");   
    }
}c,d;
int j=0;
char a[10005],b[10005];
int main(){
    scanf("%s%s",&a,&b);
    c.yw(a); d.yw(b);
    if (c<=d) swap(c,d);
    while (d.a[0]!=0){
        while (!(d.a[1]&1)&&!(c.a[1]&1)) d/=2,c/=2,j++;
        while (!(d.a[1]&1)) d/=2;
        while (!(c.a[1]&1)) c/=2;
        if (c<=d) swap(c,d);
        c-=d;
        if (c<=d) swap(c,d);
    }
    for (;j>0;j--) c*=2;
    c.print();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值