萌新求助你谷 AC Uoj 97pts 的代码 qaq
查看原帖
萌新求助你谷 AC Uoj 97pts 的代码 qaq
298549
SIXIANG32楼主2022/7/23 21:39

如题

//SIXIANG
#include <iostream>
#include <cstdlib>
#include <set>
#define MAXN 100000
#define int long long
#define QWQ cout << "QWQ" << endl;
using namespace std;

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 cdiv(int n, int m) {
	if(n % m != 0) return n / m + 1;
	else return n / m;
}
int ex_ex_crt() {
	int cnt = 0;
	for(int p = 1; p <= n; p++) 
		if(q[p] == 1) cnt++;
	if(cnt == n) {
		int res = 0;
		for(int p = 1; p <= n; p++) {
			res = max(res, cdiv(a[p], b[p]));
		}
		return res;
	} 
	
	int R = 0, M = 1;
	for(int p = 1; 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 + mul(x, M, q[p] / d * M)) % (q[p] / d * M), M = q[p] / d * M;
	}
	return R;
}

void init() {
	multiset <int> S; 
	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], S.insert(b2[p]);
	for(int p = 1; p <= n; p++) {
		if(a[p] < (*S.begin())) {
			b[p] = (*S.begin());
			S.erase(S.begin());
		}
		else {
			multiset <int>::iterator it = S.upper_bound(a[p]); it--;
			b[p] = (*it);
			S.erase(it);
		}
		S.insert(b1[p]);
	}
}

signed main() {
	int T; cin >> T;
	while(T--) {
		init();
		cout << ex_ex_crt() << endl;
	}
}
2022/7/23 21:39
加载中...