萌新求助excrt
查看原帖
萌新求助excrt
576817
Nekopedia楼主2025/1/18 18:02

有的点RE,有的点WA,求条

#include <bits/stdc++.h>
#define ll long long
#define i128 __int128
using namespace std;
ll rd(){
   ll x = 0, f = 1; char c = getchar();
   while(! isdigit(c)){if(c == '-')f = - f; c = getchar();}
   while(isdigit(c)){x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}
   return x * f;
}

const int N = 1e5 + 5;
int n, m;
i128 f[N];
struct MOD{i128 a, b, c;}A[N];

void exgcd(i128 a, i128 b, i128 &x, i128 &y){
    if(! b)return x = 1, y = 0, void();
    exgcd(b, a % b, y, x);
    y -= a / b * x;
}

i128 Gcd(i128 a, i128 b){
    return b ? Gcd(b, a % b) : a;
}

i128 excrt(){
    while(n ^ 1){
        i128 a1 = A[n].a * A[n - 1].c, a2 = A[n - 1].a * A[n].c;
        i128 b1 = A[n].b * A[n - 1].c, b2 = A[n - 1].b * A[n].c;
        i128 d = Gcd(b1, b2);
        if((a2 - a1) % d)return - 1;
        i128 c1 = b1 / d, c2 = b2 / d, x, y;
        exgcd(c1, c2, x, y); x = (x % c2 + c2) % c2;
        i128 lcm = b1 / d * b2;
        i128 nwa = (a2 - a1) / d * x % lcm * b1 + a1;
        i128 k = A[n].c * A[n - 1].c % lcm;
        nwa = (nwa % lcm + lcm) % lcm;
        A[--n] = {nwa, lcm, k};
    }
    i128 c = A[1].a, b = A[1].b, a = A[1].c;
    i128 d = Gcd(a, b);
    if(c % d)return - 1; a /= d, b /= d, c /= d;
    i128 x, y; exgcd(a, b, x, y);
    x *= c; x = (x % b + b) % b;
    return x;
}
set < ll > ss;

signed main(){
    int T = rd();
    while(T--){
        n = rd(); m = rd(); ss.clear();
        for(int i = 1; i <= n; ++i)A[i].a = rd();
        for(int i = 1; i <= n; ++i)A[i].b = rd();
        for(int i = 1; i <= n; ++i)f[i] = rd();
        for(ll i = 1, x; i <= m; ++i)x = rd(), ss.insert(x);
        for(ll i = 1; i <= n; ++i){
            set < ll > :: iterator it = ss.lower_bound(A[i].a);
            if(it == ss.end())it--;
            else if(*it > A[i].a and it != ss.begin())it--;
            A[i].c = *it; ss.erase(it);
            ss.insert(f[i]);
        }
        // for(int i = 1; i <= n; ++i)printf("%lld %lld %lld\n", A[i].a, A[i].b, A[i].c);
        printf("%lld\n", (ll)excrt());
    }
    return 0;
}
2025/1/18 18:02
加载中...