Advertisement
a53

Cartofi

a53
Oct 9th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. /*
  2. 2.3. Autor: Flavius Boian
  3. */
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. ifstream f("cartofi.in");
  7. ofstream g("cartofi.out");
  8.  
  9. pair<int,int> fibo(long long n)
  10. {
  11. if(n==0)
  12. return {0,1};
  13. auto p=fibo(n>>1ll);
  14. int ax=((p.second<<1)-p.first+10)%10;
  15. int c=(p.first*ax)%10;
  16. int d=(p.first*p.first+p.second*p.second)%10;
  17. if(n&1)
  18. return {d,(c+d)%10};
  19. return {c,d};
  20. }
  21.  
  22. int n,m;
  23. long long cols[61];
  24. long long query(int);
  25. void build();
  26. void solve1();
  27. void solve2();
  28. void solve3();
  29.  
  30. int32_t main()
  31. {
  32. int task;
  33. f>>task>>n>>m;
  34. if(task==1)
  35. solve1();
  36. else
  37. if(task==2)
  38. solve2();
  39. else
  40. solve3();
  41. return 0;
  42. }
  43.  
  44. void solve1()
  45. {
  46. long long ct=0,ax=(1ll*n*m)/60ll,ap=(1ll*n*m)%60ll;
  47. long long rez=0;
  48. for(int i=0,a=0,b=1,c;i<60;++i)
  49. {
  50. ct+=!b;
  51. if(i<ap)
  52. rez+=!b;
  53. c=(a+b)%10;
  54. a=b;
  55. b=c;
  56. }
  57. rez+=1ll*ax*ct;
  58. g<<rez;
  59. }
  60.  
  61. void solve2()
  62. {
  63. build();
  64. long long rez=0,ax;
  65. for(int i=n;i<=min(n+60,m);++i)
  66. {
  67. ax=query(i)-query(i-n);
  68. rez=max(rez,ax);
  69. }
  70. g<<rez;
  71. }
  72.  
  73. void solve3()
  74. {
  75. build();
  76. int q,st,dr;
  77. for(f>>q;q;--q)
  78. {
  79. f>>st>>dr;
  80. g<<(long long)query(dr)-query(st-1)<<'\n';
  81. }
  82. }
  83. void build()
  84. {
  85. long long aux[61],prv=0,md=n%60,ct=n/60,ind;
  86. memset(aux,0,sizeof aux);
  87. for(int i=0,nr;i<min(n,60);++i,prv+=m)
  88. for(int j=1;j<=min(m,60);++j)
  89. {
  90. ind=prv;
  91. if(i&1)
  92. ind+=m-j+1;
  93. else
  94. ind+=j;
  95. nr=fibo(ind).first;
  96. cols[j]+=nr;
  97. if(i<md)
  98. aux[j]+=nr;
  99. }
  100. for(int i=1;i<=min(m,60);++i)
  101. cols[i]=1ll*cols[i]*ct+aux[i],cols[i]+=cols[i-1];
  102. }
  103.  
  104. long long query(int poz)
  105. {
  106. long long rez=1ll*(poz/60)*cols[60];
  107. rez+=cols[poz%60];
  108. return rez;
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement