3881. Direction Assignments with Exactly K Visible People
You are given three integers n, pos, and k.
There are n people standing in a line indexed from 0 to n - 1. Each person independently chooses a direction:
'L': visible only to people on their right
'R': visible only to people on their left
pos
- A person
i < pos is visible if and only if they choose 'L'.
- A person
i > pos is visible if and only if they choose 'R'.
Return the number of possible direction assignments such that the person at index pos sees exactly k people.
Since the answer may be large, return it modulo 10^9 + 7.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| using ll = long long; ll mod = 1e9 + 7; typedef vector<ll> vll; #define rep(i,m,n) for(ll i=m;i<n;i++) #define rrep(i,m,n) for(ll i=n;i>=m;i--) struct Combination { vll fac, inv; ll n, MOD;
ll modpow(ll n, ll x, ll MOD = mod) { if(!x) return 1; ll res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }
Combination(ll _n, ll MOD = mod): n(_n + 1), MOD(MOD) { inv = fac = vll(n,1); rep(i,1,n) fac[i] = fac[i-1] * i % MOD; inv[n - 1] = modpow(fac[n - 1], MOD - 2, MOD); rrep(i,1,n - 2) inv[i] = inv[i + 1] * (i + 1) % MOD; }
ll fact(ll n) {return fac[n];} ll nCr(ll n, ll r) { if(n < r or n < 0 or r < 0) return 0; return fac[n] * inv[r] % MOD * inv[n-r] % MOD; } };
Combination comb(1e5 + 1); class Solution { public: int countVisiblePeople(int n, int pos, int k) { ll res = 0; for(int left = 0; left <= min(k,pos); left++) { res = (res + comb.nCr(pos,left) * comb.nCr(n-pos-1, k - left)) % mod; } return res * 2 % mod; } };
|