[Fortuna OJ] Jan 9 – Group A Problem-Solving Report
Feels like there is still a lot of stuff I can’t remember, gg.
A - Collecting History Homework
Idiotic 2D partial order, cracked it 8 minutes into the contest (why weren’t there free-points problems like this over the summer?)
// A.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 200;
int n, m, k, nodes[MAX_N], upper;
vector<int> mp;
struct point
{
int x, y, c;
bool operator<(const point &rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && c > rhs.c); }
} pts[MAX_N];
int ripe(int x) { return lower_bound(mp.begin(), mp.end(), x) - mp.begin() + 1; }
inline int lowbit(int x) { return x & (-x); }
void update(int x, int d)
{
for (; x <= upper; x += lowbit(x))
nodes[x] = max(nodes[x], d);
}
int query(int x, int ret = 0)
{
for (; x; x -= lowbit(x))
ret = max(ret, nodes[x]);
return ret;
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++)
{
scanf("%d%d%d", &pts[i].x, &pts[i].y, &pts[i].c);
mp.push_back(pts[i].x), mp.push_back(pts[i].y);
}
pts[++k] = point{n - 1, m - 1, -1}, mp.push_back(n - 1), mp.push_back(m - 1);
sort(mp.begin(), mp.end()), mp.erase(unique(mp.begin(), mp.end()), mp.end());
upper = mp.size();
for (int i = 1; i <= k; i++)
pts[i].x = ripe(pts[i].x), pts[i].y = ripe(pts[i].y);
sort(pts + 1, pts + 1 + k);
int ans = 0;
for (int i = 1; i <= k; i++)
{
int dp = query(pts[i].y) + pts[i].c;
if (pts[i].c == -1)
ans = ++dp;
update(pts[i].y, dp);
}
printf("%d\n", ans);
return 0;
}
B - Handing Out Bonuses
After some observation, the answer turns out to be very obvious: \(n + m \choose m\). Looking at the moduli, \(60\%\) can be done with Lucas, and then I got stuck (purely because I couldn’t remember how to write CRT).
First, the \(80\%\) is easy to write: just compute with Lucas under the two moduli, then merge with CRT. Actually the intended solution is pretty much the same, Lucas several times and then merge. But the intended solution has one special thing: there exist \(c_i \geq 1\). So you can’t compute directly with multiplicative inverses. However, we notice that a multiplicative inverse exists if and only if the two numbers are coprime. So when computing the inverse we can pull \(p_i\) out, and let the coprime part find its inverse with exgcd.
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 200;
int n, m, p, primes[MAX_N], cnt[MAX_N], ptot, cmod, pic[MAX_N], ai[MAX_N];
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, x, y), t = x;
x = y, y = t - (a / b) * y;
return d;
}
int getInv(int a)
{
int x, y;
exgcd(a, cmod, x, y);
return (x + cmod) % cmod;
}
int quick_pow(int bas, int tim)
{
if (tim < 0)
return quick_pow(getInv(bas), -tim);
int ret = 1;
while (tim)
{
if (tim & 1)
ret = 1LL * ret * bas % cmod;
bas = 1LL * bas * bas % cmod;
tim >>= 1;
}
return ret;
}
int fac(int x, int mod)
{
int ret = 1;
for (int i = 1; i <= x; i++)
if (i % mod != 0)
ret = 1LL * ret * i % cmod;
return ret;
}
int fac(int x, int mod, int &tot)
{
// lucas;
if (x < mod)
return fac(x, mod);
int seg = x / cmod, rem = x % cmod;
int res = 1LL * quick_pow(fac(cmod - 1, mod), seg) * fac(rem, mod) % cmod;
tot += x / mod;
res = 1LL * res * fac(x / mod, mod, tot) % cmod;
return res;
}
int binomial(int n_, int k_, int mod)
{
int a = 0, b = 0, c = 0, res = 1;
res = 1LL * res * fac(n_, mod, a) % cmod * getInv(fac(k_, mod, b)) % cmod * getInv(fac(n_ - k_, mod, c)) % cmod;
return 1LL * res * quick_pow(mod, a - (b + c)) % cmod;
}
void factorize(int tmp)
{
for (int i = 2; 1LL * i * i <= tmp; i++)
if (tmp % i == 0)
{
primes[++ptot] = i;
while (tmp % i == 0)
cnt[ptot]++, tmp /= i;
}
if (tmp > 1)
primes[++ptot] = tmp, cnt[ptot] = 1;
}
int main()
{
scanf("%d%d%d", &n, &m, &p), n += m;
factorize(p);
for (int i = 1; i <= ptot; i++)
{
cmod = 0x7fffffff, cmod = pic[i] = quick_pow(primes[i], cnt[i]);
ai[i] = binomial(n, m, primes[i]);
}
cmod = 0x7fffffff;
// begins to merge;
for (int i = 2; i <= ptot; i++)
{
int x, y;
exgcd(pic[i - 1], pic[i], x, y);
pic[i] *= pic[i - 1];
ai[i] = ((1LL * x * (ai[i] - ai[i - 1]) * pic[i - 1] % pic[i] + ai[i - 1]) % pic[i] + pic[i]) % pic[i];
}
printf("%d\n", ai[ptot]);
return 0;
}
C - A Big Freebie
A big nasty problem
You can tell it’s digit DP, but I just couldn’t get it written at all. The answer must be:
\[ \text{ans} = m - \frac{\text{count with reversal} \leq m \text{} - \text{count equal to own reversal}}{2} \]
So we can consider the state \(dp[i][0/1][0/1]\), meaning, up to digit \(i\), whether it equals \(m\) and whether it equals the reversal of \(m\). Then the count of those less than \(m\) is clearly \(dp[len][0][0]+dp[len][1][0]\). For the count of numbers equal to their own reversal, no doubt we only need the first half to be less, that is, \(dp[len >> 1][0][0] + {}\)
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1010, mod = 1e9 + 7;
int n, len, m, digits[MAX_N * MAX_N], dp[MAX_N * MAX_N][2][2];
char str[MAX_N * MAX_N];
int quick_pow(int bas, int tim)
{
int ret = 1;
while (tim)
{
if (tim & 1)
ret = 1LL * ret * bas % mod;
bas = 1LL * bas * bas % mod;
tim >>= 1;
}
return ret;
}
const int inv2 = quick_pow(2, mod - 2);
int main()
{
scanf("%d%s", &n, str + 1), len = n * n;
for (int i = 1; i <= len; i++)
digits[i] = str[i] - '0', m = (1LL * m * 10 % mod + digits[i]) % mod;
int last_digit;
dp[0][1][0] = 1;
for (int i = 0; i <= len - 1; i++)
for (int bit = 0; bit <= 1; bit++)
{
last_digit = bit ? digits[i + 1] : 9;
for (int k = 0; k <= last_digit; k++)
{
dp[i + 1][bit && (k == last_digit)][(k <= digits[len - i]) ? 0 : 1] =
(dp[i + 1][bit && (k == last_digit)][(k <= digits[len - i]) ? 0 : 1] + dp[i][bit][0]) % mod;
dp[i + 1][bit && (k == last_digit)][(k < digits[len - i]) ? 0 : 1] =
(dp[i + 1][bit && (k == last_digit)][(k < digits[len - i]) ? 0 : 1] + dp[i][bit][1]) % mod;
}
}
int firstTerm = (0LL + dp[len][0][0] + dp[len][1][0]) % mod;
int secondTerm = (0LL + dp[len >> 1][0][0] + dp[len >> 1][0][1] + dp[len >> 1][1][0]) % mod;
int ans = (0LL + m + mod - 1LL * (firstTerm + mod - secondTerm) % mod * inv2 % mod) % mod;
printf("%d\n", ans);
return 0;
}