提交记录 6358


用户 题目 状态 得分 用时 内存 语言 代码长度
charliez noi18d. 【NOI2018】屠龙勇士 Accepted 100 778.62 ms 7056 KB C++ 2.27 KB
提交时间 评测时间
2018-10-06 11:33:03 2020-08-01 00:42:54
#include <cstdio>
#include <set>
#define RG register
#define R RG LL
#define GC c = getchar()
using namespace std;
typedef long long LL;
const int N = 1e5 + 9;
LL a[N], p[N], b[N], X, Y, G;
multiset<LL> s;
inline LL in()
{
    RG char GC;
    while (c < '-')
        GC;
    R x = c & 15;
    GC;
    while (c > '-')
        x *= 10, x += c & 15, GC;
    return x;
}
void exgcd(R a, R b)
{
    if (!b)
    {
        X = 1;
        Y = 0;
        G = a;
        return;
    }
    exgcd(b, a % b);
    (Y ^= X ^= Y ^= X) -= a / b * X;
}
inline LL mul(R b, R k, R m)
{ //快速乘
    R a = 0;
    for (; k; k >>= 1, b = (b << 1) % m)
        if (k & 1)
            a = (a + b) % m;
    return a;
}
int main()
{
    R T = in(), n, m, i, c, k, mx;
    RG multiset<LL>::iterator it;
E:
    while (T--)
    {
        n = in();
        m = in();
        for (i = 1; i <= n; ++i)
            a[i] = in();
        for (i = 1; i <= n; ++i)
            p[i] = in();
        for (i = 1; i <= n; ++i)
            b[i] = in();
        s.clear(); //注意清空
        for (i = 1; i <= m; ++i)
            s.insert(in());
        mx = c = 0;
        m = 1; //初始总方程
        for (i = 1; i <= n; ++i)
        {
            it = s.upper_bound(a[i]); //谨慎选择lower_bound和upper_bound
            if (it != s.begin())
                --it;
            k = *it;
            s.erase(it);
            s.insert(b[i]);                   //小心手一滑erase(*it)(居然还有90分)
            mx = max(mx, (a[i] - 1) / k + 1); //更新限制
            k %= p[i];
            a[i] %= p[i]; //开始解方程,去掉系数
            if (!k && a[i])
            {
                puts("-1");
                goto E;
            }
            if (!k && !a[i])
                continue; //这两个要特判
            exgcd(k, p[i]);
            if (a[i] % G)
            {
                puts("-1");
                goto E;
            }
            p[i] /= G;
            a[i] = mul(a[i] / G, (X % p[i] + p[i]) % p[i], p[i]);
            exgcd(m, p[i]); //开始合并,X和a-c都可能是负数
            if ((a[i] - c) % G)
            {
                puts("-1");
                goto E;
            }
            m = m / G * p[i];
            c = (c + mul(mul(m / p[i], ((a[i] - c) % m + m) % m, m), (X % m + m) % m, m)) % m;
        }
        printf("%lld\n", c >= mx ? c : c + m * ((mx - c - 1) / m + 1)); //满足限制
    }
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #131.572 ms2 MB + 320 KBAcceptedScore: 5

Testcase #231.37 ms2 MB + 320 KBAcceptedScore: 5

Testcase #336.256 ms2 MB + 320 KBAcceptedScore: 5

Testcase #436.151 ms2 MB + 320 KBAcceptedScore: 5

Testcase #53.319 ms108 KBAcceptedScore: 5

Testcase #63.352 ms108 KBAcceptedScore: 5

Testcase #73.412 ms108 KBAcceptedScore: 5

Testcase #820.09 us36 KBAcceptedScore: 5

Testcase #920.08 us36 KBAcceptedScore: 5

Testcase #1019.78 us36 KBAcceptedScore: 5

Testcase #1118.75 us36 KBAcceptedScore: 5

Testcase #1219.92 us36 KBAcceptedScore: 5

Testcase #1320.65 us36 KBAcceptedScore: 5

Testcase #14248.807 ms6 MB + 912 KBAcceptedScore: 5

Testcase #15249.348 ms6 MB + 912 KBAcceptedScore: 5

Testcase #16748.345 ms6 MB + 912 KBAcceptedScore: 5

Testcase #17758.12 ms6 MB + 912 KBAcceptedScore: 5

Testcase #18778.62 ms6 MB + 912 KBAcceptedScore: 5

Testcase #19770.675 ms6 MB + 912 KBAcceptedScore: 5

Testcase #20772.583 ms6 MB + 912 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 09:43:30 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠