Advertisement
a53

inequation_caut_bin

a53
Apr 9th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #define Nmax 10001
  4. using namespace std;
  5. typedef int Huge[Nmax];
  6. int n,b;
  7. Huge N,NN,y,unu;
  8.  
  9. void Nr_Huge(Huge X,int n) /// X <- n
  10. {
  11. int i=0;
  12. X[0]=0;
  13. if(n==0)
  14. X[0]=1,X[1]=0;
  15. else
  16. while(n)
  17. ++X[0],X[++i]=n%10,n/=10;
  18. }
  19.  
  20. void Add(Huge A,Huge B) /// A <- A+B
  21. {
  22. int i,T=0;
  23. if(B[0]>A[0])
  24. {
  25. for(i=A[0]+1;i<=B[0];)
  26. A[i++]=0;
  27. A[0]=B[0];
  28. }
  29. else
  30. for(i=B[0]+1;i<=A[0];)
  31. B[i++]=0;
  32. for(i=1;i<=A[0];i++)
  33. A[i]+=B[i]+T,T=A[i]/10,A[i]%=10;
  34. if(T)
  35. A[++A[0]]=T;
  36. }
  37.  
  38. void Subtract(Huge A, Huge B) /// A <- A-B
  39. {
  40. int i, T=0;
  41. for(i=B[0]+1;i<=A[0];)
  42. B[i++]=0;
  43. for(i=1;i<=A[0];++i)
  44. A[i]+=(T=(A[i]-=B[i]+T)<0)?10:0; /// Adica A[i]=A[i]-(B[i]+T); if (A[i]<0) T=1; else T=0; if (T) A[i]+=10; */
  45. while (!A[A[0]])
  46. --A[0];
  47. }
  48.  
  49. void Mult(Huge H,int X) /// H <- H*X
  50. {
  51. int T=0;
  52. for(int i=1;i<=H[0];++i)
  53. H[i]=H[i]*X+T,T=H[i]/10,H[i]=H[i]%10;
  54. while(T) /// Cat timp exista transport
  55. H[++H[0]]=T%10,T/=10;
  56. }
  57.  
  58. void MultHuge(Huge A,Huge B) /// A <- A x B
  59. {
  60. if(B[0]==1&&B[1]==0) /// Verific daca al doilea numar este 0
  61. {
  62. A[0]=1;
  63. A[1]=0;
  64. return;
  65. }
  66. int i,j,T=0;
  67. Huge C;
  68. C[0]=A[0]+B[0]-1;
  69. for(i=1;i<=A[0]+B[0];)
  70. C[i++]=0;
  71. for(i=1;i<=A[0];++i)
  72. for(j=1;j<=B[0];++j)
  73. C[i+j-1]+=A[i]*B[j];
  74. for(i=1;i<=C[0];++i)
  75. T=(C[i]+=T)/10,C[i]%=10;
  76. if(T)
  77. C[++C[0]]=T;
  78. for(i=0;i<=C[0];++i)
  79. A[i]=C[i];
  80. }
  81.  
  82. int Sgn(Huge H1,Huge H2) /// -1 daca H1<H2; 0 daca H1=H2 si 1 daca H1>H2
  83. {
  84. /// Elimina zero-urile semnificative, daca exista.
  85. while(H1[0]&&!H1[H1[0]])
  86. H1[0]--;
  87. while(H2[0]&&!H2[H2[0]])
  88. H2[0]--;
  89. if(H1[0]<H2[0])
  90. {
  91. return -1;
  92. }
  93. else
  94. if (H1[0] > H2[0])
  95. {
  96. return +1;
  97. }
  98. for(int i=H1[0];i>0;--i)
  99. {
  100. if(H1[i]<H2[i])
  101. {
  102. return -1;
  103. }
  104. else
  105. if(H1[i]>H2[i])
  106. {
  107. return +1;
  108. }
  109. }
  110. return 0;
  111. }
  112.  
  113. void putere(Huge B,int n)
  114. {
  115. Nr_Huge(B,b);--n; /// B <- b^1
  116. while(n)
  117. {
  118. if(n%2==0)
  119. MultHuge(N,N),n/=2;
  120. else
  121. MultHuge(B,N),--n;
  122. }
  123. }
  124.  
  125. int cautbin(int st,int dr)
  126. {
  127. int mij=(st+dr)/2;
  128. if(st>dr)
  129. return st-1;
  130. Huge BlaN;
  131. Nr_Huge(N,b);
  132. putere(BlaN,mij);
  133. int semn=Sgn(BlaN,y);
  134. if(semn==1)
  135. cautbin(st,mij-1);
  136. if(semn==-1)
  137. cautbin(mij+1,dr);
  138. if(semn==0)
  139. return mij;
  140. }
  141.  
  142. int main()
  143. {
  144. ifstream f("inequation.in");
  145. ofstream g("inequation.out");
  146. int t;
  147. f>>t;
  148. char s[25001];
  149. while(t--)
  150. {
  151. f>>b;
  152. f>>n;
  153. f.get();
  154. f.get(s,25001);
  155. for(int i=1;i<=n;++i)
  156. y[n-i+1]=s[i-1]-'0';
  157. y[0]=n;
  158. unu[0]=1;unu[1]=1; /// unu <- 1
  159. Mult(y,b-1); Add(y,unu);
  160. g<<cautbin(1,n*n)<<'\n';
  161. }
  162. f.close();
  163. g.close();
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement