题面
解法
可以说是excrt(扩展中国剩余定理)的板子题了
- 首先,我们需要求出每一次打怪用的是那一把剑,这个显然可以直接用multiset解决
- 然后我们就要解决这样一个问题,求解下列形式的 n n n个同余方程:
- a t k [ i ] x ≡ h p [ i ] ( m o d h e a l [ i ] ) atk[i]x\equiv hp[i]\pmod {heal[i]} atk[i]x≡hp[i](modheal[i])
- 如何将 x x x的系数去掉?如果 h p [ i ] hp[i] hp[i]不能被 g c d ( a t k [ i ] , h e a l [ i ] ) gcd(atk[i],heal[i]) gcd(atk[i],heal[i])整除,那么一定为无解,否则先将这三者全部除去这个 g c d gcd gcd,然后右边就可以乘上 a t k [ i ] ′ atk[i]' atk[i]′关于 h e a l [ i ] ′ heal[i]' heal[i]′的逆元了
- 然后大致讲一下如何解决同余方程组中模数不保证两两互质的情况
- 假设我们当前做到第 i i i个同余方程,前 i − 1 i-1 i−1个方程的答案为 x x x,最小公倍数为 y y y
- 那么我们显然可以得到以下式子: x + k ⋅ a [ i ] ≡ b [ i ] ( m o d c [ i ] ) x+k·a[i]\equiv b[i]\pmod {c[i]} x+k⋅a[i]≡b[i](modc[i])
- 然后就发现 k k k可以通过扩展欧几里得求出,就可以得到新的 x x x了
- 时间复杂度: O ( T n log n ) O(Tn\log n) O(Tnlogn)
【注意事项】
- 正如题目所说,中间做乘法的时候注意可能会爆long long,所以要龟速乘,这个可以做到 O ( 1 ) O(1) O(1)
代码
#include <bits/stdc++.h>
#define LL long long
#define N 100010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
LL n, m, hp[N], atk[N], heal[N], sw[N], kx[N], ky[N];
LL gcd(LL x, LL y) {return y ? gcd(y, x % y) : x;}
LL mul(LL x, LL y, LL p) {
x %= p, y %= p;
LL z = x * (long double)y / p, ret = x * y - z * p;
if (ret >= p) ret -= p;
if (ret < 0) ret += p; return ret;
}
void exgcd(LL a, LL b, LL &x, LL &y) {
if (b == 0) return x = 1, y = 0, void();
LL r = a % b, q = a / b;
exgcd(b, r, y, x), y -= x * q;
}
void getatk() {
multiset <LL> s;
for (int i = 1; i <= m; i++) {
LL x; read(x);
s.insert(-x);
}
for (int i = 1; i <= n; i++) {
if (s.lower_bound(-hp[i]) != s.end()) {
atk[i] = -*s.lower_bound(-hp[i]);
s.erase(s.find(-atk[i]));
} else {
multiset <LL> ::iterator it = s.end(); it--;
atk[i] = -*it, s.erase(it);
}
s.insert(-sw[i]);
}
}
LL excrt() {
LL mx = 0;
for (int i = 1; i <= n; i++)
chkmax(mx, (LL)ceil((long double)hp[i] / atk[i]));
for (int i = 1; i <= n; i++) {
LL x = gcd(atk[i], heal[i]);
if (hp[i] % x != 0) return -1;
atk[i] /= x, heal[i] /= x, hp[i] /= x;
LL tx, ty; exgcd(atk[i], heal[i], tx, ty);
tx = (tx % heal[i] + heal[i]) % heal[i];
kx[i] = mul(hp[i], tx, heal[i]), ky[i] = heal[i];
}
LL ret = kx[1], lcm = ky[1];
for (int i = 2; i <= n; i++) {
LL tx, ty, A = ky[i], B = (kx[i] - ret + ky[i]) % ky[i], t = gcd(ky[i], lcm);
exgcd(lcm, ky[i], tx, ty);
tx = mul(tx, B / t, ky[i]);
LL tmp = lcm / t * ky[i];
ret = (ret + mul(tx, lcm, tmp)) % tmp, lcm = tmp;
}
if (ret < mx) {
LL res = (mx - ret) / lcm;
if ((mx - ret) % lcm != 0) res++;
ret += res * lcm;
}
for (int i = 1; i <= n; i++)
if (ret % ky[i] != kx[i]) return -1;
return ret;
}
int main() {
int T; read(T);
while (T--) {
read(n), read(m);
for (int i = 1; i <= n; i++) read(hp[i]);
for (int i = 1; i <= n; i++) read(heal[i]);
for (int i = 1; i <= n; i++) read(sw[i]);
getatk(); cout << excrt() << "\n";
}
return 0;
}