1. 程式人生 > >高精度乘法 【C++版(簡單模擬版和FFT快速版)和java版】

高精度乘法 【C++版(簡單模擬版和FFT快速版)和java版】

高精度乘法C++版

簡單模擬版(N^2複雜度):

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <memory.h>
using namespace std;
const int MAX=50001;
char sa[MAX],sb[MAX],ssum[2*MAX];
int lsum;
void bigchenfa(char sa[],char sb[])
{
    int a[MAX]={0},b[MAX]={0},sum[MAX*2]={0};
    int i,j,k ;
    int la=strlen(sa);
    int lb=strlen(sb);
    lsum=0;
    for(i=1,j=la-1;i<=la;i++,j--)
        a[i]=sa[j]-'0';
    for(i=1,j=lb-1;i<=lb;i++,j--)
        b[i]=sb[j]-'0';
    memset(sum,0,sizeof(sum));
    for(i=1;i<=la;i++)
        for(j=1,lsum=i-1;j<=lb;j++)
            sum[++lsum]+=b[j]*a[i];
    for(i=1;i<=lsum;i++)
        if(sum[i]>= 10)
        {
            if (sum[lsum]>= 10)
                lsum++;
            sum[i+1]+=sum[i]/10 ;
            sum[i]%=10;
        }
    for(i=lsum,j=1;i>=1;i--,j++)
            ssum[j]=sum[i];
}
int main(void)
{
    int i,j ;
    while(scanf("%s%s",sa,sb)!=EOF)
    {
        bigchenfa(sa,sb) ;
        for(i=1;i<=lsum;i++)
          printf("%d",ssum[i]);
    }
    return 0 ;
}


FFT加快版:

//FFT 大整數乘法
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;


const int N = 500005;
const double pi = acos(-1.0);

char s1[N],s2[N];
int len,res[N];

struct Complex
{
	double r,i;
	Complex(double r=0,double i=0):r(r),i(i) {};
	Complex operator+(const Complex &rhs)
	{
		return Complex(r + rhs.r,i + rhs.i);
	}
	Complex operator-(const Complex &rhs)
	{
		return Complex(r - rhs.r,i - rhs.i);
	}
	Complex operator*(const Complex &rhs)
	{
		return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);
	}
} va[N],vb[N];

void rader(Complex F[],int len)	//len = 2^M,reverse F[i] with  F[j] j為i二進位制反轉
{
	int j = len >> 1;
	for(int i = 1;i < len - 1;++i)
	{
		if(i < j) swap(F[i],F[j]);	// reverse
		int k = len>>1; 
		while(j>=k)
		{
			j -= k;
			k >>= 1;
		}
		if(j < k) j += k;
	}
}

void FFT(Complex F[],int len,int t)
{
	rader(F,len);
	for(int h=2;h<=len;h<<=1)
	{
		Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h));
		for(int j=0;j<len;j+=h)
		{
			Complex E(1,0);	//旋轉因子
			for(int k=j;k<j+h/2;++k)
			{
				Complex u = F[k];
				Complex v = E*F[k+h/2];
				F[k] = u+v;
				F[k+h/2] = u-v;
				E=E*wn;
			}
		}
	}
	if(t==-1)	//IDFT
		for(int i=0;i<len;++i)
			F[i].r/=len;
}

void Conv(Complex a[],Complex b[],int len) //求卷積
{
	FFT(a,len,1);
	FFT(b,len,1);
	for(int i=0;i<len;++i) a[i] = a[i]*b[i]; 
	FFT(a,len,-1);
}

void init(char *s1,char *s2)
{
	int n1 = strlen(s1),n2 = strlen(s2);
	len = 1;
	while(len < 2*n1 || len < 2*n2) len <<= 1;
	int i;
	for(i=0;i<n1;++i)
	{
		va[i].r = s1[n1-i-1]-'0';
		va[i].i = 0;
	}
	while(i<len)
	{
		va[i].r = va[i].i = 0;
		++i;
	}
	for(i=0;i<n2;++i)
	{
		vb[i].r = s2[n2-i-1]-'0';
		vb[i].i = 0;
	}
	while(i<len)
	{
		vb[i].r = vb[i].i = 0;
		++i;
	}
}

void gao()
{
	Conv(va,vb,len);
	memset(res,0,sizeof res);
	for(int i=0;i<len;++i)
	{
		res[i]=va[i].r + 0.5;
	}
	for(int i=0;i<len;++i)
	{
		res[i+1]+=res[i]/10;
		res[i]%=10;
	}
	int high = 0;
	for(int i=len-1;i>=0;--i)	
	{
		if(res[i])	
		{
			high = i;
			break;
		}
	}
	for(int i=high;i>=0;--i) putchar('0'+res[i]);
	puts("");
}


int main()
{
	while(scanf("%s %s",s1,s2)==2)
	{
		init(s1,s2);
		gao();
	}
	return 0;
}


java版:

import java.util.Scanner;
 import java.math.*;
 import java.text.*;
 public class Main { 
         public static void main(String[] args) {
                 Scanner cin=new Scanner(System.in);
                 BigInteger a,b;
                 while(cin.hasNext()){
                         a=cin.nextBigInteger();
                         b=cin.nextBigInteger();
                         System.out.println(a.multiply(b));
                         }
                         
                 }
 }