您的位置:首页 > 其它

hdu 1402 A * B Problem Plus || poj 2389 Bull Math

2017-08-09 09:07 405 查看
题目链接:hdu 1402点这里

poj 2389点这里

Problem Description

Calculate A * B.


Input

Each line will contain two integers A and B. Process to end of file.

Note: the length of each integer will not exceed 50000.


Output

For each case, output A * B in one line.


Sample Input

1
2
1000
2


Sample Output

2
2000


【题意】

难得的能读懂题意的题,很显然了,大数乘法。

【分析】

多么痛的领悟啊,各种乘法做了无数次,一次次的被大数虐。直到今天看到这题,想都没想就直接贴了一个大数乘法,tle,what?看了看数据量,哦,,难怪。然后就改成FFT,稳过,看了一晚上,总算是勉勉强强懂了,心态炸裂啊,还是自己太菜。反正我是讲不懂了,抽个时间多练练再说。

【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<sstream>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define sin1(a) scanf("%d",&(a))
#define sin2(a,b) scanf("%d%d",&(a),&(b))
#define sll(a) scanf("%lld",&(a))
#define sll2(a,b) scanf("%lld%lld",&(a),&(b))
#define sdo(a) scanf("%lf",&(a))
#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))
#define inf 0x3f3f3f3f
#define lson i<<1,l,mid
#define rson ((i<<1)|1),mid+1,r
#define uint unsigned int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define MK make_pair
#define ll long long
template<typename T>
void read1(T &m)
{
T x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
m = x*f;
}
template<typename T>
void read2(T &a,T &b)
{
read1(a);
read1(b);
}
template<typename T>
void read3(T &a,T &b,T &c)
{
read1(a);
read1(b);
read1(c);
}
template<typename T>
void out(T a)
{
if(a>9) out(a/10);
putchar(a%10+'0');
}
template<typename T>
void outn(T a)
{
if(a>9) out(a/10);
putchar(a%10+'0');
puts("");
}
using namespace std;
///---------------------------------------------------------------------------------
const double PI = acos(-1.0);
const int maxm = 2e5+5;
class comp
{
public:
double real,imag;
comp(double _real=0.0,double _imag=0.0)
{
real=_real;
imag=_imag;
}
comp operator - (const comp &b) const
{
return comp(real-b.real,imag-b.imag);
}
comp operator + (const comp &b) const
{
return comp(real+b.real,imag+b.imag);
}
comp operator * (const comp &b) const
{
return comp(real*b.real-imag*b.imag,real*b.imag+imag*b.real);
}
void setValue(double _real = 0.0,double _imag = 0.0)
{
real=_real;
imag=_imag;
}
};
comp A[maxm],B[maxm];
int res[maxm],len,mlen,len1,len2;
char str1[maxm>>1],str2[maxm>>1];

void swap(comp &a,comp &b)
{
comp c=a;
a=b;
b=c;
}
void prepare()
{
len1=strlen(str1);
len2=strlen(str2);
mlen=max(len1,len2);
len=1;
while(len<(mlen<<1))
len<<=1;
for(int i=0;i<len1
4000
;i++)
A[i].setValue(str1[len1-i-1]-'0',0);

for(int i=0;i<len2;i++)
B[i].setValue(str2[len2-i-1]-'0',0);

for(int i=len1;i<len;i++)
A[i].setValue();

for(int i=len2;i<len;i++)
B[i].setValue();
}
void rader(comp y[])
{
for(int i=1,j=len>>1,k;i<len-1;i++)
{
if(i<j) swap(y[i],y[j]);
k=len>>1;
while(j>=k)
{
j-=k;
k>>=1;
}
if(j<k)
j+=k;
}
}
void FFT(comp y[],int op)
{
rader(y);
for(int h=2;h<=len;h<<=1)
{
comp wn(cos(op*2*PI/h),sin(op*2*PI/h));
for(int i=0;i<len;i+=h)
{
comp w(1,0);
for(int j=i;j<i+h/2;j++)
{
comp u=y[j];
comp t=w*y[j+h/2];

y[j]=u+t;
y[j+h/2]=u-t;
w=w*wn;
}
}
}
if(op==-1)
for(int i=0;i<len;i++)
y[i].real/=len;
}
void convolution(comp *a,comp *b)
{
FFT(a,1);
FFT(b,1);
for(int i=0;i<len;++i)
a[i]=a[i]*b[i];
FFT(a,-1);
for(int i=0;i<len;i++)
res[i]=(int)(a[i].real+0.5);
}
void adjustment(int * arr)
{
for(int i=0;i<len;i++)
{
res[i+1]+=res[i]/10;
res[i]%=10;
}

while(--len&&res[len]==0);
}
void display(int *arr)
{
for(int i=len;i>=0;i--)
putchar(arr[i]+'0');
puts("");
}
int main()
{
//    freopen("in.txt","r",stdin);
while(gets(str1)&&gets(str2))
{
prepare();
convolution(A,B);
adjustment(res);
display(res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: