Advertisement
a53

fibona

a53
Dec 24th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #define M 1000000007
  3. #define LL long long int
  4. using namespace std;
  5. LL n,k,p,i,s[101],numi,numa,inv,ins;
  6.  
  7. void gcd(LL &x,LL &y,int a,int b)
  8. {
  9. if(!b)
  10. x=1, y=0;
  11. else
  12. {
  13. gcd(x,y,b,a%b);
  14. LL aux=x;
  15. x=y;
  16. y=aux-y*(a/b);
  17. }
  18. }
  19.  
  20. LL fibo(LL m)
  21. {
  22. LL a,b,c,d,x,y,z,t,u,v,w,q ;
  23. a=1;b=1;
  24. c=1;d=0;
  25. x=1;y=0;
  26. z=0;t=1;
  27. --m;
  28. while(m)
  29. {
  30. if(m&1)
  31. {
  32. u=(a*x+b*z)%M;
  33. v=(a*y+b*t)%M;
  34. w=(c*x+d*z)%M;
  35. q=(c*y+d*t)%M;
  36. x=u;y=v;
  37. z=w;t=q;
  38. --m;
  39. }
  40. u=(a*a+b*c)%M ;
  41. v=(a*b+b*d)%M;
  42. w=(a*c+c*d)%M;
  43. q=(b*c+d*d)%M;
  44. a=u;b=v;
  45. c=w;d=q;
  46. m=m/2 ;
  47. }
  48. return x;
  49. }
  50.  
  51. int main()
  52. {
  53. cin>>n>>k>>p;
  54. /// calculez numitorul fractiei S (vezi indicatii)
  55. s[0]=2;
  56. s[1]=1;
  57. for(i=2;i<=k;++i)
  58. s[i]=(s[i-1]+s[i-2])%M;
  59. if(k%2==0)
  60. numi=(2-s[k]+M)%M;
  61. else
  62. numi=(-s[k]+M)%M;
  63. /// calculez inversul modular al numitorului modulo M
  64. inv=0;
  65. gcd(inv,ins,numi,M);
  66. if(inv<=0)
  67. inv=M+inv%M;
  68. /// calculez numaratorul fractiei S
  69. numa=(fibo(p)-fibo(k*n+k+p)+M)%M;
  70. if(k%2==0)
  71. numa=(numa+fibo(k*n+p))%M;
  72. else
  73. numa=(numa-fibo(k*n+p)+M)%M;
  74. if(p%2==0)
  75. numa=(numa+fibo(k-p))%M;
  76. else
  77. numa=(numa-fibo(k-p)+M)%M;
  78. cout<<(numa*inv)%M;
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement