/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 2.02 MiB
#2 Accepted 4ms 2.035 MiB
#3 Accepted 5ms 1.84 MiB
#4 Accepted 5ms 2.082 MiB
#5 Accepted 4ms 2.082 MiB
#6 Accepted 5ms 2.09 MiB
#7 Accepted 5ms 1.926 MiB
#8 Accepted 4ms 2.023 MiB
#9 Accepted 6ms 2.105 MiB
#10 Accepted 5ms 2.02 MiB
#11 Accepted 4ms 2.066 MiB
#12 Accepted 5ms 2.113 MiB
#13 Accepted 5ms 2.109 MiB
#14 Accepted 5ms 2.066 MiB
#15 Accepted 5ms 2.066 MiB
#16 Accepted 58ms 2.148 MiB
#17 Accepted 58ms 2.836 MiB
#18 Accepted 61ms 3.598 MiB
#19 Accepted 46ms 3.523 MiB
#20 Accepted 75ms 3.598 MiB
#21 Accepted 59ms 2.84 MiB

Code

//#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

#ifdef Mhmd_Bakr
#include "mhmd_bakr.h"
#endif

//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;

// Author: Mohamed Bakr

#define ll long long
#define INT int32_t
#define int ll
#define vct vector
//#define int128 number<cpp_int_backend<128, 128, unsigned_magnitude, unchecked, void>>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sum(v) accumulate(all(v), 0LL)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define ln "\n"
#define lln cout<<ln
#define umap unordered_map
#define yes cout<<"YES"<<ln
#define no cout<<"NO"<<ln
#define precision(x,y) fixed<<setprecision(y)<<x
#define fastio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define PI 3.141592653589793238462643383279502884197
#define toDeg(theta) theta*(180.0/PI)
#define toRad(theta) theta*(PI/180.0)

template <typename T, typename C>
istream& operator>>(istream& istream, pair<T, C>& pi) { cin >> pi.first >> pi.second; return istream; }
template <typename T, typename C>
ostream& operator<<(ostream& ostream, pair<T, C>& pi) { cout << pi.first << " " << pi.second;  return ostream; }
template <typename T>
istream& operator>>(istream& istream, vector<T>& v) { for (T& it : v) cin >> it; return istream; }
template <typename T>
ostream& operator<<(ostream& ostream, vector<T>& v) { for (T it : v) cout << it << " "; return ostream; }
template <typename T>
ostream& operator<<(ostream& ostream, set<T>& v) { for (T it : v) cout << it << " "; return ostream; }


bool prime(int num) { if (num <= 1) return false; int ch = 2; while (ch * ch <= num) { if (!(num % ch)) return false; ch++; }return true; }
int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); }
int nPr(int n, int r) { int val = 1; for (int i = n - r + 1; i <= n; i++) val *= i; return val; }
int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); }
int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
int biPow(int x, int y) { int val = 1; while (y) { if (y % 2) { val *= x; y--; } else { x *= x; y /= 2; } }return val; }
int modPow(int x, int y, int m) { int val = 1; x %= m; while (y) { if (y % 2) { val *= x; val %= m; y--; } else { x *= x; x %= m; y /= 2; } }return val % m; }
int sumRng(int l, int r) { return (r - l + 1) * (l + r) / 2; }

string mv[8] = { "U", "R", "D", "L", "UR","UL","DR","DL" };
int xd[9] = { -1, 0, 1,  0, -1, -1, 1,  1, 0 };
int yd[9] = { 0, 1, 0, -1,  1, -1, 1, -1, 0 };
int xk[9] = { -1, 1, 2, -2, -1, 1, 2, -2, 0 };
int yk[9] = { 2, -2, 1, -1, -2, 2, -1, 1, 0 };

const int N = 200005;
const int mod = 1e9 + 7;
int facts[N];
void ini() {
	facts[0] = 1;
	for (int i = 1; i < N; i++)
		facts[i] = (i * facts[i - 1]) % mod;
}

void answer(int test)
{
	int n, k; cin >> n >> k;
	vct<int>a(n);
	int o = 0, z = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (a[i]) o++;
		else z++;
	}
	if (k > o) cout << "0\n";
	else {
		// fact(n) / (fact(r) * fact(n - r))
		auto C = [&](int l, int r) {
			return (facts[l] * modPow((facts[r] * facts[l - r]) % mod, mod - 2, mod) % mod) % mod;
			};
		int ans = 0;
		int tmpl = 0;
		for (int j = 1; j <= z; j++) {
			tmpl = (tmpl + C(z, j)) % mod;
		}
		int tmpr = 0;
		for (int j = 0; j <= z; j++) {
			tmpr = (tmpr + C(z, j)) % mod;
		}
		for (int i = k; i <= o; i++){
			int rem = o - i;
			if (rem == 0) ans = (ans + tmpl) % mod;
			else{
				if (z) ans = (ans + (C(o, rem) * tmpr) % mod) % mod;
				else ans = (ans + C(o, rem)) % mod;
			}
		}
		cout << ans << ln;
	}
}
bool multiTests = true;
INT main()
{
#ifdef Mhmd_Bakr
	//Contest : -
	//Problem : -
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	fastio;
	ini();
	int tests = 1;
	if (multiTests) cin >> tests;
	for (int test = 1; test <= tests; test++)
	{
		//cout << "Case #" << test << ": ";
		answer(test);
	}
#ifdef Mhmd_Bakr
	End();
#endif
}

Information

Submit By
Type
Submission
Problem
P1094 Number of ways (Hard version)
Contest
Brain Booster #5
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-05 17:21:26
Judged At
2024-11-11 02:58:18
Judged By
Score
100
Total Time
75ms
Peak Memory
3.598 MiB