提交记录 4402


用户 题目 状态 得分 用时 内存 语言 代码长度
tiantian noi18d. 【NOI2018】屠龙勇士 Accepted 100 614.186 ms 23244 KB C++11 4.91 KB
提交时间 评测时间
2018-07-22 15:56:18 2020-07-31 23:00:38
#include <iostream>
#include <cstdlib>
#include <cstdio>
#define maxn 1000006
#define INF 1LL << 60
#define ll long long
using namespace std;

struct Node{
    Node *ch[2];
    int val, cnt, sze;
    ll key;
    Node(ll V) : key(V){
        ch[0] = ch[1] = NULL;
        val = rand();
        sze = cnt = 1;
    }
    ll cmp(ll x) const{
        if (x == key) return -1;
        return x < key ? 0 : 1;
    }
    void maintain(){
        sze = cnt;
        if (ch[0] != NULL)
            sze += ch[0] -> sze;
        if (ch[1] != NULL)
            sze += ch[1] -> sze;
    }
};
void Rotate(Node* &o, int d){
    Node* k = o -> ch[d ^ 1];
    o -> ch[d ^ 1] = k -> ch[d];
    k -> ch[d] = o;
    o -> maintain();
    k -> maintain();
    o = k;
}
void ins(Node* &o, ll x){
    if (o == NULL)
        o = new Node(x);
    else{
        int d = o -> cmp(x);
        if (d == -1){
            o -> sze ++;
            o -> cnt ++;
            return;
        }
        ins(o -> ch[d], x);
        if (o -> ch[d] -> val > o -> val)
            Rotate(o, d ^ 1);
    }
    o -> maintain();
}
void del(Node* &o, ll x){
    if (o == NULL)
        return;
    int d = o -> cmp(x);
    if (d == -1){
        if (o -> cnt > 1){
            o -> cnt --;
            o -> sze --;
            return;
        }
        if (o -> ch[0] != NULL && o -> ch[1] != NULL){
            int d2 = (o -> ch[0] -> val < o -> ch[1] -> val ? 0 : 1);
            Rotate(o, d2);
            del(o -> ch[d2], x);
        }
        else{
            Node* u = o;
            if (o -> ch[0] == NULL)
                o = o -> ch[1];
            else if (o -> ch[1] == NULL)
                o = o -> ch[0];
            delete u;
        }
    }
    else
        del(o -> ch[d], x);
    if (o != NULL)
        o -> maintain();
}
ll pre(Node* o, ll x){
    if (o == NULL)
        return -INF;
	if (o -> key == x)
		return x;
    if (o -> key < x)
        return max(o -> key, pre(o -> ch[1], x));
    else
        return pre(o -> ch[0], x);
}
ll suc(Node* o, ll x){
    if (o == NULL)
        return INF;
    if (o -> key > x)
        return min(o -> key, suc(o -> ch[0], x));
    else
        return suc(o -> ch[1], x);
}

void exgcd(ll a, ll b, ll &x, ll &y){
    if (b == 0){
        x = 1;
        y = 0;
        return;
    }
    ll tx, ty;
    exgcd(b, a % b, tx, ty);
    x = ty;
    y = tx - a / b * ty;
}
ll gcd(ll x, ll y){
	if (y == 0)
		return x;
	return gcd(y, x % y);
}

ll mul(ll x, ll y, ll mod){
	ll ans = 0;
	for (ll i = 1; i <= y; i <<= 1, x = (x + x) % mod)
		if (y & i)
			ans = (ans + x) % mod;
	return ans;
}

ll a[maxn], m[maxn], get[maxn];
bool merg(ll a1, ll m1, ll a2, ll m2, ll& a3, ll& m3){
    ll d = gcd(m1, m2), c = a2 - a1;
    if (c % d)
        return false;
    c = (c % m2 + m2) % m2;
    c /= d;
    m1 /= d;
    m2 /= d;
    ll tmp1 = 0, tmp2 = 0;
    exgcd(m1, m2, tmp1, tmp2);
    tmp1 = (tmp1 + m2) % m2;
    c = mul(c, tmp1, m2);
    m3 = m1 * m2 * d;
	c = mul(c, m1, m3);
	c = mul(c, d, m3);
    a3 = ((c + a1) % m3 + m3) % m3;
    return true;
}

ll read(){
	ll cnt = 0;
	char ch = getchar();
	while (ch < '0' || '9' < ch)
		ch = getchar();
	while ('0' <= ch && ch <= '9'){
		cnt = cnt * 10 + ch - '0';
		ch = getchar();
	}
	return cnt;
}

Node* o[10];
int main(){
//	freopen("dragon.in", "r", stdin);
//	freopen("dragon.out", "w", stdout);
	int T = read();
	for (int cas = 1; cas <= T; ++ cas){
		int n = read(), M = read();
		for (int i = 1; i <= n; ++ i)
			a[i] = read();
		long long mx = 0;
		for (int i = 1; i <= n; ++ i)
			m[i] = read();
		for (int i = 1; i <= n; ++ i)
			get[i] = read();
		for (int i = 1; i <= M; ++ i)
			ins(o[cas], read());
		bool nosol = false;
		int fir = -1;
		long long mn = -INF;
		for (int i = 1; i <= n; ++ i){
			ll x = pre(o[cas], a[i]);
			if (x == -INF)
				x = suc(o[cas], a[i]);
			ll tmp = x;
			ll d = gcd(x, m[i]);
			if (x % m[i] == 0 && a[i] % m[i] == 0){
				if (a[i] % x == 0)
					mn = max(mn, a[i] / x);
				else
					mn = max(mn, a[i] / x + 1);
				a[i] = -1;
				del(o[cas], tmp);
				ins(o[cas], get[i]);
				continue;
			}
			if (a[i] % d){
				nosol = true;
				cout << -1 << endl;
				break;
			}
			if (fir == -1)
				fir = i;
			x /= d, m[i] /= d, a[i] /= d;
			ll tmp1 = 0, tmp2 = 0;
			exgcd(x, m[i], tmp1, tmp2);
			tmp1 = (tmp1 + m[i]) % m[i];
			a[i] = mul(a[i] % m[i], tmp1, m[i]);
			del(o[cas], tmp);
			ins(o[cas], get[i]);
		}
		if (nosol)
			continue;
		if (fir == -1){
			cout << mn << endl;
			continue;
		}
		ll a0 = a[fir], m0 = m[fir];
		for (int i = fir + 1; i <= n; ++ i){
			if (a[i] == -1)
				continue;
			ll _a, _m;
			if (merg(a0, m0, a[i], m[i], _a, _m) == false){
				cout << -1 << endl;
				nosol = true;
				break;
			}
			a0 = _a, m0 = _m;
		}
		if (! nosol){
			if (mn == -INF || a0 >= mn)
				cout << a0 << endl;
			else{
				long long k;
				if ((mn - a0) % m0 == 0)
					k = (mn - a0) / m0;
				else
					k = (mn - a0) / m0 + 1;
				cout << a0 + k * m0 << endl;
			}
		}
	}
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #135.21 ms2 MB + 340 KBAcceptedScore: 5

Testcase #235.097 ms2 MB + 340 KBAcceptedScore: 5

Testcase #342.791 ms2 MB + 340 KBAcceptedScore: 5

Testcase #442.636 ms2 MB + 340 KBAcceptedScore: 5

Testcase #54.455 ms124 KBAcceptedScore: 5

Testcase #64.144 ms92 KBAcceptedScore: 5

Testcase #74.118 ms84 KBAcceptedScore: 5

Testcase #842.01 us48 KBAcceptedScore: 5

Testcase #942 us48 KBAcceptedScore: 5

Testcase #1040.7 us48 KBAcceptedScore: 5

Testcase #1140.86 us48 KBAcceptedScore: 5

Testcase #1239.64 us48 KBAcceptedScore: 5

Testcase #1339.56 us48 KBAcceptedScore: 5

Testcase #14449.874 ms22 MB + 716 KBAcceptedScore: 5

Testcase #15450.814 ms22 MB + 548 KBAcceptedScore: 5

Testcase #16593.987 ms2 MB + 340 KBAcceptedScore: 5

Testcase #17601.949 ms2 MB + 340 KBAcceptedScore: 5

Testcase #18614.186 ms2 MB + 340 KBAcceptedScore: 5

Testcase #19611.664 ms2 MB + 340 KBAcceptedScore: 5

Testcase #20611.826 ms2 MB + 340 KBAcceptedScore: 5


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