1544 - Simple Arithmetics

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

Moderator: Board moderators

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

1544 - Simple Arithmetics

Post by brianfry713 »

Input:

Code: Select all

100
41*1344508
9065293*4111251
743075601*77
2015*269
9153327139*69
47779413914*40048
7795*8665536
63734158*582583187
675753436*3
85309577850*7457224
797124*5
373*1352029
3516260900*22076637
2730*75
90*36555
6968580366*5487
218*6355374
7884964544*50066
977*77732
77966*762929483
907746*1
43466*5754533152
9300661*2573932
1042*596549992
608386*831562016
1227382791*438768796
6627038*51836201
4262*641348
4911*8
119*9258
770519*385820
8384627918*9305473
2354050072*96434
57*89
5800538181*353
639593*55413334
528*171011396
829280886*709756
41180203443*6306
1520604*21
5376*4856
79151*25949293
393637*964224
47080527333*352500015
22207458*44553
3730076428*5
9156*57
623788238*90501
6229670*287754409
6631228490*1786
15096065729*2192
63*40349316
9793*1
15668741681*14
99963227*8042
94280*2674
5189034920*254194
9843235*41
490580*33648893
398886*5
4729*11
25*7429
7280*40658467
667251387*352381578
46573419309*979342
14847875*766
6778*219611900
7196*292699
706*74652
208*3907273
9283885*2422306161
6222*931
6316521326*569
26277504946*9771234591
399468470*80
793849238*2510133860
48*697619
6555969*764
11*4010236712
2105390061*45
16489802*52672
80341892128*92
3663673*7
78*65747
33*15597331
5623309123*20599
27245796649*7458184483
2023115*334
92951*76102910
8022746*269762
924*39
7888790*862594432
368609823*1201880889
7005650842*5007
2558*4709809619
934*98772985
323*9745766
4394675*6348012
228*551
133318991*65783
AC output:

Code: Select all

      41
*1344508
--------
     328
      0
   205
  164
 164
123
41
--------
55124828

       9065293
      *4111251
      --------
       9065293
     45326465
    18130586
    9065293
   9065293
  9065293
36261172
--------------
37269694911543

  743075601
        *77
 ----------
 5201529207
5201529207
-----------
57216821277

  2015
  *269
 -----
 18135
12090
4030
------
542035

  9153327139
         *69
 -----------
 82379944251
54919962834
------------
631579572591

     47779413914
          *40048
    ------------
    382235311312
   191117655656
             0
            0
191117655656
----------------
1913469968427872

       7795
   *8665536
   --------
      46770
     23385
    38975
   38975
  46770
 46770
62360
-----------
67547853120

         63734158
       *582583187
       ----------
        446139106
       509873264
       63734158
     191202474
    509873264
   318670790
  127468316
 509873264
318670790
-----------------
37130448888401546

 675753436
        *3
----------
2027260308

       85309577850
          *7457224
      ------------
      341238311400
     170619155700
    170619155700
   597167044950
  426547889250
 341238311400
597167044950
------------------
636172631372888400

 797124
     *5
-------
3985620

      373
 *1352029
 --------
     3357
     746
      0
   746
 1865
1119
373
---------
504306817

       3516260900
        *22076637
      -----------
      24613826300
     10548782700
    21097565400
   21097565400
  24613826300
           0
 7032521800
7032521800
-----------------
77627215486593300

  2730
   *75
 -----
 13650
19110
------
204750

     90
 *36555
 ------
    450
   450
  450
 540
270
-------
3289950

    6968580366
         *5487
   -----------
   48780062562
  55748642928
 27874321464
34842901830
--------------
38236600468242

       218
  *6355374
  --------
       872
     1526
     654
   1090
  1090
  654
1308
----------
1385471532

     7884964544
         *50066
    -----------
    47309787264
   47309787264
            0
           0
39424822720
---------------
394768634859904

     977
  *77732
  ------
    1954
   2931
  6839
 6839
6839
--------
75944164

         77966
    *762929483
    ----------
        233898
       623728
      311864
     701694
    155932
   701694
  155932
 467796
545762
--------------
59482560071578

907746
    *1
------
907746

          43466
    *5754533152
    -----------
          86932
        217330
        43466
      130398
     130398
    217330
   173864
  217330
 304262
217330
---------------
250126537984832

       9300661
      *2573932
      --------
      18601322
     27901983
    83705949
   27901983
  65104627
 46503305
18601322
--------------
23939268969052

        1042
  *596549992
  ----------
        2084
       9378
      9378
     9378
    4168
   5210
  6252
 9378
5210
------------
621605091664

         608386
     *831562016
     ----------
        3650316
        608386
            0
     1216772
    3650316
   3041930
   608386
 1825158
4867088
---------------
505910688666176

        1227382791
        *438768796
        ----------
        7364296746
      11046445119
      8591679537
     9819062328
    7364296746
   8591679537
  9819062328
 3682148373
4909531164
------------------
538537269438189636

        6627038
      *51836201
      ---------
        6627038
             0
     13254076
    39762228
   19881114
  53016304
  6627038
33135190
---------------
343520473802638

      4262
   *641348
   -------
     34096
    17048
   12786
   4262
 17048
25572
----------
2733425176

 4911
   *8
-----
39288

    119
  *9258
  -----
    952
   595
  238
1071
-------
1101702

      770519
     *385820
     -------
           0
    1541038
   6164152
  3852595
 6164152
2311557
------------
297281640580

       8384627918
         *9305473
      -----------
      25153883754
     58692395426
    33538511672
   41923139590
            0
 25153883754
75461651262
-----------------
78022928705995214

     2354050072
         *96434
     ----------
     9416200288
    7062150216
   9416200288
 14124300432
21186450648
---------------
227010464643248

  57
 *89
 ---
 513
456
----
5073

   5800538181
         *353
  -----------
  17401614543
 29002690905
17401614543
-------------
2047589977893

        639593
     *55413334
     ---------
       2558372
      1918779
     1918779
    1918779
    639593
  2558372
 3197965
3197965
--------------
35441980533062

        528
 *171011396
 ----------
       3168
      4752
     1584
     528
    528
     0
  528
3696
528
-----------
90294017088

      829280886
        *709756
     ----------
     4975685316
    4146404430
   5804966202
  7463527974
          0
5804966202
---------------
588587084523816

    41180203443
          *6306
   ------------
   247081220658
             0
 123540610329
247081220658
---------------
259682362911558

 1520604
     *21
 -------
 1520604
3041208
--------
31932684

    5376
   *4856
   -----
   32256
  26880
 43008
21504
--------
26105856

        79151
    *25949293
    ---------
       237453
      712359
     158302
    712359
   316604
  712359
 395755
158302
-------------
2053912490243

      393637
     *964224
     -------
     1574548
     787274
    787274
  1574548
 2361822
3542733
------------
379554242688

         47080527333
          *352500015
        ------------
        235402636665
        47080527333
                 0
                0
               0
   235402636665
   94161054666
 235402636665
141241581999
--------------------
16595886591090409995

    22207458
      *44553
    --------
    66622374
  111037290
 111037290
 88829832
88829832
------------
989408876274

 3730076428
         *5
-----------
18650382140

  9156
   *57
 -----
 64092
45780
------
521892

     623788238
        *90501
     ---------
     623788238
            0
  3118941190
          0
5614094142
--------------
56453459327238

         6229670
      *287754409
      ----------
        56067030
              0
      24918680
     24918680
    31148350
   43607690
  43607690
 49837360
12459340
----------------
1792615009115030

    6631228490
         *1786
   -----------
   39787370940
  53049827920
 46418599430
 6631228490
--------------
11843374083140

   15096065729
         *2192
   -----------
   30192131458
 135864591561
 15096065729
30192131458
--------------
33090576077968

        63
 *40349316
 ---------
       378
       63
     189
    567
   252
  189
   0
252
----------
2542006908

9793
  *1
----
9793

 15668741681
         *14
 -----------
 62674966724
15668741681
------------
219362383534

    99963227
       *8042
   ---------
   199926454
  399852908
         0
799705816
------------
803904271534

    94280
    *2674
   ------
   377120
  659960
 565680
188560
---------
252104720

      5189034920
         *254194
     -----------
     20756139680
    46701314280
    5189034920
  20756139680
 25945174600
10378069840
----------------
1319021542454480

  9843235
      *41
  -------
  9843235
39372940
---------
403572635

        490580
     *33648893
     ---------
       1471740
      4415220
     3924640
    3924640
   1962320
  2943480
 1471740
1471740
--------------
16507473927940

 398886
     *5
-------
1994430

 4729
  *11
 ----
 4729
4729
-----
52019

    25
 *7429
 -----
   225
   50
 100
175
------
185725

        7280
   *40658467
   ---------
       50960
      43680
     29120
    58240
   36400
  43680
     0
29120
------------
295993639760

         667251387
        *352381578
        ----------
        5338011096
       4670759709
      3336256935
      667251387
    5338011096
   2001754161
  1334502774
 3336256935
2001754161
------------------
235127096673748686

      46573419309
          *979342
      -----------
      93146838618
    186293677236
   139720257927
  419160773781
 326013935163
419160773781
-----------------
45611305612914678

   14847875
       *766
   --------
   89087250
  89087250
103935125
-----------
11373472250

         6778
   *219611900
   ----------
            0
           0
      61002
      6778
     6778
   40668
  61002
  6778
13556
-------------
1488529458200

      7196
   *292699
   -------
     64764
    64764
   43176
  14392
 64764
14392
----------
2106262004

     706
  *74652
  ------
    1412
   3530
  4236
 2824
4942
--------
52704312

      208
 *3907273
 --------
      624
    1456
    416
  1456
    0
1872
624
---------
812712784

          9283885
      *2422306161
      -----------
          9283885
        55703310
        9283885
      55703310
            0
    27851655
   18567770
  18567770
 37135540
18567770
-----------------
22488411833515485

   6222
   *931
   ----
   6222
 18666
55998
-------
5792682

   6316521326
         *569
  -----------
  56848691934
 37899127956
31582606630
-------------
3594100634494

          26277504946
          *9771234591
          -----------
          26277504946
        236497544514
       131387524730
      105110019784
      78832514838
     52555009892
    26277504946
  183942534622
 183942534622
236497544514
---------------------
256763665293528787086

  399468470
        *80
        ---
          0
3195747760
-----------
31957477600

          793849238
        *2510133860
        -----------
                  0
        4763095428
       6350793904
      2381547714
     2381547714
     793849238
            0
   793849238
 3969246190
1587698476
-------------------
1992667852038998680

      48
 *697619
 -------
     432
     48
   288
  336
 432
288
--------
33485712

   6555969
      *764
  --------
  26223876
 39335814
45891783
----------
5008760316

         11
*4010236712
-----------
         22
        11
       77
      66
     33
    22
    0
  11
  0
44
-----------
44112603832

 2105390061
        *45
-----------
10526950305
8421560244
-----------
94742552745

    16489802
      *52672
    --------
    32979604
  115428614
  98938812
 32979604
82449010
------------
868550850944

  80341892128
          *92
 ------------
 160683784256
723077029152
-------------
7391454075776

 3663673
      *7
--------
25645711

     78
 *65747
 ------
    546
   312
  546
 390
468
-------
5128266

       33
*15597331
---------
       33
      99
     99
   231
  297
 165
165
33
---------
514711923

     5623309123
         *20599
    -----------
    50609782107
   50609782107
  28116545615
           0
11246618246
---------------
115834544624677

          27245796649
          *7458184483
          -----------
          81737389947
        217966373192
       108983186596
      108983186596
     217966373192
     27245796649
   217966373192
  136228983245
 108983186596
190720576543
---------------------
203204177794545197467

  2023115
     *334
  -------
  8092460
 6069345
6069345
---------
675720410

        92951
    *76102910
    ---------
            0
       92951
     836559
    185902
        0
   92951
 557706
650657
-------------
7073841587410

      8022746
      *269762
     --------
     16045492
    48136476
   56159222
  72204714
 48136476
16045492
-------------
2164232006452

  924
  *39
 ----
 8316
2772
-----
36036

         7888790
      *862594432
      ----------
        15777580
       23666370
      31555160
     31555160
    70999110
   39443950
  15777580
 47332740
63110320
----------------
6804826329217280

         368609823
       *1201880889
       -----------
        3317488407
       2948878584
      2948878584
              0
    2948878584
   2948878584
   368609823
          0
 737219646
368609823
------------------
443025101761372647

    7005650842
         *5007
   -----------
   49039555894
            0
           0
35028254210
--------------
35077293765894

          2558
   *4709809619
   -----------
         23022
         2558
       15348
      23022
         0
    20464
   23022
      0
 17906
10232
--------------
12047693005402

        934
  *98772985
  ---------
       4670
      7472
     8406
    1868
   6538
  6538
 7472
8406
-----------
92253967990

       323
  *9745766
  --------
      1938
     1938
    2261
   1615
  1292
 2261
2907
----------
3147882418

       4394675
      *6348012
      --------
       8789350
      4394675
           0
   35157400
  17578700
 13184025
26368050
--------------
27897449636100

   228
  *551
  ----
   228
 1140
1140
------
125628

    133318991
       *65783
    ---------
    399956973
  1066551928
  933232937
 666594955
799913946
-------------
8770123184953

Check input and AC output for thousands of problems on uDebug!
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 1544 - Simple Arithmetics

Post by brianfry713 »

Try Input:

Code: Select all

2
1819806958297024327533977004897209481097836631285139456476858608748849533418539042919757454234208015980421574400811898554977690611101174423885167246111710471652974101755253862716340613872947334967332089466859603666945159905593249657615290350628434959141612147004765204431415459854980041575288654307760034679566956182230078835487174477834980898626959976751419256695399909201073*5809777648069237842353555788971954841508506439333798165855644951335705558113268737565240006421656328193906442218284731831493871413343835772113934637712071679742296771560703217670363572930024463242591581802772928407792914551877226594696193588538329512267566200061640279820683617588724672492498753700985143406176169726988127215785995409961790406590188922943633325895984283226075006028
79742192071113967327207269645904090+200637960276909
You are printing some extra lines on the second test case.
Check input and AC output for thousands of problems on uDebug!
restart_again
New poster
Posts: 1
Joined: Thu Feb 05, 2015 10:39 pm

Re: 1544 - Simple Arithmetics

Post by restart_again »

Getting Time limit.. Any Suggestion...Pls..??

Code: Select all


import java.util.Scanner;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) {
        
        Scanner input=new Scanner(System.in);
        
        int t=input.nextInt();
        while(t-->0)
        {
            String tmp=input.next();
            int k=1;
            BigInteger a = BigInteger.ZERO;
            String ch="";
            BigInteger b = BigInteger.ZERO;
            for(int i=0; i<tmp.length();i++)
            {
                if(tmp.charAt(i)=='+'||tmp.charAt(i)=='-'||tmp.charAt(i)=='*')
                {
                    k=0;
                    ch=""+tmp.charAt(i);
                    continue;
                }
                if(k==1)
                {
                    a=a.multiply(BigInteger.valueOf(10));
                    String s1="";
                    String c1=""+tmp.charAt(i);
                    s1=s1+c1;
                    int x=Integer.parseInt(s1);
                    a=a.add(BigInteger.valueOf(x));
                }
                else
                {
                    b=b.multiply(BigInteger.valueOf(10));
                    String s1="";
                    String c1=""+tmp.charAt(i);
                    s1=s1+c1;
                    int x=Integer.parseInt(s1);
                    b=b.add(BigInteger.valueOf(x));
                }
            }
            
            if(ch.equals("+"))
            {
                BigInteger x=a.add(b);
                String ans=x.toString();
                int l=Math.abs(a.toString().length()-b.toString().length());
                if(l==0)
                System.out.printf(" ");
                else
                    if(a.toString().length()<b.toString().length())
                 for(int i=0;i<l+1;i++)
                    System.out.printf(" ");
                System.out.println(a);
                if(a.toString().length()>b.toString().length())
                for(int i=0;i<((l-1)<0?0:l-1);i++)
                    System.out.printf(" ");
                System.out.print(ch);
                System.out.println(b);

                if(ans.length()>Math.max(a.toString().length(),b.toString().length()+1))
                {
                     for(int i=0;i<ans.length();i++)
                    System.out.printf("-");
                    System.out.println();
                    System.out.println(ans);
                }
                else if(ans.length()<Math.max(a.toString().length(),b.toString().length()+1)){
                     for(int i=0;i<ans.length()+1;i++)
                    System.out.printf("-");
                    System.out.println();
                    System.out.println(" "+ans);
                }
                else
                {
                     for(int i=0;i<ans.length();i++)
                    System.out.printf("-");
                    System.out.println();
                    System.out.println(ans);
                }
            }
            
            else if(ch.equals("-"))
            {
                BigInteger x=a.subtract(b);
                String ans=x.toString();
                int l=Math.abs(a.toString().length()-b.toString().length());
                if(l==0)
                System.out.printf(" ");
                System.out.println(a);
                for(int i=0;i<((l-1)<0?0:l-1);i++)
                    System.out.printf(" ");
                System.out.print(ch);
                System.out.println(b);
               

                for(int i=0;i<a.toString().length()+(l==0?1:0);i++)
                System.out.printf("-");
                System.out.println();
                for(int i=0;i<Math.abs(ans.length()-a.toString().length())+(l==0?1:0);i++)
                    System.out.printf(" ");
                System.out.println(ans);
            }
            else
            {
                BigInteger x=a.multiply(b);
                String ans=x.toString();
                String s1="";
                for(int i=0;i<Math.abs(ans.length()-a.toString().length());i++)
                    s1=s1+" ";
                System.out.println(s1+a);
                s1="";
                for(int i=0;i<Math.abs(ans.length()-b.toString().length())-1;i++)
                    s1=s1+" ";
                System.out.print(s1+ch);
                System.out.println(b);
                s1="";
                for(int i=0;i<Math.abs(ans.length()- Math.max(a.toString().length(),b.toString().length()));i++)
                    s1=s1+" ";
                if(b.toString().length()>1){
                System.out.print(s1);
                for(int i=0;i<Math.abs( Math.max(a.toString().length(),b.toString().length()));i++)
                System.out.printf("-");
                System.out.println();}
                String res=b.toString();
                int flag=0;
                if(b.toString().length()>1)
                for(int i=res.length()-1;i>=0;i--)
                {
                    BigInteger y=a.multiply(BigInteger.valueOf(res.charAt(i)-48));
                    String s2="";
                    for(int j=0;j<Math.abs(y.toString().length()-ans.length())-flag;j++)
                        s2=s2+" ";
                        
                    System.out.println(s2+y);
                    flag++;
                }
                   for(int i=0;i<ans.length();i++)
                    System.out.printf("-");
                    System.out.println();
                    System.out.println(ans);
                
            }
            System.out.println();
        }
    }
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 1544 - Simple Arithmetics

Post by brianfry713 »

try using BufferedReader and BufferedWriter.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 15 (1500-1599)”