Advertisement
a53

ProgresieGeom

a53
Apr 17th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #define N 100001
  4. using namespace std;
  5. typedef int Huge[N+3];
  6.  
  7. void Shl(Huge H, int Count)
  8. /* H <- H*10ACount */
  9. {
  10. /* Shifteaza vectorul cu Count pozitii */
  11. memmove(&H[Count+1],&H[1],sizeof(int)*H[0]);
  12. /* Umple primele Count pozitii cu 0 */
  13. memset(&H[1],0,sizeof(int)*Count);
  14. /* Incrementeaza numarul de cifre */
  15. H[0]+=Count;
  16. }
  17.  
  18. int Sgn(Huge H1, Huge H2) {
  19. // Elimina zero-urile semnificative, daca exista.
  20. while (H1[0] && !H1[H1[0]]) H1[0]--;
  21. while (H2[0] && !H2[H2[0]]) H2[0]--;
  22.  
  23. if (H1[0] < H2[0]) {
  24. return -1;
  25. } else if (H1[0] > H2[0]) {
  26. return +1;
  27. }
  28.  
  29. for (int i = H1[0]; i > 0; --i) {
  30. if (H1[i] < H2[i]) {
  31. return -1;
  32. } else if (H1[i] > H2[i]) {
  33. return +1;
  34. }
  35. }
  36. return 0;
  37. }
  38.  
  39. void Subtract(Huge A, Huge B)
  40. /* A <- A-B */
  41. { int i, T=0;
  42.  
  43. for (i=B[0]+1;i<=A[0];) B[i++]=0;
  44. for (i=1;i<=A[0];i++)
  45. A[i]+= (T=(A[i]-=B[i]+T)<0) ? 10 : 0;
  46. /* Adica A[i]=A[i]-(B[i]+T);
  47. if (A[i]<0) T=1; else T=0;
  48. if (T) A[i]+=10; */
  49. while (!A[A[0]]&&A[0]>1) A[0]--;
  50. }
  51.  
  52. void DivideHuge(Huge A, Huge B, Huge C, Huge R)
  53. /* A/B = C rest R */
  54. { int i;
  55.  
  56. R[0]=0;C[0]=A[0];
  57. for (i=A[0];i;i--)
  58. { Shl(R,1);R[1]=A[i];
  59. C[i]=0;
  60. while (Sgn(B,R)!=1)
  61. { C[i]++;
  62. Subtract(R,B);
  63. }
  64. }
  65. while (!C[C[0]] && C[0]>1) C[0]--;
  66. }
  67.  
  68. void MultHuge(Huge A,Huge B) /// A <- A x B
  69. {
  70. Huge C;
  71. int i,j,T=0;
  72. C[0]=A[0]+B[0]-1;
  73. for (i=1;i<=A[0]+B[0];) C[i++]=0;
  74. for (i=1;i<=A[0];i++)
  75. for (j=1;j<=B[0];j++)
  76. C[i+j-1]+=A[i]*B[j];
  77. for (i=1;i<=C[0];i++)
  78. { T=(C[i]+=T)/10;
  79. C[i]%=10;
  80. }
  81. if (T) C[++C[0]]=T;
  82. for(int i=0;i<=C[0];++i)
  83. A[i]=C[i];
  84. }
  85.  
  86. void ExpLog(Huge H,int n)
  87. {
  88. Huge P;
  89. P[0]=P[1]=1;
  90. while(n)
  91. {
  92. if(n&1) /// Daca n este impar
  93. {
  94. MultHuge(P,H);
  95. --n;
  96. }
  97. MultHuge(H,H);
  98. n>>=1 ; /// sau n=n/2
  99. }
  100. for(int i=0;i<=P[0];++i)
  101. H[i]=P[i];
  102. }
  103.  
  104. void Afis(Huge H)
  105. {
  106. for(int i=H[0];i>=1;--i)
  107. cout<<H[i];
  108. cout<<'\n';
  109. }
  110.  
  111. int main()
  112. {
  113. char x[N],a[N],b[N];
  114. int n;
  115. cin>>x>>a>>b>>n;
  116. Huge X,A,B;
  117. X[0]=strlen(x),A[0]=strlen(a),B[0]=strlen(b);
  118. for(int i=1;i<=X[0];++i)
  119. X[i]=x[X[0]-i]-'0';
  120. for(int i=1;i<=A[0];++i)
  121. A[i]=a[A[0]-i]-'0';
  122. for(int i=1;i<=B[0];++i)
  123. B[i]=b[B[0]-i]-'0';
  124. Huge R,C;
  125. DivideHuge(A,B,C,R);
  126. ExpLog(C,n-1);
  127. MultHuge(X,C);
  128. Afis(X);
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement