萌新求助又 WA 又 T 又 RE 的 0pts 代码
查看原帖
萌新求助又 WA 又 T 又 RE 的 0pts 代码
298549
SIXIANG32楼主2022/7/20 19:38

如题

大样例甚至 RE 了。。。

//SIXIANG
#include <iostream>
#include <cstdlib>
#define MAXN 100000
#define int long long
#define QWQ cout << "QWQ" << endl;
using namespace std;
struct node {
	int val, ls, rs, siz, rnd;
} T[MAXN + 10];
void update(int x) {
	T[x].siz = T[T[x].ls].siz + T[T[x].rs].siz + 1; 
}
void split(int now, int val, int &x, int &y) {
	if(!now) {
		x = y = 0;
		return ;
	}
	if(T[now].val <= val) x = now, split(T[now].rs, val, T[now].rs, y);
	else y = now, split(T[now].ls, val, x, T[now].ls);
	update(now);
}
int merge(int x, int y) {
	if(!x || !y) return x | y;
	else {
		if(T[x].rnd < T[y].rnd) {
			T[x].rs = merge(T[x].rs, y);
			update(x);
			return x;
		}
		else {
			T[y].ls = merge(x, T[y].ls);
			update(y);
			return y;
		}
	}
}
int root = 0, tot = 0;
int New(int val) {
	T[++tot].rnd = rand();
	T[tot].siz = 1;
	T[tot].val = val;
	return tot;
}
int Kth(int rt, int rnk) {
	int now = rt;
	while(114514) {
		if(!now) break;
		if(T[T[now].ls].siz >= rnk) now = T[now].ls;
		else if(T[T[now].ls].siz + 1 == rnk) return T[now].val;
		else rnk = rnk - T[T[now].ls].siz - 1, now = T[now].rs;
	}
	return 1145141919;
}
void Insert(int val) {
	int x, y;
	split(root, val, x, y);
	root = merge(merge(x, New(val)), y);
}
int Upper(int val) {
	int x, y;
	split(root, val, x, y);
	int ans = Kth(x, T[x].siz);
	return ans;
}

int n, m;
int b1[MAXN + 10], b2[MAXN + 10], b[2 * MAXN + 10], a[MAXN + 10], q[MAXN + 10];

int mul(int n, int m, int M) {
	int res = 0;
	while(m) {
		if(m & 1) res = (res + n) % M; 
		n = (n + n) % M, m >>= 1;
	}
	return res;
}
int exgcd(int a, int b, int &x, int &y) {
	if(!b) {
		x = 1, y = 0;
		return a;
	}
	else {
		int rest = exgcd(b, a % b, x, y);
		int tmp = x; x = y, y = tmp - a / b * x;
		return rest;
	}
}

int ex_ex_crt() {
	int R = a[1], M = q[1];
	for(int p = 2; p <= n; p++) {
		int K = (mul(b[p], R, q[p]) + q[p]) % q[p];
		int Q = ((a[p] - K) % q[p] + q[p]) % q[p];
		int P = mul(b[p], M, q[p]);
		int x = 0, y = 0, d = exgcd(P, q[p], x, y);
		if(Q % d != 0) return -1;
		x = (mul(x, Q / d, q[p] / d) + q[p] / d) % (q[p] / d);
		R = R + x * M, M = q[p] / d * M;
	}
	return R;
}

void init() {
	root = tot = 0;
	for(int p = 1; p <= n; p++)
		T[p].ls = T[p].rs  = T[p].rnd = T[p].siz = T[p].val = 0;
	cin >> n >> m;
	for(int p = 1; p <= n; p++) cin >> a[p];
	for(int p = 1; p <= n; p++) cin >> q[p];
	for(int p = 1; p <= n; p++) cin >> b1[p];
	for(int p = 1; p <= m; p++) cin >> b2[p], Insert(b2[p]);
	
	for(int p = 1; p <= n; p++) {
		int qwq = Upper(a[p]);
		if(qwq != 1145141919) b[p] = qwq;
		else b[p] = Kth(root, 1);
		Insert(b1[p]);
	}
}

signed main() {
	freopen("test.txt", "r", stdin);
	int T; cin >> T;
	while(T--) {
		init();
		int ans = ex_ex_crt();
		if(ans == -1145141919) cout << -1 << endl;
		else cout << ans << endl;
	}
}
2022/7/20 19:38
加载中...