Certainly! Below is the C++ translation of your Java ColoredBalls program. This C++ program performs the same operations:
- Precomputes Combinations (Binomial Coefficients): It calculates combinatorial values using dynamic programming and stores them in a 2D array
comb.
- Reads Input:
K: Number of color groups.
color[]: An array representing the number of balls in each color group.
- Calculates the Result:
- It computes the number of ways to arrange the colored balls based on the combinatorial values.
- Outputs the Result:
- Prints the final result modulo (10^9 + 7).
Here is the complete C++ code:
#include <bits/stdc++.h>
using namespace std;
// Define constants
const int MAXN = 1010;
const long long MOD = 1000000007;
// Declare a 2D array to store combination values
long long comb[MAXN][MAXN];
int main() {
// Optimize I/O operations
ios::sync_with_stdio(false);
cin.tie(0);
// Initialize combinations
comb[0][0] = 1;
for(int i = 1; i < MAXN; i++) {
comb[i][0] = 1;
for(int j = 1; j <= i; j++) {
comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) % MOD;
}
}
int K;
cin >> K; // Read the number of color groups
vector<int> color(K);
for(int &x : color) {
cin >> x; // Read the number of balls in each color group
}
long long res = 1;
int total = 0;
for(int i = 0; i < K; i++) {
// Calculate combination: C(total + color[i] - 1, color[i] - 1)
res = (res * comb[total + color[i] - 1][color[i] - 1]) % MOD;
total += color[i]; // Update the total number of balls processed
}
cout << res; // Output the result
return 0;
}
Explanation of the C++ Code:
-
Headers and Namespace:
#include <bits/stdc++.h> includes all standard C++ libraries.
using namespace std; allows us to use the standard namespace without prefixing std::.
-
Constants:
MAXN is set to 1010, similar to the Java program.
MOD is defined as (10^9 + 7) to perform modulo operations.
-
Combination Array (comb):
- A 2D array
comb is used to store precomputed binomial coefficients.
- It is initialized with
comb[0][0] = 1.
- The nested loops compute
C(i, j) = C(i-1, j) + C(i-1, j-1) modulo MOD.
-
Input Handling:
- The program reads an integer
K representing the number of color groups.
- It then reads
K integers into the color vector, each representing the number of balls in that color group.
-
Calculating the Result (res):
- Initializes
res to 1 and total to 0.
- For each color group, it updates
res by multiplying it with the combination C(total + color[i] - 1, color[i] - 1) modulo MOD.
- It then adds the number of balls in the current color group to
total.
-
Output:
- Finally, the result
res is printed.
How to Compile and Run:
-
Save the Code:
- Save the above code in a file named, for example,
ColoredBalls.cpp.
-
Compile:
-
Run:
- Execute the compiled program:
./ColoredBalls
- Provide the input as per the program's requirements.
Example:
Input:
3
2 3 1
Output:
10
This output represents the number of ways to arrange the colored balls given the input constraints, computed modulo (10^9 + 7).
Feel free to ask if you have any questions or need further assistance!