An event is being held for n performers. When a performer arrives, they are
assigned to one of the x stages. All performers assigned to the same stage will perform together as a band, though some stages might remain
empty.
After all performances are completed, the jury will award each band a score in the range [1, y].
Return the total number of possible ways the event can take place.
Since the answer may be very large, return it modulo10^9 + 7.
Note that two events are considered to have been held differently if
either of the following conditions is satisfied:
Input: n =1, x =2, y =3Output: 6Explanation:
* There are 2 ways to assign a stage to the performer.* The jury can award a score of either 1,2, or 3 to the only band.
Each performer can be assigned to any of the x stages, so there are x^n ways to assign performers. After assignment, each non-empty stage (band) gets a score from 1 to y. The number of non-empty bands can range from 1 to min(n, x). For each possible number of non-empty bands k, we count:
Ways to partition n performers into k non-empty bands: S(n, k) (Stirling numbers of the second kind).
Ways to assign these bands to x stages: P(x, k) = x! / (x-k)!.
Ways to assign scores to bands: y^k.
Sum over all possible k.
classSolution {
publicintnumberOfWays(int n, int x, int y) {
int MOD = 1000000007;
int maxv = Math.max(n, x);
long[] fact =newlong[maxv+2];
long[] invf =newlong[maxv+2];
fact[0]= 1;
for (int i = 1; i < fact.length; i++) fact[i]= fact[i-1]* i % MOD;
invf[maxv+1]= powmod(fact[maxv+1], MOD-2, MOD);
for (int i = maxv; i >= 0; i--) invf[i]= invf[i+1]* (i+1) % MOD;
java.util.function.BiFunction<Integer, Integer, Long> perm = (a, b) -> b > a ? 0L : fact[a]* invf[a-b]% MOD;
long[][] S =newlong[n+1][x+1];
S[0][0]= 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= x; j++) {
S[i][j]= (S[i-1][j-1]+ S[i-1][j]* j) % MOD;
}
}
long ans = 0;
for (int k = 1; k <= Math.min(n, x); k++) {
long ways = S[n][k]* perm.apply(x, k) % MOD;
long score = powmod(y, k, MOD);
ans = (ans + ways * score % MOD) % MOD;
}
return (int)ans;
}
longpowmod(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
}
classSolution {
funnumberOfWays(n: Int, x: Int, y: Int): Int {
val MOD = 1_000_000_007
val maxv = maxOf(n, x)
val fact = LongArray(maxv+2) { 1L }
val invf = LongArray(maxv+2) { 1L }
for (i in1 until fact.size) fact[i] = fact[i-1] * i % MOD
invf[maxv+1] = powmod(fact[maxv+1], MOD-2, MOD)
for (i in maxv downTo 0) invf[i] = invf[i+1] * (i+1) % MOD
funperm(a: Int, b: Int) = if (b > a) 0Lelse fact[a] * invf[a-b] % MOD
val S = Array(n+1) { LongArray(x+1) }
S[0][0] = 1Lfor (i in1..n) for (j in1..x) S[i][j] = (S[i-1][j-1] + S[i-1][j] * j) % MOD
var ans = 0Lfor (k in1..minOf(n, x)) {
val ways = S[n][k] * perm(x, k) % MOD
val score = powmod(y.toLong(), k.toLong(), MOD)
ans = (ans + ways * score % MOD) % MOD
}
return ans.toInt()
}
funpowmod(a: Long, b: Long, mod: Long): Long {
var res = 1L; var aa = a; var bb = b
while (bb > 0) {
if (bb and 1L==1L) res = res * aa % mod
aa = aa * aa % mod
bb = bb shr 1 }
return res
}
}
classSolution:
defnumberOfWays(self, n: int, x: int, y: int) -> int:
MOD =10**9+7 maxv = max(n, x)
fact = [1] * (maxv +2)
invf = [1] * (maxv +2)
for i in range(1, len(fact)):
fact[i] = fact[i-1] * i % MOD
invf[-1] = pow(fact[-1], MOD-2, MOD)
for i in range(len(fact)-2, -1, -1):
invf[i] = invf[i+1] * (i+1) % MOD
defperm(a, b):
if b > a: return0return fact[a] * invf[a-b] % MOD
S = [[0] * (x+1) for _ in range(n+1)]
S[0][0] =1for i in range(1, n+1):
for j in range(1, x+1):
S[i][j] = (S[i-1][j-1] + S[i-1][j] * j) % MOD
ans =0for k in range(1, min(n, x)+1):
ways = S[n][k] * perm(x, k) % MOD
score = pow(y, k, MOD)
ans = (ans + ways * score % MOD) % MOD
return ans
impl Solution {
pubfnnumber_of_ways(n: i32, x: i32, y: i32) -> i32 {
let n = n asusize;
let x = x asusize;
let y = y asi64;
let maxv = n.max(x);
letmut fact =vec![1i64; maxv+2];
letmut invf =vec![1i64; maxv+2];
letMOD=1_000_000_007i64;
for i in1..fact.len() {
fact[i] = fact[i-1] * i asi64%MOD;
}
invf[maxv+1] = powmod(fact[maxv+1], MOD-2, MOD);
for i in (0..=maxv).rev() {
invf[i] = invf[i+1] * (i asi64+1) %MOD;
}
let perm =|a: usize, b: usize| -> i64 {
if b > a { 0 } else { fact[a] * invf[a-b] %MOD }
};
letmut S =vec![vec![0i64; x+1]; n+1];
S[0][0] =1;
for i in1..=n {
for j in1..=x {
S[i][j] = (S[i-1][j-1] + S[i-1][j] * j asi64) %MOD;
}
}
letmut ans =0i64;
for k in1..=n.min(x) {
let ways = S[n][k] * perm(x, k) %MOD;
let score = powmod(y, k asi64, MOD);
ans = (ans + ways * score %MOD) %MOD;
}
ans asi32 }
}
fnpowmod(mut a: i64, mut b: i64, m: i64) -> i64 {
letmut res =1;
while b >0 {
if b &1==1 {
res = res * a % m;
}
a = a * a % m;
b >>=1;
}
res
}