/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 10ms 3.574 MiB
#2 Accepted 10ms 3.551 MiB
#3 Accepted 11ms 3.566 MiB
#4 Accepted 11ms 3.383 MiB
#5 Accepted 11ms 3.434 MiB
#6 Accepted 11ms 3.602 MiB
#7 Accepted 11ms 3.438 MiB
#8 Accepted 11ms 3.426 MiB
#9 Accepted 11ms 3.488 MiB
#10 Accepted 11ms 3.645 MiB
#11 Accepted 11ms 3.473 MiB
#12 Accepted 11ms 3.562 MiB
#13 Accepted 11ms 3.562 MiB
#14 Accepted 11ms 3.414 MiB
#15 Accepted 11ms 3.48 MiB
#16 Accepted 29ms 3.473 MiB
#17 Accepted 28ms 4.359 MiB
#18 Accepted 30ms 5.129 MiB
#19 Accepted 34ms 5.125 MiB
#20 Accepted 24ms 5.02 MiB
#21 Accepted 29ms 4.332 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using minheap = priority_queue<long long, vector<long long>, greater<long long>>;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define ll long long
#define ld long double
#define MOD 1000000007
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl '\n'
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define life_is_a_race ios::sync_with_stdio(false); cin.tie(nullptr);

// Modulo nCr
const int Max = 2e5 + 5;
ll fact[Max], factInv[Max];

ll mod = 1e9+7;
ll Pow(ll a, ll b)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1) ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    ans += mod;
    ans %= mod;
    return ans;
}

void build_fact() {
    fact[0] = 1;
    for(int i = 1; i < Max; i++)
    {
        fact[i] = 1LL * fact[i - 1] * i % mod;
    }
    factInv[Max - 1] = Pow(fact[Max - 1], mod - 2);
    for(int i = Max - 2; i >= 0; i--)
    {
        factInv[i] = 1LL * factInv[i + 1] * (i + 1) % mod;
    }
    return;
}

ll nCr_mod(int n, int r) {
    if(n < r or n < 0 or r < 0) return 0;
    return 1LL * fact[n] * factInv[r] % mod * factInv[n - r] % mod;
}

int nPr_mod(int n, int r) { // nPr = nCr * r! 
    if(n < r or n < 0 or r < 0) return 0;
    return (1LL * nCr_mod(n, r) * fact[r]) % mod;
}

void solve(int tc)
{
    //cout<<"Case "<<tc<<": ";
    ll n,k; cin >> n >> k;
    ll a[n],sum=0;
    for(int i=0;i<n;i++) cin >> a[i], sum+=a[i];
    if(sum<k)
    {
        cout<<0<<endl;
        return;
    }
    ll ans = 0, one;
    ll z = Pow(2,n-sum);
    ans = z-1;
    for(ll i=sum-1;i>=k;i--)
    {
        one = sum-i;
        ans += ((nCr_mod(sum,one) * z) % mod);
        ans = ans%mod;
    }
    cout << ans % mod << endl;
}
int main()
{
    life_is_a_race
    build_fact();
    int t=1; 
    cin>>t;
    for(int i=1;i<=t;i++) solve(i);
}

Information

Submit By
Type
Submission
Problem
P1094 Number of ways (Hard version)
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-04 20:48:33
Judged At
2024-11-11 03:04:20
Judged By
Score
100
Total Time
34ms
Peak Memory
5.129 MiB