Advertisement
a53

inequation

a53
Apr 8th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #define N 25001
  4. using namespace std;
  5. typedef int Huge[N];
  6.  
  7. void Nr_Huge(Huge X,int n) /// X <- n
  8. {
  9. int i=0;
  10. X[0]=0;
  11. if(n==0)
  12. X[0]=1,X[1]=0;
  13. else
  14. while(n)
  15. ++X[0],X[++i]=n%10,n/=10;
  16. }
  17.  
  18. void Add(Huge A,Huge B) /// A <- A+B
  19. {
  20. int i,T=0;
  21. if(B[0]>A[0])
  22. {
  23. for(i=A[0]+1;i<=B[0];)
  24. A[i++]=0;
  25. A[0]=B[0];
  26. }
  27. else
  28. for(i=B[0]+1;i<=A[0];)
  29. B[i++]=0;
  30. for(i=1;i<=A[0];i++)
  31. A[i]+=B[i]+T,T=A[i]/10,A[i]%=10;
  32. if(T)
  33. A[++A[0]]=T;
  34. }
  35.  
  36. void Mult(Huge H,short X) /// H <- H*X
  37. {
  38. short T=0;
  39. for(int i=1;i<=H[0];++i)
  40. H[i]=H[i]*X+T,T=H[i]/10,H[i]=H[i]%10;
  41. while(T) /// Cat timp exista transport
  42. H[++H[0]]=T%10,T/=10;
  43. }
  44.  
  45. int Sgn(Huge H1,Huge H2) /// -1 daca H1<H2; 0 daca H1=H2 si 1 daca H1>H2
  46. {
  47. /// Elimina zero-urile semnificative, daca exista.
  48. while(H1[0]&&!H1[H1[0]])
  49. H1[0]--;
  50. while(H2[0]&&!H2[H2[0]])
  51. H2[0]--;
  52. if(H1[0]<H2[0])
  53. {
  54. return -1;
  55. }
  56. else
  57. if (H1[0] > H2[0])
  58. {
  59. return +1;
  60. }
  61. for(int i=H1[0];i>0;--i)
  62. {
  63. if(H1[i]<H2[i])
  64. {
  65. return -1;
  66. }
  67. else
  68. if(H1[i]>H2[i])
  69. {
  70. return +1;
  71. }
  72. }
  73. return 0;
  74. }
  75.  
  76. int main()
  77. {
  78. ifstream f("inequation.in");
  79. ofstream g("inequation.out");
  80. int t;
  81. f>>t;
  82. int n;
  83. short b;
  84. Huge y;
  85. char s[25001];
  86. int rez;
  87. while(t--)
  88. {
  89. f>>b;
  90. f>>n;
  91. f.get();
  92. f.get(s,25001);
  93. for(int i=1;i<=n;++i)
  94. y[n-i+1]=s[i-1]-'0';
  95. y[0]=n;
  96. Huge unu;
  97. Nr_Huge(unu,1);
  98. Mult(y,b-1); Add(y,unu);
  99. Huge BlaN;
  100. Nr_Huge(BlaN,1);
  101. int semn=Sgn(BlaN,y);
  102. rez=0;
  103. while(semn==-1||semn==0)
  104. Mult(BlaN,b),semn=Sgn(BlaN,y),++rez;
  105. g<<rez-1<<'\n';
  106. }
  107. f.close();
  108. g.close();
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement