fork download
  1. //Lib
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<ctime>
  7.  
  8. #include<iostream>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<string>
  12. #include<queue>
  13. #include<stack>
  14. #include<set>
  15. #include<map>
  16. using namespace std;
  17. //Macro
  18. #define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i)
  19. #define drep(i,a,b) for(int i=a,tt=b;i>=tt;--i)
  20. #define erep(i,e,x) for(int i=x;i;i=e[i].next)
  21. #define irep(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++)
  22. #define read() (strtol(ipos,&ipos,10))
  23. #define sqr(x) ((x)*(x))
  24. #define pb push_back
  25. #define PS system("pause");
  26. typedef long long ll;
  27. typedef pair<int,int> pii;
  28. const int oo=~0U>>1;
  29. const double inf=1e100;
  30. const double eps=1e-6;
  31. string name="", in=".in", out=".out";
  32. //Var
  33. ll mod=1000000007;
  34. int c[16],n;
  35. ll f[16][16][16][16][16][6];
  36. bool vis[16][16][16][16][16][6];
  37. ll DS(int c1,int c2,int c3,int c4,int c5,int c)
  38. {
  39. if(c1+c2+c3+c4+c5==0)return 1;
  40. if(vis[c1][c2][c3][c4][c5][c])return f[c1][c2][c3][c4][c5][c];
  41. ll tmp=0;
  42. if(c1)
  43. tmp+=(c1-(c==2))*DS(c1-1,c2,c3,c4,c5,1);
  44. if(c2)
  45. tmp+=(c2-(c==3))*DS(c1+1,c2-1,c3,c4,c5,2);
  46. if(c3)
  47. tmp+=(c3-(c==4))*DS(c1,c2+1,c3-1,c4,c5,3);
  48. if(c4)
  49. tmp+=(c4-(c==5))*DS(c1,c2,c3+1,c4-1,c5,4);
  50. if(c5)
  51. tmp+=c5*DS(c1,c2,c3,c4+1,c5-1,5);
  52. tmp%=mod;
  53. vis[c1][c2][c3][c4][c5][c]=true;
  54. return f[c1][c2][c3][c4][c5][c]=tmp;
  55. }
  56. void Work()
  57. {
  58. int t;
  59. scanf("%d",&n);
  60. rep(i,1,n)scanf("%d",&t),c[t]++;
  61. cout<<DS(c[1],c[2],c[3],c[4],c[5],0)<<endl;
  62. }
  63. int main()
  64. {
  65. // freopen((name+in).c_str(),"r",stdin);
  66. // freopen((name+out).c_str(),"w",stdout);
  67. // Init();
  68. Work();
  69. return 0;
  70. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty