Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grid Completion Solution #4786

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions content/5_Plat/PIE.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
"solutionMetadata": {
"kind": "internal"
}
},
{
"uniqueId": "cses-2429",
"name": "Grid Completion",
"url": "https://cses.fi/problemset/task/2429",
"source": "CSES",
"difficulty": "Hard",
"isStarred": false,
"tags": ["PIE"],
"solutionMetadata": {
"kind": "internal"
}
}
]
}
128 changes: 128 additions & 0 deletions solutions/platinum/cses-2429.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
---
id: cses-2429
source: CSES
title: Grid Completion
author: Alexis Tao
---

## Explanation

This problem is all about finding the number of valid ways to configure a grid with some special conditions. The key is to break down the grid into manageable pieces.

Save the effort of recalculating factorials and their modular inverses for the binomial coefficient by precomputing them using Fermat's Little Theorem. Then, track the character positions and count the rows and columns with A or B. Additionaly, define a helper function that returns the number of valid configurations based on how many rows we decide to fill with A’s and B’s. This function utilizes the inclusion-exclusion principle to adjust the counts. The outer loop iterates over all possible configurations and sums up the valid configurations.
Comment on lines +10 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good if you talked more about the intuition of arriving at the solution



## Implementation

lext88 marked this conversation as resolved.
Show resolved Hide resolved
**Time Complexity:** $\mathcal{O}(N^3)$

<LanguageSection>

<CPPSection>


```cpp
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

/** @return The modular inverse of x modulo MOD */
ll modInverse(ll x, ll MOD) {
lext88 marked this conversation as resolved.
Show resolved Hide resolved
ll res = 1;
ll b = MOD - 2;
while (b) {
if (b & 1) { res = (res * x) % MOD; }
x = (x * x) % MOD;
b >>= 1;
}
return res;
}

int main() {
int n;
cin >> n;

// Initialize arrays for column position of 'A' and column position of 'B' in each
// row
Comment on lines +47 to +48
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orphan looks weird, could you even out the lines?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unresolved?

vector<int> colA(n, -1), colB(n, -1), config_count(6, 0);
vector<bool> hasA(n, false), hasB(n, false);
vector<ll> fact(n + 1), inv_fact(n + 1);
string S;

// Precompute factorials
fact[0] = 1;
for (int i = 1; i <= n; i++) { fact[i] = (fact[i - 1] * i) % MOD; }

// Compute the modular inverse of the largest factorial then do it for all
// factorials in reverse order
inv_fact[n] = modInverse(fact[n], MOD);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we passing in MOD?

for (int i = n - 1; i >= 0; i--) {
inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
}

for (int i = 0; i < n; i++) {
cin >> S;
for (int j = 0; j < n; j++) {
if (S[j] == 'A') {
colA[i] = j;
hasA[j] = true;
}
if (S[j] == 'B') {
colB[i] = j;
hasB[j] = true;
}
}
}

// Count various configurations based on positions of 'A' and 'B'
for (int i = 0; i < n; i++) {
if (colA[i] == -1 && colB[i] == -1) { config_count[0]++; }
if (colA[i] == -1 && colB[i] != -1 && !hasA[colB[i]]) { config_count[1]++; }
if (colA[i] != -1 && colB[i] == -1 && !hasB[colA[i]]) { config_count[2]++; }
}

// Count rows and columns without 'A' or 'B'
for (int i = 0; i < n; i++) {
if (!hasA[i] && !hasB[i]) { config_count[3]++; }
if (!hasA[i]) { config_count[4]++; }
if (!hasB[i]) { config_count[5]++; }
}

/** @return The binomial coefficient C(x, y) modulo MOD */
auto choose = [&](int x, int y) -> ll {
if (y > x || y < 0) return 0LL;
return (fact[x] * inv_fact[y] % MOD) * inv_fact[x - y] % MOD;
};

/** @return The number of valid grid configurations */
auto f = [&](int i, int j, int k) -> ll {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you name this function something more readable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something like, valid_configs would be good

ll res = (choose(config_count[0], i) * choose(config_count[1], j) % MOD *
choose(config_count[2], k) % MOD * choose(config_count[3], i) % MOD);

// Multiply by the factorials of the number of rows and columns left
res = (res * fact[i] % MOD * fact[config_count[4] - i - j] % MOD *
fact[config_count[5] - i - k] % MOD);

// Apply inclusion-exclusion principle to adjust the result
if ((i + j + k) % 2 == 1) { res = (MOD - res); }

return res;
};

// Calculate the number of valid grid configurations
ll ans = 0;
for (int i = 0; i <= min(config_count[0], config_count[3]); i++) {
for (int j = 0; j <= config_count[1]; j++) {
for (int k = 0; k <= config_count[2]; k++) {
ans = (ans + f(i, j, k)) % MOD;
}
}
}

cout << ans << endl;
}
```
</CPPSection>
</LanguageSection>
Loading