Advertisement
a53

fibosir_100_PUNCTE

a53
Nov 29th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #define Nmax 500
  5. #define Rmax 420000
  6. using namespace std;
  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.  
  26. public:
  27. InParser(const char* nume)
  28. {
  29. fin=fopen(nume,"r");
  30. buff=new char[4096]();
  31. sp=4095;
  32. }
  33.  
  34. InParser& operator >> (int &n)
  35. {
  36. char c;
  37. while(!isdigit(c=read_ch())&&c!='-');
  38. int sgn=1;
  39. if (c=='-')
  40. {
  41. n=0;
  42. sgn=-1;
  43. }
  44. else
  45. {
  46. n=c-'0';
  47. }
  48. while(isdigit(c=read_ch()))
  49. {
  50. n=10*n+c-'0';
  51. }
  52. n*=sgn;
  53. return *this;
  54. }
  55.  
  56. InParser& operator >> (long long &n)
  57. {
  58. char c;
  59. n=0;
  60. while(!isdigit(c=read_ch())&&c!='-');
  61. long long sgn=1;
  62. if(c=='-')
  63. {
  64. n=0;
  65. sgn=-1;
  66. }
  67. else
  68. {
  69. n=c-'0';
  70. }
  71. while(isdigit(c=read_ch()))
  72. {
  73. n=10*n+c-'0';
  74. }
  75. n*=sgn;
  76. return *this;
  77. }
  78. };
  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==500000)
  90. {
  91. fwrite(buff,1,500000,fout);
  92. sp=0;
  93. buff[sp++]=ch;
  94. }
  95. else
  96. {
  97. buff[sp++]=ch;
  98. }
  99. }
  100.  
  101. public:
  102. OutParser(const char* name)
  103. {
  104. fout=fopen(name,"w");
  105. buff=new char[500000]();
  106. sp=0;
  107. }
  108. ~OutParser()
  109. {
  110. fwrite(buff,1,sp,fout);
  111. fclose(fout);
  112. }
  113.  
  114. OutParser& operator <<(int vu32)
  115. {
  116. if(vu32<=9)
  117. {
  118. write_ch(vu32+'0');
  119. }
  120. else
  121. {
  122. (*this) <<(vu32/10);
  123. write_ch(vu32%10+'0');
  124. }
  125. return *this;
  126. }
  127.  
  128. OutParser& operator <<(long long vu64)
  129. {
  130. if(vu64<=9)
  131. {
  132. write_ch(vu64+'0');
  133. }
  134. else
  135. {
  136. (*this) <<(vu64/10);
  137. write_ch(vu64%10+'0');
  138. }
  139. return *this;
  140. }
  141.  
  142. OutParser& operator <<(char ch)
  143. {
  144. write_ch(ch);
  145. return *this;
  146. }
  147. OutParser& operator <<(const char *ch)
  148. {
  149. while(*ch)
  150. {
  151. write_ch(*ch);
  152. ++ch;
  153. }
  154. return *this;
  155. }
  156. };
  157.  
  158. InParser f("fibosir.in");
  159. OutParser g("fibosir.out");
  160. int N, M, K, la, lb, lc;
  161. char s[420001], a[500], b[500], c[500];
  162.  
  163. void add(char *a, char *b, char *c)
  164. {
  165. int i = 0 , j = 0;
  166. int x, k, t;
  167.  
  168. x = k = t = 0;
  169. while (i < la && j < lb)
  170. {
  171. x = (a[i] - 48) + (b[j] - 48) + t;
  172. c[k] = (x % 10) + 48;
  173. t = x/10;
  174. ++i; ++j; ++k;
  175. }
  176. while (i < la)
  177. {
  178. x = (a[i] - 48) + t;
  179. c[k] = (x % 10) + 48;
  180. t = x/10;
  181. ++i; ++k;
  182. }
  183. while (j < lb)
  184. {
  185. x = (b[j] - 48) + t;
  186. c[k] = (x % 10) + 48;
  187. t = x/10;
  188. ++j; ++k;
  189. }
  190. if (t)
  191. {
  192. c[k] = t + 48;
  193. ++k;
  194. }
  195. c[k] = '\0';
  196. lc = k;
  197. }
  198. int main()
  199. {
  200. char max;
  201. int i, j, poz;
  202.  
  203. f >> N >> M >> K;
  204.  
  205. //construim fibosir(n)
  206. a[0] = b[0] = s[0] = s[1] = '1';
  207. a[1] = b[1] = s[2] = '\0';
  208. la = lb = lc = 1;
  209. for (i=2; i<N; ++i)
  210. {
  211. add(a, b, c);
  212. memcpy(a, b, lb);
  213. memcpy(b, c, lc);
  214. reverse(c, c + lc);
  215. la = lb; lb = lc;
  216. strcat(s, c);
  217. }
  218. N = strlen(s);
  219. i = 0;
  220. while (i < N)
  221. {
  222. for (j=1, max = '0'; j<=M; ++j)
  223. {
  224. if (s[i + j*K] > max)
  225. {
  226. max = s[i + j*K];
  227. poz = j;
  228. if (max == '9') break;
  229. }
  230. }
  231. if (s[i] < max) /// jump
  232. {
  233. i += (poz * K);
  234. M -= poz;
  235. }
  236. else
  237. {
  238. g << s[i++];
  239. }
  240. }
  241. return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement