Advertisement
a53

shop

a53
Apr 18th, 2020
1,732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. #include <fstream>
  2. #define N 10001
  3. #define M 1000000007
  4. using namespace std;
  5. int F[N],P[5001];
  6.  
  7. void factoriale()
  8. {
  9. int m, p, pro;
  10. F[0]=1,P[0]=1;
  11. /// Aici se calculeaza factorialele pana la 5000 si produsele partiale de factoriale
  12. for(int i=1;i<=5000;++i)
  13. F[i]=(1LL*F[i-1]*i)%M,P[i]=(1LL*P[i-1]*F[i])%M;
  14. /// Aici se calculeaza n!, unde n=k*(k+1)/2, pentru k>=100;
  15. /// la factorialul calculat anterior se adauga doi factori deodata
  16. /// produsul (j+2)*(j+3) difera de j*(j+1) prin 4*j+6
  17. p=F[5000];
  18. int j=5001;
  19. pro=j*(j+1);
  20. for(int i=100;i<=5000;++i)
  21. {
  22. m=i*(i+1)/2;
  23. while(j<m)
  24. p=(1LL*p*pro)%M,pro=(pro+4*j+6)%M,j+=2;
  25. if(j==m+1)
  26. F[4901+i]=p;
  27. else
  28. F[4901+i]=(1LL*p*m)%M;
  29. }
  30. }
  31.  
  32. int pw(int b,int e)
  33. {
  34. if(e==0)
  35. return 1;
  36. if(e%2==0)
  37. {
  38. int x=pw(b,e/2);
  39. return (1LL*x*x)%M;
  40. }
  41. else
  42. return (1LL*b*pw(b,e-1))%M;
  43. }
  44.  
  45. int main()
  46. {
  47. ifstream f("shop.in");
  48. factoriale();
  49. int t;
  50. f>>t;
  51. int k,sol;
  52. ofstream g("shop.out");
  53. while(t--)
  54. {
  55. f>>k;
  56. int n=k*(k+1)/2,d;
  57. if(n<=5000)
  58. d=F[n];
  59. else
  60. d=F[4901+k];
  61. sol=(1LL*d*pw(P[k],M-2))%M;
  62. g<<sol<<'\n';
  63. }
  64. f.close(),g.close();
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement