Educational Codeforces Round 71 Div.2 Problem-Solving Report (CF1207)
D - Numbers of Permutations
A problem that takes very little thinking.
First, compute it indirectly. The initial answer is \(n!\), and then we consider removing, by inclusion–exclusion, the arrangements that follow the sorting rule. It turns out we can consider them separately, in inclusion–exclusion order: first consider the ones counted on only the first dimension and only the second dimension, then add back the ones that consider both dimensions.
The method for the first and the second dimension is very obvious: after sorting, just use permutations of a multiset, multiplying things together inside each range of equal values. If there are \(k\) consecutive runs, then this part of the answer is \(\prod_{i = 1}^l len_k!\).
The case with both dimensions is a bit more complicated. First, if after sorting with the first dimension taking priority we find the sequence cannot guarantee that the second dimension is non-decreasing, then the case with both dimensions does not exist; otherwise, we scan the first dimension to split it into runs of the first dimension, then within each run count how many there are of each number, and the third dimension is then the answer for these “second-dimension runs within which the first dimension is guaranteed equal”.
// CF1207D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5 + 200, mod = 998244353;
pair<int, int> nodes[MAX_N];
int fac[MAX_N], n;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &nodes[i].first, &nodes[i].second);
for (int i = fac[0] = 1; i <= n; i++)
fac[i] = 1LL * fac[i - 1] * i % mod;
int ans[4] = {fac[n], 1, 1, 1};
for (int c = 1; c <= 2; c++)
{
sort(nodes + 1, nodes + 1 + n);
int l = 1, r;
while (l <= n)
{
r = l + 1;
while (r <= n && nodes[r].first == nodes[l].first)
r++;
ans[c] = 1LL * ans[c] * fac[r - l] % mod;
l = r;
}
ans[0] = (1LL * ans[0] + 1LL * (mod - ans[c])) % mod;
for (int i = 1; i <= n; i++)
swap(nodes[i].first, nodes[i].second);
}
int l = 1, r;
sort(nodes + 1, nodes + 1 + n);
while (l <= n)
{
r = l + 1;
while (r <= n && nodes[r].first == nodes[l].first)
r++;
map<int, int> mp;
mp.clear();
for (int i = l; i < r; i++)
mp[nodes[i].second]++;
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
ans[3] = 1LL * ans[3] * fac[(*it).second] % mod;
l = r;
}
for (int i = 2; i <= n; i++)
if (nodes[i].second < nodes[i - 1].second)
ans[3] = 0;
ans[0] = (1LL * ans[0] + 1LL * ans[3]) % mod;
printf("%d", ans[0]);
return 0;
}
E - XOR Guessing
A stupid problem.
Consider splitting a 14-bit binary number into the first 7 bits and the last 7 bits. To get the two halves, make sure each query leaves the first seven bits or the last seven bits empty, then just put them together.
// CF1207E.cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
fflush(stdout);
cout << "? ";
for (int i = 1; i < 100; i++)
cout << i << " ";
cout << 100 << endl;
fflush(stdout);
int ans1, ans2;
cin >> ans1;
fflush(stdout);
cout << "? ";
for (int i = 1; i < 100; i++)
cout << (i << 7) << " ";
cout << 12800 << endl;
fflush(stdout);
cin >> ans2;
int mask1 = 0;
for (int i = 0; i < 7; i++)
mask1 |= (1 << i);
int mask2 = mask1 << 7;
cout << "! " << ((ans1 & mask2) | (ans2 & mask1)) << endl;
return 0;
}
F - Remainder Problem
Truly, brute force works miracles. A great sqrt decomposition problem.
Simplifying the problem a bit, it is roughly:
- Support updates
- Given the block size \(x\), find the sum of the elements at in-block index \(y\) over all the blocks.
First, when the block size \(x\) is large, answering a query takes only \(O(\lfloor \frac{n}{x} \rfloor)\) time to get it done. If the block size is fairly small, say we set a threshold \(k\), then there can be at most on the order of \(k^2\) queries; dividing out one \(k\), that means each update only affects \(k\) queries. So on every update we precompute for each block length less than \(k\), and then when \(x\) is small we can answer in \(O(1)\).
Obviously, \(k = \sqrt{n}\).
// CF1207F.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5 + 200, MAX_K = 750;
int sum[MAX_K][MAX_K], ai[MAX_N];
int main()
{
int q;
scanf("%d", &q);
while (q--)
{
int opt, x, y;
scanf("%d%d%d", &opt, &x, &y);
if (opt == 1)
{
ai[x] += y;
for (int i = 1; i < MAX_K; i++)
sum[i][x % i] += y;
}
else if (x >= MAX_K)
{
int ans = 0;
for (int i = y; i <= 500000; i += x)
ans += ai[i];
printf("%d\n", ans);
}
else
printf("%d\n", sum[x][y]);
}
return 0;
}