10106 - Product

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Salsabil
New poster
Posts: 4
Joined: Tue Sep 10, 2013 1:43 pm

Re: 10106 - Product

Post by Salsabil » Tue Sep 10, 2013 2:02 pm

hey, could i get some help please?
i'm consistently getting WA in this problem,but can't figure out why.. :(
here's my code:
http://paste.ubuntu.com/6087236/

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 - Product

Post by brianfry713 » Wed Sep 11, 2013 12:15 am

Input:

Code: Select all

1698978606
984130093
1601193768
1144856060
1442887218
1301509043
1538561662
156545264
34893116
1104252427
323746640
852155117
118650948
1412362195
1833036276
356369000
1052951484
735043626
1063805700
972401858
1397070675
654126530
216974834
1542201252
619696498
138514000
1152146431
1707249273
1884423009
1722341500
352547894
1435917967
558987945
1953741662
433290379
2001875163
1107767058
1971852041
10936780
1142660174
928620820
334683420
1994815291
1047271768
1747045615
1680367919
1403640768
652513451
267927897
319962821
1624915309
1664998572
974089351
1841890143
1059716176
1593785849
1980404144
64378959
1153551474
1717343505
1786720459
1506099368
1005777824
198224756
1312357383
1439068203
52616272
272640793
1263436597
63553052
1415300967
44573769
398236472
1262632610
1091845538
2145282088
795516881
348002658
650311891
1063444778
667965479
127743553
580959702
1642054830
1969633696
1640675878
1088357032
1802554192
1705054837
94424858
1372414049
1344291648
1600524227
230708225
1542516404
765397962
1669776429
1595132676
1038038755
785729378
1658685728
305856074
830303147
2056922201
1568488684
1922148685
2054720641
216521917
122667696
557548884
1279966695
790633175
685292437
1860926397
285204358
507442486
1354118627
1373561390
162513030
911689816
1467986248
1534927080
108497816
921026827
1765635305
1651014220
1686424789
1287928086
1098663249
576979896
2073657464
609865329
882835970
756476964
519303882
303841006
531142001
426540875
520362923
653809697
984089760
1800329618
1444442873
1669382197
1513772367
1729647231
29341035
720407346
955724973
191854066
1632097162
276227573
1726781146
1740594978
1197254401
1344932803
1244125551
736195542
485377242
195305152
1313175439
411551058
805170481
48527761
1168028022
1324474364
352368768
1699170024
1751015239
872731691
205496073
587621351
525577662
1649938946
109519901
2039350029
1232102529
138860936
612273728
40343854
330715002
96887242
316571428
2057496148
1837482221
1513825829
1254945304
934124124
102537723
1740322546
1129429276
1415713162
4389956
1934599757
1464240924
1172417979
1111590473
1816609692
724104355
715122065
AC output:

Code: Select all

1672015973527790358
1833136388529034080
1877930762256112374
240854541558068768
38530808028592532
275882355887556880
167578113356110860
653237304641844000
773965276801440984
1034446639230990600
913860992802507750
334618860647292168
85836640723972000
1967001156714294663
3245619951955573500
506229855222611498
1092118036702264590
867393248086956777
2184352734269865378
12497022937799720
310793991920804400
2089113736639004488
2935679404475625185
915894481491970368
85726965748717437
2705481669105938748
1794165574008167193
1688960645265193424
127496357190006096
1981044131557076370
2690978554092569912
199370063752610944
1888571780847592749
14345342122783696
80295251747844044
63085298368534623
502826356038551920
2342316675534123344
276841989071869698
691570784555255198
85328283568806887
953967684704460660
3231530493523185088
1961822530424278144
160999560865938146
1844924743668562752
369254103480667075
1180638911973168648
2663514943512494004
815617545306044390
507319104765911872
1707868976624466547
3014868461387980540
444892052088788797
68393237007651264
1011984131962106625
1275278785677759489
144724808441553988
1859965063527011530
148161474418302480
2253251845122795840
99929399206909832
2915088995889037100
2171993850679723854
633906607147042104
1264651791515665656
667845074295595080
157785813926585292
226553773855790875
340218325016664331
1771685941698511680
2411327216769731981
2618292182945865777
21137497153243110
183359722047790218
450830237959447826
3005626590832684788
1610226717441016003
915919684334493642
94796676026150784
540438741260064462
39073120666223041
1547023171572628008
598734447979410432
1528166490499239149
120753880041454623
867171053681424252
223349413280427129
171090910424907144
24701481890467712
32042064431804484
651344493676859344
2781628046478086209
1172274682766913696
178448711152402758
1598947891581330712
8492807810840692
1716702384885172596
2019326026786664316
517823001623093075
Check input and AC output for thousands of problems on uDebug!

Salsabil
New poster
Posts: 4
Joined: Tue Sep 10, 2013 1:43 pm

Re: 10106 - Product

Post by Salsabil » Fri Sep 13, 2013 8:00 am

ok.. my code doesn't seem to run for some inputs like.. 1538561662 156545264, 34893116 1104252427, 1833036276 356369000 etc.. but i dont know why..
could you please help brianfry713?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 - Product

Post by brianfry713 » Fri Sep 13, 2013 10:00 pm

You can easily solve this using Java's BigInteger.
In C I would first reverse the X and Y strings, clear Z, and then do something like:

Code: Select all

    for(i = 0; X[i]; i++) {
      carry = 0;
      for(j = 0; Y[j]; j++) {
        Z[i + j] += (X[i] - '0') * (Y[j] - '0') + carry;
        carry = Z[i + j] / 10;
        Z[i + j] %= 10;
      }
      Z[i + j] += carry;
    }
Now print Z reversed without leading zeros, and handle the case where Z == 0.
Check input and AC output for thousands of problems on uDebug!

Salsabil
New poster
Posts: 4
Joined: Tue Sep 10, 2013 1:43 pm

Re: 10106 - Product

Post by Salsabil » Sat Sep 14, 2013 7:56 am

actually i haven't learnt java yet.. but thanks a lot for your suggestion! :D I'll try fixing my code..

helli3papa
New poster
Posts: 8
Joined: Wed Apr 11, 2012 5:48 pm

Re: 10106 - Product

Post by helli3papa » Mon Sep 16, 2013 10:08 am

HI all
TY all :D

Code: Select all

Remove After AC
Last edited by helli3papa on Tue Sep 17, 2013 9:49 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 - Product

Post by brianfry713 » Tue Sep 17, 2013 12:22 am

Read this thread.
Check input and AC output for thousands of problems on uDebug!

luhego
New poster
Posts: 2
Joined: Tue Oct 01, 2013 1:48 am

10106 -Product

Post by luhego » Tue Oct 01, 2013 2:01 am

Hi, could i get some help please?
i'm constantly getting WA in this problem,but can't figure out why.
Here's my code:

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

string addBigInt(string a, string b) {
int suma, llevo;
string respuesta;
respuesta = "";
suma = llevo = 0;

while(a.size() < b.size() || b.size() < a.size()) {
if(a.size() < b.size()) a.append("0");
if(b.size() < a.size()) b.append("0");
}

for(int i = 0; i < a.size(); i++) {
suma = (a-'0') + (b-'0') + llevo;
respuesta += suma % 10 + '0';
llevo = suma / 10;
}

if(llevo != 0)
respuesta += llevo + '0';

return respuesta;
}

string productBigInt(string a, string b) {
int producto, llevo, p, n;
string parcial, suma;

producto = 1;
llevo = 0;
parcial = suma = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());

n = b.size();

for(int i = 0; i < b.size(); i++) {
for(int j = 0; j < a.size(); j++) {
p = ((a[j]-'0') * (b-'0')) + llevo;
parcial += p % 10 + '0';
llevo = p / 10;
}

if(llevo != 0)
parcial += llevo + '0';



for(int k = n; k < b.size(); k++) {
parcial.insert(0, "0");
}

n--;

suma = addBigInt(suma, parcial);
llevo = 0;
parcial = "";
}
reverse(suma.begin(), suma.end());
return suma;
}

int main() {
freopen("10106.in","r",stdin);
freopen("10106.out","w",stdout);

string a, b, c;

while(cin>>a>>b) {
c = productBigInt(a, b);
cout<<c<<endl;
}

return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 -Product

Post by brianfry713 » Wed Oct 02, 2013 12:03 am

Don't read and write to files.
Check input and AC output for thousands of problems on uDebug!

Loading...
New poster
Posts: 6
Joined: Fri Jun 21, 2013 3:31 am

Re: 10106 - Product

Post by Loading... » Thu Jan 02, 2014 4:26 pm

removed After AC :evil:

Code: Select all

Removed After AC

try_tired
New poster
Posts: 6
Joined: Thu Jan 23, 2014 3:22 am

Re: 10106 -Product

Post by try_tired » Thu Jan 23, 2014 7:32 pm

import java.util.Scanner;
import java.math.BigInteger;
public class kmn{
public static void main(String args[]){

Scanner s = new Scanner(System.in);
BigInteger res;
while(s.hasNext()){
BigInteger n=s.nextBigInteger();
BigInteger p=s.nextBigInteger();
res=n.multiply(p);
System.out.println(res.toString());
}
}
}


why i cant accept it?
please tell me ,why is wrong here.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 -Product

Post by brianfry713 » Thu Jan 23, 2014 10:00 pm

Use class Main
Check input and AC output for thousands of problems on uDebug!

try_tired
New poster
Posts: 6
Joined: Thu Jan 23, 2014 3:22 am

Re: 10106 -Product

Post by try_tired » Fri Jan 24, 2014 7:25 am

why i use class Main?
isnt it class name?
so,kmn is class name here.
please tell me the differences between using,class Main and class kmn .

try_tired
New poster
Posts: 6
Joined: Thu Jan 23, 2014 3:22 am

Re: 10106 -Product

Post by try_tired » Fri Jan 24, 2014 1:19 pm

why i use class Main?
isnt it class name?
so,kmn is class name here.
please tell me the differences between using,class Main and class kmn .
:(
import java.util.Scanner;
import java.math.BigInteger;
public class kmn{
public static void main(String args[]){

Scanner s = new Scanner(System.in);
BigInteger res;
while(s.hasNext()){
BigInteger n=s.nextBigInteger();
BigInteger p=s.nextBigInteger();
res=n.multiply(p);
System.out.println(res.toString());
}
}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10106 -Product

Post by brianfry713 » Fri Jan 24, 2014 9:36 pm

If you want to get AC, use class Main
http://uva.onlinejudge.org/index.php?op ... &Itemid=30
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 101 (10100-10199)”