提交记录 4568


用户 题目 状态 得分 用时 内存 语言 代码长度
langsike noi18d. 【NOI2018】屠龙勇士 Accepted 100 737.489 ms 8628 KB C++ 3.02 KB
提交时间 评测时间
2018-07-25 15:25:16 2020-07-31 23:08:10
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=1e6;
int t,n,m;
LL a[N+10],p[N+10],atk[N+10];
struct data {
    LL x;
    int loc;
    bool operator < (const data &p) const {
        if (x!=p.x) return x<p.x;
        return loc<p.loc;
    }
};
set <data> s;
LL Find(LL x) {
    data now,ans;
    now.x=x;
    now.loc=1e9;
    set <data>::iterator it=s.lower_bound(now);
    if (it==s.begin()) {
        ans=*it;
        s.erase(it);
        return ans.x;
    }
    it--;
    ans=*it;
    s.erase(it);
    return ans.x;
}
LL gcd(LL x,LL y) {
    if (y==0) return x;
    return gcd(y,x%y);
}
void ex_gcd(LL a,LL b,LL &x,LL &y) {
    if (b==0) {
        x=1;
        y=0;
        return;
    }
    LL nowx,nowy;
    ex_gcd(b,a%b,nowx,nowy);
    x=nowy;
    y=nowx-a/b*nowy;
}
LL LCM(LL x,LL y) {
    LL d=gcd(x,y);
    return x/d*y;
}
LL multi(LL x,LL y,LL mod) {
    LL z=0;
    while (y) {
        if (y&1) z=(z+x)%mod;
        y>>=1;
        x=x*2%mod;
    }
    return z;
}
LL work(LL p1,LL a1,LL p2,LL a2) {
    LL d=gcd(p1,p2);
    if ((a2-a1)%d!=0) return -1;
    LL nowp1=p1/d,nowp2=p2/d;
    LL nowv=(a2-a1)/d;
    LL nowx,nowy;
    ex_gcd(nowp1,nowp2,nowx,nowy);
    nowx=(nowx%nowp2+nowp2)%nowp2;
    nowv=(nowv%nowp2+nowp2)%nowp2;
    nowx=multi(nowx,nowv,nowp2);
    LL lcm=LCM(p1,p2);
    nowx=((nowx*p1+a1)%lcm+lcm)%lcm;
    return nowx;
}
int main() {
    //freopen("dragon.in","r",stdin);
    //freopen("dragon.out","w",stdout);
    scanf("%d",&t);
    while (t--) {
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
        for (int i=1;i<=n;i++) scanf("%lld",&p[i]);
        for (int i=1;i<=n;i++) scanf("%lld",&atk[i]);
        s.clear();
        for (int i=1;i<=m;i++) {
            data now;
            scanf("%lld",&now.x);
            now.loc=i;
            s.insert(now);
        }
        if (m==0) {
            printf("-1\n");
            continue;
        }
        LL lim=0,lcm=1,nowmod=0;
        bool judge=1;
        for (int i=1;i<=n;i++) {
            LL ki=Find(a[i]);
            data nowpv;
            nowpv.x=atk[i];
            nowpv.loc=m+i;
            s.insert(nowpv);
            lim=max(lim,(a[i]-1)/ki+1);
            a[i]%=p[i];
            LL d=gcd(p[i],ki);
            if (a[i]%d) {
                judge=0;
                break;
            }
            a[i]/=d;
            p[i]/=d;
            ki/=d;
            LL nowx,nowy;
            ex_gcd(ki,p[i],nowx,nowy);
            nowx=(nowx%p[i]+p[i])%p[i];
            nowx=multi(nowx,a[i],p[i]);
            nowmod=work(lcm,nowmod,p[i],nowx);
            if (nowmod==-1) {
                judge=0;
                break;
            }
            lcm=LCM(lcm,p[i]);
        }
        if (!judge) {
            printf("-1\n");
            continue;
        }
        nowmod=(nowmod%lcm+lcm)%lcm;
        nowmod+=lim/lcm*lcm;
        while (nowmod<lim) nowmod+=lcm;
        while (nowmod-lcm>=lim) nowmod-=lcm;
        printf("%lld\n",nowmod);
    }
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1139.338 ms2 MB + 328 KBAcceptedScore: 5

Testcase #2139.019 ms2 MB + 328 KBAcceptedScore: 5

Testcase #3146.084 ms2 MB + 328 KBAcceptedScore: 5

Testcase #4146.129 ms2 MB + 328 KBAcceptedScore: 5

Testcase #53.441 ms124 KBAcceptedScore: 5

Testcase #63.401 ms124 KBAcceptedScore: 5

Testcase #73.454 ms124 KBAcceptedScore: 5

Testcase #823.07 us36 KBAcceptedScore: 5

Testcase #922.39 us36 KBAcceptedScore: 5

Testcase #1022.15 us36 KBAcceptedScore: 5

Testcase #1122.31 us36 KBAcceptedScore: 5

Testcase #1222.75 us36 KBAcceptedScore: 5

Testcase #1324.14 us36 KBAcceptedScore: 5

Testcase #14395.341 ms8 MB + 436 KBAcceptedScore: 5

Testcase #15395.63 ms8 MB + 436 KBAcceptedScore: 5

Testcase #16721.521 ms8 MB + 436 KBAcceptedScore: 5

Testcase #17720.57 ms8 MB + 436 KBAcceptedScore: 5

Testcase #18737.489 ms8 MB + 436 KBAcceptedScore: 5

Testcase #19736.453 ms8 MB + 436 KBAcceptedScore: 5

Testcase #20732.599 ms8 MB + 436 KBAcceptedScore: 5


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