跳到正文

题解

HBCPC2023 vp记录

kaederei7约 17 分钟
原文
文章信息与版本

文章信息

发布
2024年11月9日
收录
2026年11月20日
更新
2026年11月20日
篇幅
约 17 分钟
获赞
1

版本

  1. v1当前

gym: 这里

和 厦马✌ 一起打的,哥们还是有实力的。

总结:开场签到结论题注意力涣散+160,中场偶遇小马强如怪物,拼尽全力无法战胜,5题647喜提cu。

M. Different Billing

第一道签

根据题意,显然 a,b,ca, b , c 满足下列关系:

{a+b+c=x1000b+2500c=y\left\{\begin{matrix}a+b+c=x \\1000b+2500c=y\end{matrix}\right.

注意到 x≤106x \le 10^6,所以枚举 bb 或 cc,算出一组合法答案即可,否则无解。

当时写的时候还想着二分之类的,但是感觉不太好写就直接写枚举了。

C++
// 这里的x,y,z是变量,a,b才是已知量
void solve(){
    int a, b;
    cin >> a >> b;
    int x, y, z;
    for(int i = 0; i <= a; i++) {
        z = i;
        int res = b - i * 2500;
        if(res % 1000 == 0) {
            y = res / 1000;
            x = a - z - y;
            if(y * 1000 + z * 2500 == b && x >= 0 && y >= 0 && z >= 0) {
                cout << x << " " << y << " " << z << "\n";
                return;
            }
        } 
    }
    cout << -1 << "\n";
}

C. Different Billing

乙级战犯时刻

开场 StanleyEx 看错题了,果断 nm2\frac{nm}{2} 喜提wa2。

然后在改成ll再wa2的时候我们才意识到看错了。

然后我们就在不断逼近正解并注意力涣散地交了 6 发错误解(一开始猜max(n, m), 后面加了特判1,后面意识到不用全覆盖到每一列) 之后靠画图猜到了正解 ⌈n+m⌉2\frac{\lceil n+m \rceil }{2}。

原理(来自官方题解):注意到任何一次变色后,黑格的总周长不会变大,那么要充满 n×mn×m 格子,至少需要 ⌈2(n+m)4⌉=⌈n+m2⌉⌈\frac{2(n+m)}{4} ⌉=⌈\frac{n+m}{2} ⌉ 个初始黑格。

H. Binary Craziness

看起来很吓人但是没那么难的签到。

容易发现只有 degdeg 不同的一对二元组才会对结果有贡献。因为之前vp了JXCPC2023,所以当时直觉认为不同的 degudeg_u 应该不会很多,使用 map 统计 cntcnt 之后合并处理即可。总复杂度O(m)O(m)。

C++
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m; cin >> n >> m;
    vector<int> deg(n + 1);
    map<int, i64> mp;
    for(int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        deg[u]++; deg[v]++;
    }
    i64 ans = 0;
    for(int i = 1; i <= n; i++) {
        mp[deg[i]]++;
    }
    int k = 0;
    vector<i64> a(n + 1);
    for(auto [num, cnt] : mp) {
        a[k++] = num;
    }
    for(int i = 0; i < k; i++) {
        for(int j = i + 1; j < k; j++) {
            ans = (ans + 1LL * mp[a[i]] * mp[a[j]] % P * (a[i] & a[j]) % P * (a[i] | a[j]) % P * (a[i] ^ a[j]) % P) % P;
        }
    }
    cout << ans << "\n";
    return 0;
}

F. Inverse Manacher

一道不需要会马拉车也能做的题。

首先,只需要关注字符串中 | 的部分。

如果回文长度为1,说明两边字符不一样,否则两边字符相同,输出上一个结果即可。

StanleyEx 的解法:

C++
#include <iostream>

using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int n = 0,t = 0,cur = 1;
	string res = "";
	cin >> n;
	n = n * 2 + 2;
	cin >> t;
	cin >> t;
	for(int i = 3;i <= n;++i) {
		cin >> t;
		if(i % 2 == 0) {
			if(cur == 1) {
				res += 'a';
			}
			else {
				res += 'b';
			}
			if(t == 1) {
				cur = -cur;
			}
		}
	}
	cout << res;
	return 0;
}

K. Dice Game

因为当时自己在开 j 题,就把这个丢给 StanleyEx 了。

最后还真靠打表找出式子来了

C++
#include <iostream>

using namespace std;

typedef long long i64;

#define Pow 998244351
#define Mod 998244353

i64 pow(i64 x,i64 y,i64 mod) {
	i64 res = 1,base = x;
	while(y) {
		if(y % 2 == 1) {
			res *= base,res %= mod;
		}
		base *= base;
		base %= mod;
		y = y >> 1;
	}
	return res % mod;
}

int main() {
	ios::sync_with_stdio(false);
	int n = 0,m = 0;
	cin >> n >> m;
    // 大胆猜想时刻 
	//for(int t = -4;t <= 4;++t) {
	//cout << "T=" << t << "\t\t";
	
	cout << "1 ";
	for(int x = 2;x < m;++x) {
		i64 p,q;
		p = pow(m - x,n,Mod);
		q = pow(m - 1,n,Mod);
		q = pow(q,Pow,Mod);
		cout << (p * q) % Mod << " ";
	}
	cout << " 0\n";
	
	//}
	return 0;
}

J. Expansion

本人的甲级战犯时刻,三个小时没调出来一道铜牌题

其实思路是很好想的,就是在能往前的时候尽量往前走,如果资源不过往前的话就在之前收益最高的地方停下来,相应地计算需要增加的天数即可。

一开始想到这个的时候还想着用单调栈来着,然后发现完全不需要。 (后面看jls录播发现他也这么想了)

不过当时没有注意到第一天的时候其实是到达起点1的位置而不是从1出发,所以写唐了。

C++
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u32 = unsigned;
using PII = pair<int, int>;
using ld = long double;
constexpr int P = 998244353;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n; cin >> n;
    vector<i64> pre(n + 1);
    for(int i = 1; i <= n; i++) {
        int x; cin >> x;
        pre[i] = pre[i - 1] + x;
    }

    if(pre[n] < 0) {
        cout << -1 << "\n";
        return 0;
    }
    i64 ti = n, mx = pre[1], res = 0;
    for(int i = 1; i <= n; i++) {
        if(res + pre[i] < 0) {
            if(mx <= 0) {
                cout << -1 << "\n";
                return 0;
            }
            i64 t = (abs(res + pre[i]) + mx - 1) / mx;
            ti += t;
            res += t * mx;
        }
        res += pre[i];
        mx = max(mx, pre[i]);
    }
    cout << ti << "\n";
    return 0;
}

I. Step

vp时试图开出来的题,可惜不会正解

总觉得上一次写exgcd都已经是高中了

我们需要求一个数 tt ,满足对所有的 pip_i 都有 t(t+1)2∣pi\frac{t(t+1)}{2}|p_i

所以取 pp 数组的 LCM,不妨记为 MM,则有 t(t+1)∣2Mt(t+1)|2M

因为tt 和 t+1t+1 是互质的,所以 MM 中的质因子只会属于 tt 和 t+1t+1 中的其中一种,因为 MM 不大,所以不会有太多质因子,可以直接枚举

然后令 ax=t,by=t+1ax=t, by=t+1,有 a(−x)+by=1a(-x)+by=1,用exgcd求x最小的可行解即可,最终答案 tt 即为枚举过程中最小的 tt 值

C++
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u32 = unsigned;
using PII = pair<int, int>;
using ld = long double;
constexpr int P = 998244353;
using i128 = __int128;
i64 lcm (i64 a, i64 b) {
    return a / __gcd(a, b) * b;
}
// 套了jls的模板,事实上因为pi不大,直接质因数分解也是可以的
i64 mul(i64 a, i64 b, i64 m) {
    return static_cast<__int128>(a) * b % m;
}
i64 power(i64 a, i64 b, i64 m) {
    i64 res = 1 % m;
    for (; b; b >>= 1, a = mul(a, a, m))
        if (b & 1)
            res = mul(res, a, m);
    return res;
}
bool isprime(i64 n) {
    if (n < 2)
        return false;
    static constexpr i64 A[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
    int s = __builtin_ctzll(n - 1);
    i64 d = (n - 1) >> s;
    for (auto a : A) {
        if (a == n)
            return true;
        i64 x = power(a, d, n);
        if (x == 1 || x == n - 1)
            continue;
        bool ok = false;
        for (i64 i = 0; i < s - 1; ++i) {
            x = mul(x, x, n);
            if (x == n - 1) {
                ok = true;
                break;
            }
        }
        if (!ok)
            return false;
    }
    return true;
}
std::vector<i64> factorize(i64 n) {
    std::vector<i64> p;
    std::function<void(i64)> f = [&](i64 n) {
        if (n <= 10000) {
            for (i64 i = 2; i * i <= n; ++i)
                for (; n % i == 0; n /= i)
                    p.push_back(i);
            if (n > 1)
                p.push_back(n);
            return;
        }
        if (isprime(n)) {
            p.push_back(n);
            return;
        }
        auto g = [&](i64 x) {
            return (mul(x, x, n) + 1) % n;
        };
        i64 x0 = 2;
        while (true) {
            i64 x = x0;
            i64 y = x0;
            i64 d = 1;
            i64 power = 1, lam = 0;
            i64 v = 1;
            while (d == 1) {
                y = g(y);
                ++lam;
                v = mul(v, std::abs(x - y), n);
                if (lam % 127 == 0) {
                    d = __gcd(v, n);
                    v = 1;
                }
                if (power == lam) {
                    x = y;
                    power *= 2;
                    lam = 0;
                    d = __gcd(v, n);
                    v = 1;
                }
            }
            if (d != n) {
                f(d);
                f(n / d);
                return;
            }
            ++x0;
        }
    };
    f(n);
    std::sort(p.begin(), p.end());
    return p;
}
i64 Exgcd(i64 a, i64 b, i64 &x, i64 &y) {
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    i64 d = Exgcd(b, a % b, x, y);
    i64 t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n; cin >> n;
    i64 lc = 1;
    for(int i = 0; i < n; ++i) {
        i64 x; cin >> x;
        lc = lcm(lc, x);
    }
    lc <<= 1;
    map<int, int> cnt;
    auto fac = factorize(lc);
    for(auto i : fac) {
        cnt[i]++;
    }
    vector<int> vt; 
    for(auto [x, y] : cnt) {
        i64 res = 1;
        while(y--) {
            res *= x;
        }
        vt.push_back(res);
    }
    i64 ans = 1e18;
    int k = vt.size();
    for(int s = 0; s < (1 << k); ++s) {
        i64 a = 1, b = 1;
        for(int i = 0; i < k; i++) {
            if((s >> i) & 1) a *= vt[i];
            else b *= vt[i];
        }
        i64 x, y;
        i64 d = Exgcd(a, b, x, y);
        x = (-x % b + b) % b;
        if(x) ans = min(ans, a * x);
    }
    cout << ans << "\n";
    return 0;
}
原文