Advertisement
a53

FactorialQuery

a53
May 2nd, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #define M 1000000007
  4. #define N 1000001
  5. using namespace std;
  6. int v[N];
  7.  
  8. #define DIM 10000
  9. char buff[DIM];
  10. int poz=0;
  11.  
  12. void citeste(int &numar)
  13. {
  14. numar = 0;
  15. //cat timp caracterul din buffer nu e cifra ignor
  16. while (buff[poz] < '0' || buff[poz] > '9')
  17. //daca am "golit" bufferul atunci il umplu
  18. if (++poz == DIM)
  19. fread(buff,1,DIM,stdin),poz=0;
  20. //cat timp dau de o cifra recalculez numarul
  21. while ('0'<=buff[poz] && buff[poz]<='9')
  22. {
  23. numar = numar*10 + buff[poz] - '0';
  24. if (++poz == DIM)
  25. fread(buff,1,DIM,stdin),poz=0;
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. int n;
  32. cin>>n;
  33. for(int i=0;i<n;++i)
  34. citeste(v[i]);
  35. sort(v,v+n);
  36. int fact=1;
  37. for(int i=1;i<=v[0];++i)
  38. fact=(int)(1LL*fact*i%M);
  39. int p=fact;
  40. for(int i=1;i<n;++i)
  41. {
  42. if(v[i]==v[i-1])
  43. p=(int)(1LL*p*fact%M);
  44. else
  45. {
  46. for(int j=v[i-1]+1;j<=v[i];++j)
  47. fact=(int)(1LL*fact*j%M);
  48. p=(int)(1LL*p*fact%M);
  49. }
  50. }
  51. cout<<p<<'\n';
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement