Advertisement
a53

2017_PARSER

a53
Mar 25th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <fstream>
  2. #define Xmin 0LL
  3. #define Xmax 1000000000000000000LL
  4. #define LL long long
  5. using namespace std;
  6. /// Clasa pentru citiri de date din fisiere
  7. /// =======================================
  8. class InParser
  9. {
  10. private:
  11. FILE *fin;
  12. char *buff;
  13. int sp;
  14.  
  15. char read_ch()
  16. {
  17. ++sp;
  18. if (sp == 4096)
  19. {
  20. sp = 0;
  21. fread(buff, 1, 4096, fin);
  22. }
  23. return buff[sp];
  24. }
  25. public:
  26. InParser(const char* nume)
  27. {
  28. fin = fopen(nume, "r");
  29. buff = new char[4096]();
  30. sp = 4095;
  31. }
  32.  
  33. InParser& operator >> (int &n)
  34. {
  35. char c;
  36. while (!isdigit(c = read_ch()) && c != '-');
  37. int sgn = 1;
  38. if (c == '-')
  39. {
  40. n = 0;
  41. sgn = -1;
  42. }
  43. else
  44. {
  45. n = c - '0';
  46. }
  47. while (isdigit(c = read_ch()))
  48. {
  49. n = 10 * n + c - '0';
  50. }
  51. n *= sgn;
  52. return *this;
  53. }
  54.  
  55. InParser& operator >> (long long &n)
  56. {
  57. char c;
  58. n = 0;
  59. while (!isdigit(c = read_ch()) && c != '-');
  60. long long sgn = 1;
  61. if (c == '-')
  62. {
  63. n = 0;
  64. sgn = -1;
  65. }
  66. else
  67. {
  68. n = c - '0';
  69. }
  70. while (isdigit(c = read_ch()))
  71. {
  72. n = 10 * n + c - '0';
  73. }
  74. n *= sgn;
  75. return *this;
  76. }
  77. };
  78. /// Clasa pentru scrieri de date in fisiere
  79. /// =======================================
  80. class OutParser
  81. {
  82. private:
  83. FILE *fout;
  84. char *buff;
  85. int sp;
  86.  
  87. void write_ch(char ch)
  88. {
  89. if (sp == 50000)
  90. {
  91. fwrite(buff, 1, 50000, fout);
  92. sp = 0;
  93. buff[sp++] = ch;
  94. }
  95. else
  96. {
  97. buff[sp++] = ch;
  98. }
  99. }
  100. public:
  101. OutParser(const char* name)
  102. {
  103. fout = fopen(name, "w");
  104. buff = new char[50000]();
  105. sp = 0;
  106. }
  107. ~OutParser()
  108. {
  109. fwrite(buff, 1, sp, fout);
  110. fclose(fout);
  111. }
  112.  
  113. OutParser& operator << (int vu32)
  114. {
  115. if (vu32 <= 9)
  116. {
  117. write_ch(vu32 + '0');
  118. }
  119. else
  120. {
  121. (*this) << (vu32 / 10);
  122. write_ch(vu32 % 10 + '0');
  123. }
  124. return *this;
  125. }
  126.  
  127. OutParser& operator << (long long vu64)
  128. {
  129. if (vu64 <= 9)
  130. {
  131. write_ch(vu64 + '0');
  132. }
  133. else
  134. {
  135. (*this) << (vu64 / 10);
  136. write_ch(vu64 % 10 + '0');
  137. }
  138. return *this;
  139. }
  140.  
  141. OutParser& operator << (char ch)
  142. {
  143. write_ch(ch);
  144. return *this;
  145. }
  146. OutParser& operator << (const char *ch)
  147. {
  148. while (*ch)
  149. {
  150. write_ch(*ch);
  151. ++ch;
  152. }
  153. return *this;
  154. }
  155. };
  156.  
  157. InParser f("2017.in");
  158. OutParser g("2017.out");
  159. int Q,c;
  160. LL K;
  161.  
  162. LL F(LL x,int c)
  163. {
  164. LL p,n,z;
  165. n=0; /// nr de aparitii a cifrei c
  166. p=1; /// pornim de la ultima cifra(p=10-penultima cifra,p=100-antepenultima...)
  167. z=0; /// in z memoram ultimele cifre ale numarului k, care au fost extrase
  168. while(x>=c)
  169. {
  170. if(x%10>c)
  171. n+=(x/10+1)*p;
  172. else
  173. if(x%10==c)
  174. n+=x/10*p+z+1;
  175. else
  176. n+=x/10*p;
  177. z+=x%10*p;
  178. x/=10;
  179. p*=10;
  180. }
  181. return n;
  182. }
  183.  
  184. LL query(int c,LL K)
  185. {
  186. LL le=K/10+1,ri=K*10,mid,best;
  187. while(le<=ri)
  188. {
  189. mid=(le+ri)/2;
  190. if(F(mid,c)>=K)
  191. {
  192. best=mid;
  193. ri=mid-1;
  194. }
  195. else le=mid+1;
  196. }
  197. return best;
  198. }
  199.  
  200. int main()
  201. {
  202.  
  203. for(f>>Q;Q;--Q)
  204. {
  205. f>>c>>K;
  206. g<<query(c,K)<<'\n';
  207. }
  208. return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement