BZOJ 1754: [Usaco2005 qua]Bull Math 高精乘

来源:互联网 发布:java byte 转换base64 编辑:程序博客网 时间:2024/06/06 09:25

1754: [Usaco2005 qua]Bull Math

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 543  Solved: 343
[Submit][Status][Discuss]

Description

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). FJ asks that you do this yourself; don't use a special library function for the multiplication. 输入两个数,输出其乘积

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

11111111111111
1111111111

Sample Output

12345679011110987654321

讲真

不会写高精度啊啊啊


#include<ctime>#include<cmath>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<bitset>#include<queue>#include<map>#include<set>#define Pi acos(-1)using namespace std;typedef double db;inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}const int N=110;char s1[N],s2[N];int a[N],b[N],c[N];int main(){scanf("%s%s",s1,s2);int lena=strlen(s1),lenb=strlen(s2);for(int i=0;s1[i];i++)a[lena-i]=s1[i]-'0';for(int i=0;s2[i];i++)b[lenb-i]=s2[i]-'0';for(int i=1;i<=lena;i++)for(int j=1;j<=lenb;j++){c[i+j-1]+=a[i]*b[j];}for(int i=1;i<=lena+lenb+2;i++){if(c[i]>=10){c[i+1]+=c[i]/10;c[i]%=10;}}int lenc;for(lenc=lena+lenb+3;lenc>0;lenc--)if(c[lenc])break;for(int i=lenc;i;i--)printf("%d",c[i]);puts("");return 0;} /*11111111111111111111111112345679011110987654321*/


原创粉丝点击