a53

FB

a53
Oct 7th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. # include <fstream>
  2. # include <cstring>
  3. # include <algorithm>
  4. using namespace std;
  5.  
  6. ifstream f("fibosir.in");
  7. ofstream g("fibosir.out");
  8.  
  9. int N, M, K, la, lb, lc;
  10. char s[420001], a[500], b[500], c[500];
  11.  
  12. void add(char *a, char *b, char *c)
  13. {
  14. int i = 0 , j = 0;
  15. int x, k, t;
  16.  
  17. x = k = t = 0;
  18. while (i < la && j < lb)
  19. {
  20. x = (a[i] - 48) + (b[j] - 48) + t;
  21. c[k] = (x % 10) + 48;
  22. t = x/10;
  23. ++i; ++j; ++k;
  24. }
  25. while (i < la)
  26. {
  27. x = (a[i] - 48) + t;
  28. c[k] = (x % 10) + 48;
  29. t = x/10;
  30. ++i; ++k;
  31. }
  32. while (j < lb)
  33. {
  34. x = (b[j] - 48) + t;
  35. c[k] = (x % 10) + 48;
  36. t = x/10;
  37. ++j; ++k;
  38. }
  39. if (t)
  40. {
  41. c[k] = t + 48;
  42. ++k;
  43. }
  44. c[k] = '\0';
  45. lc = k;
  46. }
  47. int main()
  48. {
  49. char max;
  50. int i, j, poz, nr;
  51.  
  52. f >> N >> M >> K;
  53.  
  54. //construim fibosir(n)
  55. a[0] = b[0] = s[0] = s[1] = '1';
  56. a[1] = b[1] = s[2] = '\0';
  57. la = lb = lc = 1;
  58. for (i=2; i<N; ++i)
  59. {
  60. add(a, b, c);
  61. memcpy(a, b, lb);
  62. memcpy(b, c, lc);
  63. reverse(c, c + lc);
  64. la = lb; lb = lc;
  65. strcat(s, c);
  66. }
  67. N = strlen(s);
  68. i = 0;
  69. while (i < N)
  70. {
  71. for (j=1, max = '0'; j<=M; ++j)
  72. {
  73. if (s[i + j*K] > max)
  74. {
  75. max = s[i + j*K];
  76. poz = j;
  77. if (max == '9') break;
  78. }
  79. }
  80. if (s[i] < max) // jump
  81. {
  82. i += (poz * K);
  83. M -= poz;
  84. }
  85. else
  86. {
  87. g << s[i++];
  88. }
  89. }
  90. return 0;
  91. }
Add Comment
Please, Sign In to add comment