Advertisement
a53

sir9

a53
Mar 14th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. // sir-Em O(n)
  2. #include <fstream>
  3. using namespace std;
  4. #define M 20173333
  5. int a[131072];
  6. int inv(long long x)
  7. {long long p=1,n=M-2;
  8. while (n>0)
  9. { if (n%2==1)
  10. { p=(p*x)%M; n--;}
  11. else
  12. { x=(x*x)%M; n/=2;}
  13. }
  14. return p;
  15. }
  16. int main()
  17. {
  18. ifstream fi("sir9.in"); ofstream fo("sir9.out");
  19. int n,u,i,p;
  20. long long r,t;
  21. fi>>p;
  22. fi>>n>>u;
  23. if(p==1)
  24. { /*
  25. a[0]=1; // triunghiul lui Pascal
  26. for(i=1;i<n;i++)
  27. { a[i]=1;
  28. for(j=i-1;j>=0;j--)
  29. a[j]=(a[j]+a[j-1])%M;
  30. }
  31. fo<<a[u-1]<<"\n";
  32. */
  33. for(r=1,i=2;i<n;i++)
  34. r=(r*i)%M;
  35. for(t=1,i=2;i<u;i++)
  36. t=(t*i)%M;
  37. r=(r*inv(t))%M;
  38. for(t=1,i=2;i<=n-u;i++)
  39. t=(t*i)%M;
  40. r=(r*inv(t))%M;
  41. fo<<r<<"\n";
  42. }
  43. else
  44. { a[1]=1;
  45. r=(n==u);
  46. for(i=2;i<=n;i++)
  47. { if(i>u) t=a[i-1]+M-a[i-u-1];
  48. else t=a[i-1];
  49. a[i]=(a[i-1]+t)%M;
  50. if(i>=n-u+1) r=(r+t)%M;
  51. }
  52. fo<<r<<"\n";
  53. }
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement