[ACM] 高精度模板

1.高精度加法

复杂度O(n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
#define DEBUG(x) cerr << #x << "=" << x << endl
const int L = 110;

string a, b;

string add(string a, string b)//只限两个非负整数相加
{
   
   
    string ans;
    int na[L] = {
   
   0};
    int nb[L] = {
   
   0};
    int la = a.size();
    int lb = b.size();
    for (int i = 0; i < la; i++)
        na[la - 1 - i] = a[i] - '0';
    for (int i = 0; i < lb; i++)
        nb[lb - 1 - i] = b[i] - '0';
    int lmax = la > lb ? la : lb;
    for (int i = 0; i < lmax; i++)
    {
   
   
        na[i] += nb[i];
        na[i + 1] += na[i] / 10;
        na[i] %= 10;
    }
    if (na[lmax]) lmax++;
    for (int i = lmax - 1; i >= 0; i--)
        ans += na[i] + '0';
    return ans;
}

int main()
{
   
   
    ios::sync_with_stdio(false);
    cin.tie(0);
    while (cin >> a >> b)
        cout << add(a, b) << endl;
    return 0;
}

2.高精度减法

复杂度O(n)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
//#define DEBUG(x) cerr << #x << "=" << x << endl
typedef unsigned char BYTE;

BYTE a[10005] = {
   
   0};
BYTE b[10005] = {
   
   0};
BYTE *pa = 0, *pb = 0;
int la, lb;
int sign;

int main()
{
   
   
    scanf("%s", a);
    scanf("%s", b);
    la = strlen((char*)a);
    lb = strlen((char*)b);
    int i;
    for (int i = 0; i < la; i++)
    {
   
   
        a[i] -= '0';
    }
    for (int i = 0; i < lb; i++)
    {
   
   
        b[i] -= '0';
    }
    if (la > lb)
    {
   
   
        sign = 1;
    }
    else if (la < lb)
    {
   
   
        sign = 0;
    }
    else
    {
   
   
        sign = -1;
        int BCT;
        for (BCT = 0; BCT < la; BCT++)
        {
   
   
            if (a[BCT] > b[BCT])
            {
   
   
                sign = 1;
                break;
            }
            if (a[BCT] < b[BCT])
            {
   
   
                sign = 0;
                break;
            }
        }
        if (sign == -1)
        {
   
   
            printf("0");
            return 0;
        }
    }
    if (sign == 1)
    {
   
   
        pa = a;
        pb = b;
    }
    else
    {
   
   
        pa = b;
        pb = a;
        int buf;
        buf = la;
        la = lb;
        lb = buf;
    }
    memmove(pb + la - lb, pb, lb);
    memset(pb, 0, la - lb);
    for (int i = 0; i < la; i++)
    {
   
   
        pb[i] = 9 - pb[i];
    }
    pb[la - 1]++;
    for (int i = la - 1; i >= 0; i--)
    {
   
   
        pa[i] += pb[i];
        if (pa[i] >= 10)
        {
   
   
            pa[i] -= 10;
            if (i != 0)
            {
   
   
                pa[i - 1]++;
            }
        }
    }
    for (int i = 0; i < la; i++)
    {
   
   
        pa[i] += '0';
    }
    if (sign == 0)
    {
   
   
        printf("-");
    }
    for (int i = 0; i < la; i++)
    {
   
   
        if (pa[i] != '0')
        {
   
   
            printf((char*)pa + i);
            return 0;
        }
    }
    return 0;
}

3.高精度乘法

复杂度O(n*n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
//#define DEBUG(x) cerr << #x << "=" << x << endl
const int L = 110;

string a, b;

string mul(string a, string b)
{
   
   
    string s;
    int na[L];
    int nb[L];
    int nc[L];
    int La = a.size();
    int Lb = b.size();
    fill(na, na + L, 0);
    fill(nb, nb + L, 0);
    fill(nc, nc + L, 0);
    for (int i = La - 1; i >= 0; i--)
        na[La - i] = a[i] - '0';
    for (int i = Lb - 1; i >= 0; i--)
        nb[Lb - i] = b[i] - '0';
    for (int i = 1; i <= La; i++)
    {
   
   
        for (int j = 1; j <= Lb; j++)
        {
   
   
            nc[i + j - 1] += na[i] * nb[j];
        }
    }
    for (int i = 1; i <= La + Lb; i++)
    {
   
   
        nc[i + 1] += nc[i] / 10;
        nc[i] %= 10;
    }
    if (nc[La + Lb])
       s += nc[La + Lb] + '0';
    for (int i = La + Lb - 1; i >= 1; i--)
        s += nc[i] + '0';
    return s;
}

int main()
{
   
   
    while (cin >> a >> b)
        cout << mul(a, b) << endl;
    return 0;
}

4.高精度乘法FFT优化

复杂度O(nlogn)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
//#define DEBUG(x) cerr << #x << "=" << x << endl
#define L(x) (1 << (x))
const double PI = acos(-1.0);
const int maxn = 133015;

double ax[maxn], ay[maxn], bx[maxn], by[maxn];
char sa[maxn / 2], sb[maxn / 2];
int sum[maxn];
int x1[maxn], x2[maxn];
string a, b;

int max(int a, int b)
{
   
   
    return a > b ? a : b;
}

int revv(int x, int bits)
{
   
   
    int ret = 0;
    for (int i = 0; i < bits; i++)
    {
   
   
        ret <<= 1;
        ret |= x & 1;
        x >>= 1;
    }
    return ret;
}

void fft(double *a, double *b, int n, bool rev)
{
   
   
    int bits = 0;
    while (1 << bits < n)
        ++bits;
    for (int i = 0; i < n; i++)
    {
   
   
        int j = revv(i, bits);
        if (i < j)
        {
   
   
            swap(a[i], a[j]);
            swap(b[i], b[j]);
        }
    }
    for (int len = 2; len <= n; len <<= 1)
    {
   
   
        int half = len >> 1;
        double wmx = cos(2 * PI / len);
        double wmy = sin(2 * PI / len);
        if (rev)
            wmy = -wmy;
        for (int i = 0; i < n; i += len)
        {
   
   
            double wx = 1;
            double wy = 0;
            for (int j = 0; j < half; j++)
            {
   
   
                double cx = a[i + j];
                double cy = b[i 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值