hdu 1402 A * B Problem Plus
来源:互联网 发布:软件开发外包靠谱吗 编辑:程序博客网 时间:2024/05/16 14:26
开始用普通高精度乘法,超时了,然后又用FFT优化才过.自从偷了学长的模版后,高精度的题我是一点也不慌
#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int L=50005;string mul(string a,string b)//高精度乘法a,b,均为非负整数{ string s; int na[L],nb[L],nc[L],La=a.size(),Lb=b.size();//na存储被乘数,nb存储乘数,nc存储积 fill(na,na+L,0);fill(nb,nb+L,0);fill(nc,nc+L,0);//将na,nb,nc都置为0 for(int i=La-1;i>=0;i--) na[La-i]=a[i]-'0';//将字符串表示的大整形数转成i整形数组表示的大整形数 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];//a的第i位乘以b的第j位为积的第i+j-1位(先不考虑进位) 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';//判断第i+j位上的数字是不是0 for(int i=La+Lb-1;i>=1;i--) s+=nc[i]+'0';//将整形数组转成字符串 return s;}int main(){cin.sync_with_stdio(false); string a,b; while(cin>>a>>b) cout<<mul(a,b)<<endl; return 0;}
0 0
- hdu 1402 A * B Problem Plus
- hdu 1402 A * B Problem Plus FFT
- HDU 1402 A * B Problem Plus FFT
- HDU 1402 A * B Problem Plus
- hdu 1402 A * B Problem Plus
- [HDU 1402]A * B Problem Plus(FFT)
- HDU 1402 A * B Problem Plus FFT
- 【HDU】1402 A * B Problem Plus 【FFT】
- HDU ACM 1402 A * B Problem Plus
- 【HDU】1402 A * B Problem Plus 【FFT】
- HDU 1402 A * B Problem Plus
- HDU 1402 A * B Problem Plus
- HDU 1402 A * B Problem Plus
- HDU 1402:A * B Problem Plus
- HDU 1402 A * B Problem Plus FFT
- hdu 1402 A * B Problem Plus[【FFT】
- HDU 1402 A * B Problem Plus
- HDU 1402 A * B Problem Plus FFT
- 工作流Activiti5表的命名规范
- Mybatis 之mapUnderscoreToCamelCase
- 通配符与文件名变量
- BQFZ coach handbags 9NM oeGU
- HDU 1171 Big Event in HDU (多重背包)
- hdu 1402 A * B Problem Plus
- Android开发之Service服务详解
- 画费氏数列螺线的代码
- 输入/输出重定向与管道
- Cocos2dx加入Dragonbones支持
- NodeJS 学习笔记1
- tee命令
- HDU 5171 GTY's birthday gift 矩阵快速幂
- android利用Chronometer实现倒计时(已封装,可直接使用)