Advertisement
Saleh127

Light OJ 1226 / Combinatorics

Oct 31st, 2022
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. /***
  2.  created: 2022-10-31-21.14.53
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16. const ll mod=1e9+7;
  17. const ll mx=1e6+2;
  18. ll f[1000007];
  19. ll invf[1000007];
  20.  
  21. ll bigmod(ll a,ll c,ll m)
  22. {
  23.     if(!c) return 1ll;
  24.     ll x=bigmod(a,c/2,m);
  25.     x=(x*x)%m;
  26.     if(c&1){
  27.         x=(x*a)%m;
  28.     }
  29.     return x;
  30. }
  31. void fact()
  32. {
  33.     f[0]=1ll;
  34.     for(ll i=1; i<=mx; i++)
  35.     {
  36.         f[i]=(f[i-1]*i)%mod;
  37.     }
  38.     invf[mx]=bigmod(f[mx],mod-2,mod);
  39.     for(ll i=mx-1; i>=0; i--)
  40.     {
  41.         invf[i]=(invf[i+1]*(i+1))%mod;
  42.     }
  43. }
  44.  
  45. ll nCr(ll n,ll r)
  46. {
  47.     if(r>n) return 0ll;
  48.     return ((f[n]*invf[n-r])%mod * invf[r])%mod;
  49. }
  50.  
  51. int main()
  52. {
  53.     ios_base::sync_with_stdio(0);
  54.     cin.tie(0);
  55.     cout.tie(0);
  56.  
  57.     fact();
  58.  
  59.     test
  60.     {
  61.         ll n,m,i,j,k,l,sum=0;
  62.  
  63.         cin>>n; ll a[n+4];
  64.  
  65.         for(i=0; i<n; i++)
  66.         {
  67.             cin>>a[i];
  68.             sum+=a[i];
  69.         }
  70.  
  71.         ll ans=1;
  72.  
  73.         for(i=n-1; i>=0; i--)
  74.         {
  75.             ans*=nCr(sum-1,a[i]-1);
  76.             ans%=mod;
  77.             sum-=a[i];
  78.         }
  79.  
  80.         cout<<"Case "<<cs<<": "<<ans<<nl;
  81.     }
  82.  
  83.     get_lost_idiot;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement