|
36鱼币
大整数加法运算
窗体底端
【问题描述】
给定测试数据若干组,每组有两个100位以内的正整数,要求程序给出这两个正整数相乘的结果。
【输入格式】
第一行为数据的组数 N,后面有N组测试数据,每组测试数据占一行,每行有两个100位以内的十进制整数。
【输出格式】
输出每一组测试数据的运算结果,每行一个结果。
【示例】
[输入]
6
47858628539074 212821064467
51357484452396 62369401380260
41972493335 769524402008059
16223931314069 936153278101
11775956124 981003321643
4648312830247 63220212506777
[输出]
10185324269616473782483558
3203135561691948572664102960
32298857834403116993786765
15188086483351158930902969
11552252073166227591732
293867324926193383538083919
我用的是vs2019
#include<iostream>
#include<string>
using namespace std;
const int MAX = 100;
class bigint
{
public:
bigint() { length = 0; }
bigint(int a[],int n);
~bigint() {};
friend bigint addition(bigint a, bigint b);
void Printlist();
private:
int data[MAX];
int length;
};
bigint::bigint(int a[],int n)
{
length = n;
if (length > MAX)throw"yichu";
n--;
for (int i = 0; i < length; i++)
data[length - i] = a[i];
}
bigint addition(bigint A, bigint B)
{
int flag = 0, i = 0;
int n, m,len;
bigint C;
n = A.length;
m = B.length;
len = n > m ? n : m;
for (i = 0; i < len; i++)
{
C.data[i] = A.data[i] + B.data[i];
if (C.data[i] > 9)
{
C.data[i] -= 10;
C.data[i + 1] += 1;
}
}
if (C.data[len] != 0)
len++;
return C;
}
void bigint::Printlist()
{
for (int i = length - 1; i >= 0; i--)
cout << data[i] << endl;
}
int main()
{
string a, b;
int a1[MAX], b1[MAX];
cout << "请输入1个大数:";
cin >> a;
cout << endl;
cout << "请输入第二个大数:";
cin >> b;
int lena, lenb;
lena = a.size();
lenb = b.size();
for (int i = 0; i < lena; i++)
{
a1[i] = a[i] - '48';
}
for (int i = 0; i < lenb; i++)
{
b1[i] = b[i] - '48';
}
bigint A(a1, lena), B(b1, lenb);
bigint C;
C = addition(A, B);
cout << "结果:";
C.Printlist();
return 0;
}
fft算法,最快的乘法了,如果再不行,我也没办法。
- #include <iostream>
- #include <math.h>
- #include <string>
- #define PI (3.1415926535897932)
- typedef struct tagComplex
- {
- double X;
- double Y;
-
- tagComplex()
- {
- X = 0;
- Y = 0;
- }
-
- tagComplex(const struct tagComplex & c)
- {
- X = c.X;
- Y = c.Y;
- }
-
- tagComplex(double x,double y = 0.0)
- {
- X = x;
- Y = y;
- }
-
- struct tagComplex & operator = (double x)
- {
- X = x;
- Y = 0;
- return * this;
- }
-
-
- struct tagComplex & operator = (const tagComplex & c)
- {
- X = c.X;
- Y = c.Y;
- return * this;
- }
-
- struct tagComplex & operator += (const tagComplex & c)
- {
- X += c.X;
- Y += c.Y;
- return * this;
- }
-
- struct tagComplex & operator -= (const tagComplex & c)
- {
- X -= c.X;
- Y -= c.Y;
- return * this;
- }
-
- struct tagComplex & operator *= (const tagComplex & c)
- {
- COMPLEX t = * this;
- X = t.X * c.X - t.Y * c.Y;
- Y = t.X * c.Y + t.Y * c.X;
- return * this;
- }
-
- struct tagComplex & operator /= (double n)
- {
- X /= n;
- Y /= n;
- return * this;
- }
-
- void Mul(const tagComplex & a,const tagComplex & b)
- {
- * this = a;
- * this *= b;
- }
-
- operator double()
- {
- return sqrt(X*X + Y*Y);
- }
-
- operator long()
- {
- return (long)(X + 0.4);
- }
-
-
- }COMPLEX, *LPCOMPLEX;
- class CFFT
- {
- private:
- CFFT(const CFFT &);
- CFFT & operator = (const CFFT &);
-
- private:
- unsigned int BinaryRev(unsigned int ul,int len)
- {
- unsigned int nRet = 0;
-
- for(int i = 0 ; i < len ; i ++)
- {
- nRet <<= 1;
- nRet |= (ul & 1);
- ul >>= 1;
- }
-
- return nRet;
- }
-
- public:
- CFFT(){};
- ~CFFT(){};
-
- void FFT(LPCOMPLEX fft,long n,bool flag = false)
- {
- long i,j;
- long log2n = (long)(log(n)/log(2) + 0.4);
- COMPLEX omegaTemp;
-
- for(i = 0 ; i < n ; i ++)
- {
- j = BinaryRev(i,log2n);
-
- if(i < j)
- {
- omegaTemp = fft[i];
- fft[i] = fft[j];
- fft[j] = omegaTemp;
- }
- }
-
- for(i = 1 ; i <= log2n ; i++)
- {
- long m = (1 << i);
- COMPLEX omega_m(cos(2*PI/m),sin(2*PI/m));
-
- if(flag)
- {
- omega_m.Y = -omega_m.Y;
- }
-
- for(j = 0 ; j < n ; j += m)
- {
- COMPLEX omegaBase(1,0);
-
- for(long k = 0 ; k < m / 2 ; k ++)
- {
- long loc = j + k + m / 2;
-
- omegaTemp = omegaBase;
- omegaTemp *= fft[loc];
-
- fft[loc] = fft[j+k];
- fft[j+k] += omegaTemp;
- fft[loc] -= omegaTemp;
-
- omegaBase *= omega_m;
- }
- }
- }
-
- if(flag)
- {
- for(i = 0 ; i < n ; i++)
- {
- fft[i] /= n;
- }
- }
- }
-
- void Mul(std::string a,std::string b,std::string & out)
- {
- long len = 1;
- long i = a.length() > b.length() ? a.length() : b.length();
- long j = 0;
-
- while(i > len)
- {
- len <<= 1;
- }
-
- len <<= 1;
-
- char * buffer = new char[(len * sizeof(COMPLEX) * 3)];
- memset(buffer,0,(len * sizeof(COMPLEX) * 3));
- LPCOMPLEX f1 = (LPCOMPLEX)buffer;
- LPCOMPLEX f2 = f1 + len;
- LPCOMPLEX f3 = f2 + len;
-
-
-
- for(i = a.length()-1; i >= 0 ; i--)
- {
- f1[j++] = a[i] - 0X30;
- }
-
- for(j = 0, i = b.length()-1 ; i >= 0 ; i--)
- {
- f2[j++] = b[i] - 0X30;
- }
-
- FFT(f1,len);
- FFT(f2,len);
-
-
- for(i = 0 ; i < len ; i++)
- {
- f3[i].Mul(f1[i],f2[i]);
- }
-
- FFT(f3,len,true);
-
- out = "";
- typedef std::string::iterator SI;
- SI si = out.begin();
-
- long num = 0;
-
- for(i = 0 ; i < len ; i ++)
- {
- long n = f3[i];
- num += n;
- out.insert(si,(num % 10 + 0X30));
- num /= 10;
- }
-
- while(out[0] == 0X30 && out.length() > 1)
- {
- out.erase(si);
- }
- delete [] buffer;
- }
- };
- int main()
- {
- std::string a,b,c;
- //输入两个大整数
- std::cin >> a >> b;
-
- CFFT fft;
- fft.Mul(a,b,c);
- std::cout << c;
- return 0;
- }
复制代码
|
最佳答案
查看完整内容
fft算法,最快的乘法了,如果再不行,我也没办法。
|