「BZOJ1754」[Usaco2005 qua] Bull Math
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
1111111111
Sample Output
12345679011110987654321
题解
高精乘高精
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #define inf 0x7fffffff #define ll long long using namespace std; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int la,lb,lc; char ch[55]; int a[55],b[55],c[105]; void cal() { for(int i=1;i<=la;i++) for(int j=1;j<=lb;j++) c[i+j-1]+=a[i]*b[j]; for(int i=1;i<=la+lb;i++) { c[i+1]+=c[i]/10; c[i]%=10; if(c[i])lc=i; } } int main() { scanf("%s",ch+1); la=strlen(ch+1); for(int i=1;i<=la;i++) a[i]=ch[la-i+1]-'0'; scanf("%s",ch+1); lb=strlen(ch+1); for(int i=1;i<=lb;i++) b[i]=ch[lb-i+1]-'0'; cal(); for(int i=lc;i;i--) printf("%d",c[i]); return 0; } |
Subscribe