Question J: "High precision" high accuracy multiplication time limit: 1 Sec memory limit: up to MB
Submitted by: Solution: 17
Submitted State [Discussion Version] The title describes the third lock on the door, the need to use high-precision multiplication to decipher the corresponding password, of course, the "universal algorithm" must be used to calculate two non-negative integers a, b of the product, where A and b the number of bits within 10,000 bits. Input a total of two rows of data, the first behavior of a nonnegative integer A, the second behavior a nonnegative integer b,a, B, the number of bits are within 10000. Output outputs a number, which is the product of A and B. Sample input
23
Sample output
6
Code:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Chara[10001];//store A, from low to highCharb[10001];//store A, from low to highCharc1[10001];//Temporary storageCharc2[10001];//Temporary storageintf[20000]={0};//store results in reverse ordervoidChengintLaintlb) { intk=0; intGE; intShi; if(la>=lb) { for(intI=0; i<lb;i++){ for(intj=0; j<la;j++) {f[k+j]+= (a[j]-'0') * (b[i]-'0'); } k++; } for(intL=0; L<= (la+lb-1); l++){ if(f[l]>=Ten) {GE=f[l]%Ten; Shi=f[l]/Ten; F[L]=GE; F[l+1]+=Shi; } } } if(la<lb) { for(intI=0; i<la;i++){ for(intj=0; j<lb;j++) {f[k+j]+= (b[j]-'0') * (a[i]-'0'); } k++; } for(intL=0; L<= (lb+la-1); l++){ if(f[l]>=Ten) {GE=f[l]%Ten; Shi=f[l]/Ten; F[L]=GE; F[l+1]+=Shi; } } }}voidPrintintl) { intk=0; for(intj=l;j>=0; j--){ if(f[j]==0) {k++; }Else{ Break; } } if(k==l+1) {printf ("0"); }Else{ for(inti=l-k;i>=0; i--) {printf ("%d", F[i]); } }}intMain () {intLA; intlb; while(SCANF ("%s%s", C1,C2)! =EOF) {La=strlen (C1); for(intI=0; i<la;i++) {A[i]=c1[la-1-i]; } lb=strlen (C2); for(intI=0; i<lb;i++) {B[i]=c2[lb-1-i]; } cheng (la,lb); Print (La+lb-1); printf ("\ n"); Memset (F,0,20000*sizeof(int)); } return 0;}
High-precision multiplication