fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <sstream>
  6. using namespace std;
  7. string l1,l2;
  8. char l3[1000000],l4[1000000];
  9. inline void odwroc(string &x){
  10. int dlugosc = x.length();
  11. for (int i = 0; i < dlugosc / 2; i++) swap(x[i],x[dlugosc-1-i]);
  12. }
  13. inline void usunzera(string &x){
  14. while (x[0]=='0')x.erase(0,1);
  15. }
  16. inline string dodawanie(string x, string y){
  17. string w,ww;
  18. int a,b,c,d,e,pamiec = 0;
  19. a = x.length() - 1;
  20. b = y.length() - 1;
  21. if(a > b) y.insert(0, a - b, '0');
  22. else if(b > a) {x.insert(0, b - a, '0'); a = b;}
  23. for(int i = a; i >= 0; i--){
  24. c = x[i] - '0';
  25. d = y[i] - '0';
  26. e = c + d + pamiec;
  27. if(e > 9){
  28. pamiec = e / 10;
  29. e = e % 10;
  30. }
  31. else pamiec = 0;
  32. w = w+ char(e + '0');
  33. }
  34. if(pamiec>0) w.insert(a + 1, 1, '1');
  35. odwroc(w);
  36. return w;
  37. }
  38. inline string mnozenie(string x, string y){
  39. string w,ww;
  40. int pamiec = 0,wynik,liczbamnoz;
  41. if( y.length() >= x.length() ) {x.swap(y);}
  42. for(int i = y.length(); i > 0; i--){
  43. for(int j = x.length(); j > 0; j--){
  44. liczbamnoz = ( (y[i-1]-'0') * (x[j-1]-'0') + pamiec);
  45. wynik = liczbamnoz % 10;
  46. pamiec = liczbamnoz / 10;
  47. w = w + char(wynik + '0');
  48. }
  49. if (pamiec) w = w + char(pamiec + '0');
  50. odwroc(w);
  51. w = w.insert( w.length(), y.length() - i, '0' );
  52. ww = dodawanie(ww,w);
  53. pamiec = 0;
  54. w="";
  55. }
  56. usunzera(ww);
  57. if(ww=="")return "0";
  58. return ww;
  59. }
  60. int main(){
  61. ios_base::sync_with_stdio(0);
  62. int n;
  63. scanf("%d",&n);
  64. for(int i=0;i<n;i++){
  65. scanf("%s",l3);
  66. scanf("%s",l4);
  67. l1=l3;
  68. l2=l4;
  69. cout<<mnozenie(l1,l2)<<endl;
  70. }
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 4344KB
stdin
5
4 2
123 43
324 342
0 12
9999 12345
stdout
8
5289
110808
0
123437655