洛谷2152 [SDOI] Super GCD

题意:

高精GCD,两个数大小 ( 1 ≤ a , b ≤ 1 0 10000 ) (1 \leq a, b \leq 10^{10000}) (1a,b1010000)

思路:

其实挺玄学的。

首先想到的就是用最普通的辗转相除法做,在低精度情况下,辗转相除的时间复杂度应该是 O ( l g n ) O(lg_n) O(lgn)左右,但是辗转相除需要用到取模运算,在高精度下取模运算时间复杂度 O ( n 2 ) O(n^2) O(n2)左右, n n n是数字的位数,会大大拖慢其速度,就算压位也过不去。hack数据随便取fibonacci数列的连续两项输入, B o o m ! Boom! Boom!

然后是辗转相减,就是中国老祖宗的更相减损法。话说这种方法复杂度很玄学,只要输入一个极大的数和一个小的数就可以完美卡爆,但是就是过了这道题。(upd.复杂度一点问题都没有,因为假如某次两个都是奇数才需要相减,减完必定出现偶数又可以除二,所以总的复杂度是 O ( l e n ( n ) log ⁡ n ) O(len(n)\log n) O(len(n)logn),压位就过了)他的单个运算只需要:1.乘以2;2.除以2;3.大整数减法;4.比较大小,如此四种就行了,全都是 O ( l e n ( n ) ) O(len(n)) O(len(n))的复杂度。

更相减损法大致思路:(摘自洛谷题解传送门)
对于 a,b 的 GCD(a, b) 有:
[1]. 若 a 为奇数, b 为偶数, GCD(a, b) = GCD(a, b / 2)
表示 b 存在2这个因子而 aa 不存在,则将 bb 除以2,,不考虑因子2;
[2]. 若 a 为偶数, b 为奇数, GCD(a, b) = GCD(a / 2, b)
表示 a 存在2这个因子而 bb 不存在,则将 aa 除以2,不考虑因子2;
[3]. 若 a 为偶数, b 为偶数, GCD(a, b) = GCD(a / 2, b / 2)
表示 a,b 都存在2这个因子,则 GCD(a, b)也存在因子2,则将当前答案乘以2, a,b 都除以2;
[4]. 若 a 为奇数, b 为奇数, GCD(a, b) = GCD(a - b, b) (a > b)

代码:

第一次的辗转相除法,压位打萎了,就放个没有压的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N = 10010;
struct NUM{
    int len, a[N];
    void Getnum(){
        memset(a, 0, sizeof(a)); len = 0;
        char c = getchar();
        while (c < '0' || c > '9') c = getchar();
        while (c >= '0' && c <= '9')
            Leftshift(c-'0'), c = getchar();
    }
    bool operator <= (const NUM &x)const{
        if (len != x.len) return len < x.len;
        for (int i = len; i >= 1; i--)
            if (a[i] != x.a[i])
                return a[i] < x.a[i];
        return 1;
    }
    NUM operator - (NUM x){
        NUM y;
        memset(y.a, 0, sizeof(y.a));
        y.len = max(len, x.len);
        for (int i = 1; i <= y.len; i++){
            y.a[i] += a[i]-x.a[i];
            if (y.a[i] < 0){
                y.a[i+1] -= 1;
                y.a[i] += 10;
            }
        }
        while (y.a[y.len] == 0 && y.len > 0) y.len--;
        return y;
    }
    void Leftshift(int x){
        for (int i = len; i >= 1; i--)
            a[i+1] = a[i];
        a[1] = x;
        len++;
    }
    NUM operator % (NUM x){
        NUM tmp;
        memset(tmp.a, 0, sizeof(tmp.a)); tmp.len = 0;
        for (int i = len; i >= 1; i--){
            tmp.Leftshift(a[i]);
            while (x <= tmp) tmp = tmp-x;
        }
        return tmp;
    }
    void Print(){
        for (int i = len; i >= 1; i--)
            putchar(a[i]+'0');
        printf("\n");
    }
};

NUM Gcd(NUM x, NUM y)
{
    NUM tmp;
    while (y.len > 1 || y.a[1] != 0){
        tmp = y;
        y = x%y;
        x = tmp;
    }
    return x;
}

int main()
{
    NUM a, b;
    a.Getnum(); b.Getnum();
    Gcd(a, b).Print();
    return 0;
}

第二次的更相减损法,压位。有一个可以增加代码准确性的方法,就是先把要重载的运算符或者要在结构体里写的函数整理出来,先把框架写好,再一个一个写,写好一个就调试一个,避免因为函数太多,到后面连错在哪里都找不出来。我重构代码的时候就是这么做的,以前从没打过压位高精,还是一遍打出来了,很有用。然后就是要注意一些细节,在注释里写了

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 10010;
const int E = 1e5;
const int B = 5;
const int D[5] = {1, 10, 100, 1000, 10000};
struct NUM{
    int len, a[N];
    void Getnum(){
        string s; cin >> s;
        int sz = s.size();
        len = sz/B+(sz%B > 0);
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= len; i++){
            int l = max(0, sz-B*i);
            int r = sz-1-B*(i-1);
            for (int j = l; j <= r; j++)
                a[i] = a[i]*10+s[j]-'0';
        }
    }
    void Print(){
        for (int i = len; i >= 1; i--){
            for (int j = B-1; j >= 0; j--){
                if (a[i]/D[j] > 0){
                    printf("%d", a[i]);
                    break;
                }
                else if (i < len) putchar('0');
            }
        }
        putchar('\n');
    }
    void Div2(){
        for (int i = len; i >= 1; i--){
            a[i-1] += (a[i]&1)*E;
            a[i] >>= 1;
        }
        if (a[len] == 0) len--;
    }
    void Mul2(){
        for (int i = 1; i <= len; i++)
            a[i] <<= 1;
        for (int i = 1; i <= len; i++)
            if (a[i] > E){
                a[i+1]++;
                a[i] -= E;
            }
        if (a[len+1] > 0) len++;
    }
    bool operator < (const NUM &x)const{
        if (len != x.len) return len < x.len;
        for (int i = len; i >= 1; i--)
            if (a[i] != x.a[i])
                return a[i] < x.a[i];
        return 0;
    }
    NUM operator - (NUM x){
        NUM y;
        memset(y.a, 0, sizeof(y.a));
        y.len = max(len, x.len);
        for (int i = 1; i <= y.len; i++){
            y.a[i] += a[i]-x.a[i];
            if (y.a[i] < 0)
                y.a[i] += E, y.a[i+1]--;
        }
        while (y.a[y.len] == 0 && y.len > 1) y.len--;
        return y;
    }
    NUM operator -= (NUM x){
        *this = *this - x;
        return *this;
    }
}a, b;
int tot2;

void Swap(NUM &x, NUM &y){NUM tmp = x; x = y; y = tmp;}

NUM Gcd(NUM x, NUM y)
{
    while (y.len > 1 || y.a[1] != 0){
        if (x < y) Swap(x, y);
        if ((x.a[1]&1^1) && (y.a[1]&1^1))
            tot2++, x.Div2(), y.Div2();
        else if (x.a[1]&1^1) x.Div2();
        else if (y.a[1]&1^1) y.Div2();
        else x -= y;
        //这一句打成while就T飞,因为此时x和y都是奇数,相减的差一定是偶数,又可以做除2操作,但如果这里是while,就会一直不停减,假如x和y相差倍数太大,复杂度就增加了非常非常非常多
    }
    for (int i = 1; i <= tot2; i++) x.Mul2();
    return x;
}

int main()
{
    a.Getnum(); b.Getnum();
    tot2 = 0;
    Gcd(a, b).Print();
    return 0;
}

还要加油啊

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值