/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 1.816 MiB
#2 Accepted 4ms 2.059 MiB
#3 Accepted 6ms 2.059 MiB
#4 Accepted 8ms 2.062 MiB
#5 Accepted 6ms 1.875 MiB
#6 Accepted 10ms 2.074 MiB
#7 Accepted 10ms 2.105 MiB
#8 Accepted 7ms 2.109 MiB
#9 Accepted 10ms 1.906 MiB
#10 Accepted 9ms 2.109 MiB
#11 Accepted 4ms 1.977 MiB
#12 Accepted 10ms 2.113 MiB
#13 Accepted 7ms 2.109 MiB
#14 Accepted 9ms 1.926 MiB
#15 Accepted 10ms 1.945 MiB
#16 Accepted 232ms 2.145 MiB
#17 Accepted 224ms 2.836 MiB
#18 Accepted 249ms 3.598 MiB
#19 Accepted 247ms 3.598 MiB
#20 Accepted 247ms 3.598 MiB
#21 Accepted 229ms 2.832 MiB

Code

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define MP make_pair
#define ll long long
#define F first
#define S second

const long long MOD=1e9+7;
/*
vector <int> primes;
int first_prime[(int)1e6+1];

void seive(){

  for (int i=2;i<=1e6;i++){
        if (first_prime[i]==0){
                first_prime[i]=i;
                primes.push_back(i);
            for (int j=i+i;j<=1e6;j+=i){
                    if (first_prime[j]==0)
                first_prime[j]=i;
            }
        }
  }
}





long long mulmod(long long x,long long y,long long m){
    return ((x%m)*(y%m))%m;
}

long long addmod(long long x,long long y,long long m){
    if (x+y>=m) return x-m+y;
    return x+y;
}

long long submod(long long x, long long y, long long m){
    if (x-y<0) return x-y+m;
    return x-y;
}

long long inv (long long x, long long m){
 return powmod(x,m-2,m);
}

struct DSU {
	vi e; void init(int N) { e = vi(N,-1); }
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
	bool sameSet(int a, int b) { return get(a) == get(b); }
	int size(int x) { return -e[get(x)]; }
	bool unite(int x, int y) { // union by size
		x = get(x), y = get(y); if (x == y) return 0;
		if (e[x] > e[y]) swap(x,y);
		e[x] += e[y]; e[y] = x; return 1;
	}
};

int longest_path(unordered_map< int, vector < int > >& tree,int n )
{
    vector < int > leafs ;
    vector < int > dgr ( n+1 );
    for ( auto el : tree )
    {
        dgr[el.first] = el.second.size() ;
    }
    for ( int i = 1 ; i <= n ; i++ )
    {
        if ( dgr[i] == 1 ) leafs.pb(i) ;
    }
    int ans = 2 ;
    while ( leafs.size() > 0 )
    {
        vector < int > new_leafs ;
        for ( auto el : leafs )
        {
            for ( auto nd : tree[el] )
            {
                dgr[nd] -- ;
                if ( dgr[nd] == 1 ) new_leafs.pb(nd) ;
            }
        }
        leafs = new_leafs ;
        if ( leafs.size() > 1 ) ans += 2 ;
        else if ( leafs.size() == 1 ) ans += 1 ;
    } // center is the last element in the leafs vector
    return ans ;
}

char a[16]={'0','1','2','3','4','5','6','7','8','9',
  'A','B','C','D','E','F'};
string tenToM(int n, int m)
{
  int temp=n;
  string result="";
  while (temp!=0)
  {
    result=a[temp%m]+result;
    temp/=m;
  }
  return result;
}

long long powmod(long long x,long long y,long long n){
    long long res=1;
    while(y>0){
        if (y&1){res=((res%n)*(x%n))%n;}
        x=((x%n)*(x%n))%n;
        y/=2;
    }
    return res;
}
long long mulmod(long long x,long long y,long long m){
    return ((x%m)*(y%m))%m;
}

ll modInverse ( ll n , ll Mod )
{
  return powmod ( n , Mod - 2 , Mod ) ;
}

const int N=1e5+5;
bool vis[N];
ll depth[N];
vector <int> leaf;

void dfs(int p, vector <vector <int> >& v){
    vis[p]=1;
    if (v[p].size()==1 && p!=1){leaf.pb(p); return;}
    for (int i=0;i<v[p].size();i++){
        if (!vis[v[p][i]]){
            depth[v[p][i]]=depth[p]+1;
            dfs(v[p][i],v);
        }
    }
}
ll fact[N];
void factorial(){

    fact[0]=1;
    for (int i=1;i<N;i++){
        fact[i]=(fact[i-1]*i)%MOD;
    }
}
ll ncr(long long n,long long r, long long p){
 return (fact[n]*modInverse(mulmod(fact[r],fact[n-r],p),p))%p;
}



int parent[5005][14],dep[5005],dep2[5005];
void dfs(int p, int x, vector <vector <int> >& v){
    if (v[x].size()==1 && x!=1){dep2[x]=1;return;}
    int d=0;
    for (int i=0;i<v[x].size();i++){
        if (v[x][i]==p)continue;
        parent[v[x][i]][0]=x;

        for (int j=1;j<14;j++){
            parent[v[x][i]][j]=parent[parent[v[x][i]][j-1]][j-1];
        }
        dep[v[x][i]]=dep[x]+1;
        dfs(x,v[x][i],v);
       d=max(d,dep2[v[x][i]]+1);
    }
    dep2[x]=d;
}
int bi_lift(int x,int k){

if (k>=dep[x]){return 1;}
    for ( int i=13;i>=0;i--){
        if (k>=(1<<i)){
            x=parent[x][i];
            k-=1<<i;
        }
    }
    return x;
}

int child(int p, int x, vector <vector <int> >& v){

    int ans=x;
    if (v[x].size()==1 && x!=1){return x;}
    int mx=0,ind=-1;
    for (int i=0;i<v[x].size();i++){
        if (v[x][i]==p)continue;
        else {
            if (mx<dep2[v[x][i]]){
                mx=dep2[v[x][i]];
                ind=i;
            }

        }
    }
    if (ind!=-1)
     ans=child(x,v[x][ind],v);
    return ans;

}

__int128 read() {
    __int128 x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
void print(__int128 x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}
bool cmp(__int128 x, __int128 y) { return x > y; }

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
  template<
	  typename Key, // Key type
	  typename Mapped, // Mapped-policy
	  typename Cmp_Fn = std::less<Key>, // Key comparison functor
	  typename Tag = rb_tree_tag, // Specifies which underlying data structure to use
	  template<
	  typename Const_Node_Iterator,
	  typename Node_Iterator,
	  typename Cmp_Fn_,
	  typename Allocator_>
	  class Node_Update = null_node_update, // A policy for updating node invariants
	  typename Allocator = std::allocator<char> > // An allocator type
	  class tree;
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update> ordered_set;



struct segtr{
   int sz;
   vector <ll > pre,suf,ans,sum;

    void cmb(int x){
        sum[x]=sum[2*x]+sum[2*x+1];
        pre[x]=max(pre[2*x],sum[2*x]+pre[2*x+1]);
        suf[x]=max(suf[2*x+1],sum[2*x+1]+suf[2*x]);
        ans[x]=max({pre[x],suf[x],suf[2*x]+pre[2*x]});
    }

    void build(ll a[], int x, int lx, int rx){
        if (lx==rx){
            sum[x]=a[lx];
            pre[x]=suf[x]=ans[x]=max(0ll,a[lx]);
            return ;
        }
        int m=(lx+rx)/2;
        build(a,2*x,lx,m);
        build(a,2*x+1,m+1,rx);
        cmb(x);
    }

   void build(ll a[],int n){
       sz=1;
       while(sz<n){
        sz*=2;
       }
       pre.assign(2*sz,0ll);
       suf.assign(2*sz,0ll);
       ans.assign(2*sz,0ll);
       sum.assign(2*sz,0ll);
    build(a,1,1,sz);
   }

   void update(int i,ll v,int x, int lx, int rx){
    if (lx==rx){
        sum[x]=v;
            pre[x]=suf[x]=ans[x]=max(0ll,v);
            return ;
    }
    int m=(lx+rx)/2;
    if (i<=m){
        update(i,v,2*x,lx,m);
    }
    else {
        update(i,v,2*x+1,m+1,rx);
    }
    cmb(x);
   }

   void update(int i,ll v){
    update(i,v,1,1,sz);
   }

   ll pr(){
    return ans[1];
   }
};*/

long long powmod(long long x,long long y,long long n){
    long long res=1;
    while(y>0){
        if (y&1){res=((res%n)*(x%n))%n;}
        x=((x%n)*(x%n))%n;
        y/=2;
    }
    return res;
}
long long mulmod(long long x,long long y,long long m){
    return ((x%m)*(y%m))%m;
}

ll modInverse ( ll n , ll Mod )
{
  return powmod ( n , Mod - 2 , Mod ) ;
}
const int N=2*100001;
ll fact[N];
void factorial(){

    fact[0]=1;
    for (int i=1;i<N;i++){
        fact[i]=(fact[i-1]*i)%MOD;
    }
}
ll ncr(long long n,long long r, long long p){
 return (fact[n]*modInverse(mulmod(fact[r],fact[n-r],p),p))%p;
}

void solve()
{

  ll n,k;
  cin>>n>>k;
  ll a[n],sum=0,z=0;
  for (int i=0;i<n;i++){
    cin>>a[i];
    sum+=a[i];
    if (a[i]==0)z++;
  }
  if (sum<k)cout<<0<<endl;
  else {
    ll zsum=0;
    for (ll i=0;i<=z;i++){
        zsum+=ncr(z,i,MOD);
        if (zsum>=MOD)zsum%=MOD;
    }
    //cout<<zsum<<' ';
    ll j=sum-k;
    ll one=0;
    for (ll i=0;i<=j;i++){
        one+=ncr(n-z,i,MOD);
        if (one>=MOD)one%=MOD;
    }
    //cout<<one<<' ';
    ll ans=(one*zsum)%MOD;
    ans--;
    if (ans<0)ans+=MOD;
    cout<<ans<<endl;
  }

}// end of solve function


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen( ".in" , "r" , stdin );

    factorial();
    int t=1;

    cin>>t;
    while(t--)
    {
        solve();


    }

    return 0;
}

/*



*/

Information

Submit By
Type
Submission
Problem
P1094 Number of ways (Hard version)
Contest
Brain Booster #5
Language
C++17 (G++ 13.2.0)
Submit At
2024-09-05 16:41:05
Judged At
2024-11-11 02:59:47
Judged By
Score
100
Total Time
249ms
Peak Memory
3.598 MiB