a53

numere20

a53
Oct 16th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int NMAX=101;
  5. char nr[NMAX];
  6. int a[NMAX],b[NMAX],c[NMAX],nsol,na,nb,nc,sol[NMAX],dif[NMAX],ndif;
  7. int p,q;
  8. ifstream fin("numere20.in");
  9. ofstream fout("numere20.out");
  10. inline void Imp(int aux[],int &naux,int k)
  11. {
  12. int cifra=0;
  13. for(int i=naux;i>=1;i--)
  14. {
  15. cifra=cifra*10+aux[i];
  16. aux[i]=cifra/k;
  17. cifra=cifra-aux[i]*k;
  18. }
  19. while(!aux[naux])
  20. naux--;
  21. }
  22. inline void S()
  23. {
  24. nsol=max(na,nb);
  25. if(na<nb)
  26. {
  27. for(int i=na+1;i<=nb;i++)
  28. a[i]=0;
  29. }
  30. else
  31. {
  32. for(int i=nb+1;i<=na;i++)
  33. b[i]=0;
  34. }
  35. int t,cifra;
  36. t=0;
  37. for(int i=1;i<=nsol;i++)
  38. {
  39. cifra=a[i]+b[i]+t;
  40. sol[i]=cifra%10;
  41. t=cifra/10;
  42. }
  43. if(t)
  44. sol[++nsol]=t;
  45. }
  46. inline void D2()
  47. {
  48. if(nsol<nc)
  49. {
  50. for(int i=nsol+1;i<=nc;i++)
  51. sol[i]=0;
  52. nsol=nc;
  53. }
  54. else
  55. {
  56. for(int i=nc+1;i<=nsol;i++)
  57. c[i]=0;
  58. }
  59. int cifra,t=0;
  60. for(int i=1;i<=nsol;i++)
  61. {
  62. cifra=sol[i]-c[i]+t;
  63. if(cifra<0)
  64. {
  65. sol[i]=cifra+10;
  66. t=-1;
  67. }
  68. else
  69. {
  70. sol[i]=cifra;
  71. t=0;
  72. }
  73. }
  74. while(!sol[nsol])
  75. nsol--;
  76. }
  77. inline void D()
  78. {
  79. if(nsol<ndif)
  80. {
  81. for(int i=nsol+1;i<=ndif;i++)
  82. sol[i]=0;
  83. nsol=ndif;
  84. }
  85. else
  86. {
  87. for(int i=ndif+1;i<=nsol;i++)
  88. dif[i]=0;
  89. }
  90. int cifra,t=0;
  91. for(int i=1;i<=nsol;i++)
  92. {
  93. cifra=dif[i]-sol[i]+t;
  94. if(cifra<0)
  95. {
  96. sol[i]=cifra+10;
  97. t=-1;
  98. }
  99. else
  100. {
  101. sol[i]=cifra;
  102. t=0;
  103. }
  104. }
  105. while(!sol[nsol])
  106. nsol--;
  107. }
  108. int main()
  109. {
  110. fin>>nr>>p>>q;
  111. for(int i=strlen(nr)-1;i>=0;i--)
  112. {
  113. a[++na]=nr[i]-'0';
  114. b[++nb]=nr[i]-'0';
  115. c[++nc]=nr[i]-'0';
  116. dif[++ndif]=nr[i]-'0';
  117. }
  118. Imp(a,na,p);
  119. Imp(b,nb,q);
  120. Imp(c,nc,p*q);
  121. S();
  122. D2();
  123. D();
  124. for(int i=nsol;i>=1;i--)
  125. fout<<sol[i];
  126. fout<<"\n";
  127. fin.close();
  128. fout.close();
  129. return 0;
  130. }
Add Comment
Please, Sign In to add comment