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 11 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"
}
}
]
}
121 changes: 121 additions & 0 deletions solutions/platinum/cses-2429.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
id: cses-2429
source: CSES
title: Grid Completion
author: Alexis Tao
---

## 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 */
lext88 marked this conversation as resolved.
Show resolved Hide resolved
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), configCount(6, 0);
vector<bool> hasA(n, false), hasB(n, false);
vector<ll> fact(n + 1), invFact(n + 1);
lext88 marked this conversation as resolved.
Show resolved Hide resolved
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 first
invFact[n] = modInverse(fact[n], MOD);

// Compute modular inverses of all factorials in reverse order
lext88 marked this conversation as resolved.
Show resolved Hide resolved
for (int i = n - 1; i >= 0; i--) { invFact[i] = (invFact[i + 1] * (i + 1)) % MOD; }

// Read grid and fill arrays colA, colB, hasA, and hasB
lext88 marked this conversation as resolved.
Show resolved Hide resolved
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) { configCount[0]++; }
if (colA[i] == -1 && colB[i] != -1 && !hasA[colB[i]]) { configCount[1]++; }
if (colA[i] != -1 && colB[i] == -1 && !hasB[colA[i]]) { configCount[2]++; }
}

// Count rows and columns without 'A' or 'B'
for (int i = 0; i < n; i++) {
if (!hasA[i] && !hasB[i]) { configCount[3]++; }
if (!hasA[i]) { configCount[4]++; }
if (!hasB[i]) { configCount[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] * invFact[y] % MOD) * invFact[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(configCount[0], i) * choose(configCount[1], j) % MOD *
choose(configCount[2], k) % MOD * choose(configCount[3], i) % MOD);

// Multiply by the factorials of the number of rows and columns left
res = (res * fact[i] % MOD * fact[configCount[4] - i - j] % MOD *
fact[configCount[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(configCount[0], configCount[3]); i++) {
for (int j = 0; j <= configCount[1]; j++) {
for (int k = 0; k <= configCount[2]; k++) {
ans = (ans + f(i, j, k)) % MOD;
}
}
}

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