NOI2018 屠龙勇士

作者:

在

,

$$ \text{首先考虑}a_i\le p_i\text{的情况} \\ \text{假设攻击第}i\text{条龙时的攻击为}A_i \\ \text{那么对于第}i\text{条龙,就是求一个}x\text{,满足} \\ x\times A_i-yp_i=a_i \\ \text{即}A_ix=a_i\ \left( mod\ p_i \right) \\ \text{因为}a_i\le \text{p}_i\text{,所以不会出现攻击次数为负的情况。} \\ \text{所以题目要求等价于解方程组} \\ \begin{cases} A_1x\equiv a_1\ \ \left( mod\ p_1 \right)\\ A_2x\equiv a_2\ \ \left( mod\ p_2 \right)\\ …\\ A_nx\equiv a_n\ \ \left( mod\ p_n \right)\\ \end{cases} \\ \text{然而}exCRT\text{只能处理}A_i=\text{1的情况}.. \\ \text{所以需要转变一下} \\ \text{拿出来第}i\text{个方程} \\ A_ix+p_iy=a_i \\ \text{我们现在要求一个}x. \\ \text{我们可以先求出这个方程的一组解}\left( x_{i}^{‘},y_{i}^{‘} \right) \\ \text{那么则有}x\equiv x_{i}^{‘}\left( mod\ \frac{p_i}{gcd\left( A_i,p_i \right)} \right) \\ \text{所以原方程组变为} \\ \begin{cases} x\equiv x_{1}^{‘}\left( mod\ \frac{p_1}{gcd\left( A_1,p_1 \right)} \right)\\ x\equiv x_{1}^{‘}\left( mod\ \frac{p_2}{gcd\left( A_2,p_2 \right)} \right)\\ …\\ x\equiv x_{n}^{‘}\left( mod\ \frac{p_n}{gcd\left( A_n,p_n \right)} \right)\\ \end{cases} \\ \text{这个方程组可以使用}exCRT\text{求解。} \\ \text{考虑一下}a_i>p_i\text{的情况。} \\ \text{这个时候满足}p_i=1 \\ \text{那么我们只需要求出把每条龙血砍到非正的次数,取个最大值即可。} \\ $$

#include <iostream>
#include <algorithm>
#include <set>
#include <utility>
#include <assert.h>
#include <functional>
using int_t = long long int;
using pair_t = std::pair<int_t, int_t>;
using std::cin;
using std::cout;
using std::endl;

const int_t LARGE = 100000;

pair_t exgcd(int_t a, int_t b)
{
    if (b == 0)
        return pair_t(1, 0);
    auto temp = exgcd(b, a % b);
    int_t x = temp.first;
    int_t y = temp.second;
    return pair_t(y, x - a / b * y);
}

int_t gcd(int_t a, int_t b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
//求x模p意义下的逆元
int_t inv(int_t x, int_t p)
{
    x = (x % p + p) % p;
    if (gcd(x, p) != 1)
        return -1;
    int_t inv = exgcd(x, p).first;
    inv = (inv % p + p) % p;
    return inv;
}
int_t mul(int_t a, int_t b, int_t p)
{
    int_t result = 0;
    while (b)
    {
        if (b & 1)
        {
            result = (result + a) % p;
        }
        a = (a + a) % p;
        b >>= 1;
    }
    return result;
}
//求方程Ax+By=C的最小非负解
int_t solve(int_t A, int_t B, int_t C)
{
    int_t gcd = ::gcd(A, B);
    if (C % gcd)
        return -1;
    auto temp = exgcd(A, B);
    int_t x = temp.first;
    x = mul(x, C / gcd, B / gcd);
    x = (x % (B / gcd) + B / gcd) % (B / gcd);
    return x;
}
struct Processor
{
    int_t n, m;
    //系数
    int_t a[LARGE + 1];
    //模数
    int_t rec[LARGE + 1];
    int_t reward[LARGE + 1];
    //攻击
    int_t attack[LARGE + 1];
    //模数
    int_t mod[LARGE + 1];
    //系数
    int_t c[LARGE + 1];
    // int_t attacks[LARGE + 1];
    std::multiset<int_t, std::greater<int_t>> swords;
    int_t process()
    {
        cin >> n >> m;
        for (int_t i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        bool flag = false;
        for (int_t i = 1; i <= n; i++)
        {
            cin >> rec[i];
            if (rec[i] != 1)
                flag = true;
        }
        for (int_t i = 1; i <= n; i++)
        {
            cin >> reward[i];
        }
        for (int_t i = 1; i <= m; i++)
        {
            int_t x;
            cin >> x;
            swords.insert(x);
        }
        for (int_t i = 1; i <= n; i++)
        {
            int_t attack = next(a[i]);
            this->attack[i] = attack;
            // cout << "x=" << a[i] << "(mod " << mod[i] << ")" << endl;
            swords.insert(reward[i]);
        }
        if (flag == false)
        {
            int_t result = 0;
            for (int_t i = 1; i <= n; i++)
            {
                result = std::max(result, a[i] / attack[i] + (a[i] % attack[i] != 0));
            }
            return result;
        }
        else
        {
            for (int_t i = 1; i <= n; i++)
            {
                mod[i] = rec[i] / gcd(rec[i], attack[i]);
                c[i] = solve(attack[i], rec[i], a[i]);
                if (c[i] == -1)
                    return -1;
                // cout << c[i] << " " << mod[i] << endl;
            }
            int_t M = mod[1];
            int_t prex = c[1];
            for (int_t i = 2; i <= n; i++)
            {
                int_t A = M;
                int_t B = mod[i];
                int_t C = ((c[i] - prex) % B + B) % B;
                int_t gcd = ::gcd(A, B);
                int_t x = exgcd(A, B).first;
                if (C % gcd)
                    return -1;
                x = mul(x, C / gcd, B / gcd);
                x = (x % (B / gcd) + (B / gcd)) % (B / gcd);
                int_t preM = M;
                M = M / gcd * mod[i];
                prex = (mul(x, preM, M) + prex) % M;
            }
            return prex;
        }
    }
    int_t next(int_t val)
    {
        decltype(swords.begin()) ptr;
        if (val < *(--swords.end()))
        {
            ptr = (--swords.end());
        }
        else
        {
            ptr = swords.lower_bound(val);
            while (*ptr > val)
            {
                ptr++;
            }
        }
        int_t temp = *ptr;
        swords.erase(ptr);
        return temp;
    }
};

int main()
{
#ifdef ONLINE_JUDGE
    freopen("dragon.in", "r", stdin);
    freopen("dragon.out", "w", stdout);
#endif
    int_t T;
    cin >> T;
    for (int_t i = 1; i <= T; i++)
    {
        Processor *p = new Processor;
        cout << p->process() << endl;
        delete p;
    }
    return 0;
}

 

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。