您的位置:首页 > 其它

POJ 1001 Exponentiation(高精度乘法)

2017-09-06 23:04 411 查看
题目链接:点击打开链接

首先去掉前导零,取出小数点位置,进行高精度乘法运算(这里也可以使用快速幂)

在最后的数中放入小数点并去掉后导零即可

不过这题数据确实有点坑,无限wa

AC代码如下:

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

using namespace std;

const double eps(1e-8);
typedef long long lint;

const double PI = acos(-1.0);

struct Complex
{
double real, image;
Complex(double _real, double _image)
{
real = _real;
image = _image;
}
Complex() {}
};

Complex operator + (const Complex &c1, const Complex &c2)
{
return Complex(c1.real + c2.real, c1.image + c2.image);
}

Complex operator - (const Complex &c1, const Complex &c2)
{
return Complex(c1.real - c2.real, c1.image - c2.image);
}

Complex operator * (const Complex &c1, const Complex &c2)
{
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}

int rev(int id, int len)
{
int ret = 0;
for(int i = 0; (1 << i) < len; i++)
{
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
}

Complex A[140000];
void FFT(Complex* a, int len, int DFT)
{
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++)
{
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
for(int k = 0; k < len; k += m)
{
Complex w = Complex(1, 0);
for(int j = 0; j < (m >> 1); j++)
{
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i = 0; i < len; i++) a[i] = A[i];
return;
}

char numA[50010], numB[50010];
Complex a[140000], b[140000];
int ans[140000];

void multi()
{
int lenA = strlen(numA);
int sa = 0;
while((1 << sa) < lenA) sa++;
int lenB = strlen(numB);
int sb = 0;
while((1 << sb) < lenB) sb++;
int len = (1 << (max(sa, sb) + 1));
for(int i = 0; i < len; i++)
{
if(i < lenA) a[i] = Complex(numA[lenA - i - 1] - '0', 0);
else a[i] = Complex(0, 0);
if(i < lenB) b[i] = Complex(numB[lenB - i - 1] - '0', 0);
else b[i] = Complex(0, 0);
}
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);
for(int i = 0; i < len; i++)
ans[i] = (int)(a[i].real + 0.5);
for(int i = 0; i < len - 1; i++)
{
ans[i + 1] += ans[i] / 10;
ans[i] %= 10;
}
int temp=0;
bool flag = 0;
for(int j=len-1; j>=0; --j)
{
if(ans[j])  numB[temp++]='0'+ans[j], flag = 1;
else if(flag || j == 0) numB[temp++]='0';
}
numB[temp] = 0;

}

int main()
{
int k;
char temp[50010];
while(scanf("%s %d",temp,&k)!=EOF)
{
int pos=0;
while(temp[pos]!='.'&&temp[pos]!=0)   pos++;
if(pos!=strlen(temp))   pos = strlen(temp) - pos - 1;
else pos=0;
pos*=k;
memset(numB,0,sizeof(numB));
memset(ans,0,sizeof(ans));
memset(numA,0,sizeof(numA));
int t=0;
while(temp[t]=='0')  t++;
for(int i=t, j=0;i<strlen(temp);++i)

aa84
{
if(temp[i]!='.')        numA[j++]=temp[i];
}

//numB[0]='1';
// multi();
memcpy(numB,numA,strlen(numA)+1);
for(int i=1; i<k; ++i)
{
// if(i==1)    memcpy(numB,numA,strlen(numA)+1);
memset(ans,0,sizeof(ans));
multi();
//cout<<numB<<endl;
}
//cout<<strlen(numB)<<endl;
int m = strlen(numB);
int pos_2 = strlen(numB) - pos;
while(numB[m-1]=='0'&&m-1>=pos_2)     numB[m-1]=0, m--;
if(strlen(numB)==0)     {  printf("0\n"); continue;}

if(pos_2<=0)
{
printf(".");
for(int i=1;i<=0-pos_2;++i)
printf("0");
pos_2=0;
}

for(int i=0;i<strlen(numB)&&numB[i]!=0;++i)
{
if(i==pos_2-1&&pos_2!=strlen(numB))    printf("%c.",numB[i]);
else   printf("%c",numB[i]);
}
printf("\n");
}
return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: