提交记录 4356


用户 题目 状态 得分 用时 内存 语言 代码长度
cold_chair noi18d. 【NOI2018】屠龙勇士 Accepted 100 699.162 ms 9416 KB C++ 2.35 KB
提交时间 评测时间
2018-07-20 14:44:37 2020-07-31 22:54:33
#include<set>
#include<cstdio>
#define ll long long
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;

const int N = 2e5 + 5;

const ll inf = - (1LL << 62);

int T, n, m, q;
ll a[N], p[N], v1[N], v2[N];

struct node {
	ll a, p;
} b[N];

multiset<ll> s;

ll mx, ans;

ll gcd(ll x, ll y) {return !y ? x : gcd(y, x % y);}

ll x1, y1;

void exgcd(ll a, ll b) {
	if(b == 0) {x1 = a; y1 = 0; return;}
	exgcd(b, a % b);
	ll x2 = x1, y2 = y1;
	x1 = y2; y1 = x2 - (a / b) * y2;
}

ll getni(ll x, ll p) {
	if(gcd(x, p) != 1 && gcd(x, p) != -1) return inf;
	exgcd(x, -p);
	return (x1 % p + p) % p;
}

ll lcm, mo;

ll mul(ll x, ll y, const ll mo) {
	x %= mo; y %= mo;
    ll z = (long double) x * y / mo; z = x * y - z * mo;
    if(z < 0) z += mo; else if(z > mo) z -= mo;
    return z;
}

ll solve(ll a, ll b, ll c) {
	ll d = gcd(a, gcd(b, c));
	a /= d; b /= d; c /= d;
	if(c == 0) return b;
	if(gcd(a, b) != 1 && gcd(a, b) != -1) return inf;
	exgcd(a, b);
	return mul(x1, c, mo);	
}


int main() {
	for(scanf("%d", &T); T; T --) {
		scanf("%d %d", &n, &m);
		fo(i, 1, n) scanf("%lld", &a[i]);
		fo(i, 1, n) scanf("%lld", &p[i]);
		fo(i, 1, n) scanf("%lld", &v2[i]);
		fo(i, 1, m) scanf("%lld", &v1[i]);
		
		s.clear();
		fo(i, 1, m) s.insert(v1[i]);
		mx = 0; ans = 0; q = 0;
		
		fo(i, 1, n) {
			ll x;
			if((*s.begin()) >= a[i]) {
				x = *s.begin();
			} else {
				if((*s.rbegin()) > a[i]) {
					x = *(-- s.upper_bound(a[i]));
				} else x = *s.rbegin();
			}
			s.erase(s.find(x)); s.insert(v2[i]);
			ll d = gcd(x, gcd(a[i], p[i]));
			x /= d; a[i] /= d; p[i] /= d;
			mx = max(mx, a[i] / x + (a[i] % x > 0));
			if(p[i] == 1) continue;
			mo = p[i];
			ll y = solve(x, -p[i], a[i]);
			if(y == inf) {ans = -1; break;}
			y = (y % p[i] + p[i]) % p[i];
			b[++ q].a = y; b[q].p = p[i];
		}
		if(ans == -1) {
			printf("-1\n"); continue;
		}
		fo(i, 2, q) {
			ll d = gcd(b[i - 1].p, b[i].p);
			lcm = b[i - 1].p / d * b[i].p;
			mo = lcm;
			ll x = solve(b[i - 1].p, -b[i].p, b[i - 1].a - b[i].a);
			if(x == inf) {
				ans = -1; break;
			}
			x = b[i - 1].a - mul(x, b[i - 1].p, lcm);
			x = (x % lcm + lcm) % lcm;
			b[i].p = lcm; b[i].a = x;
		}
		if(ans == -1) {
			printf("-1\n"); continue;
		}
		if(q == 0) {
			ans = mx;			
		} else {
			ans = mx / b[q].p * b[q].p + b[q].a;
			if(ans < mx) ans += b[q].p;
		}
		printf("%lld\n", ans);
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #168.957 ms2 MB + 340 KBAcceptedScore: 5

Testcase #268.206 ms2 MB + 340 KBAcceptedScore: 5

Testcase #371.118 ms2 MB + 340 KBAcceptedScore: 5

Testcase #470.419 ms2 MB + 340 KBAcceptedScore: 5

Testcase #54.114 ms140 KBAcceptedScore: 5

Testcase #64.148 ms140 KBAcceptedScore: 5

Testcase #74.183 ms140 KBAcceptedScore: 5

Testcase #821.87 us44 KBAcceptedScore: 5

Testcase #921.62 us44 KBAcceptedScore: 5

Testcase #1021.19 us44 KBAcceptedScore: 5

Testcase #1120.9 us44 KBAcceptedScore: 5

Testcase #1220.72 us44 KBAcceptedScore: 5

Testcase #1321.71 us44 KBAcceptedScore: 5

Testcase #14290.217 ms7 MB + 688 KBAcceptedScore: 5

Testcase #15290.766 ms7 MB + 688 KBAcceptedScore: 5

Testcase #16698.926 ms9 MB + 200 KBAcceptedScore: 5

Testcase #17699.162 ms9 MB + 200 KBAcceptedScore: 5

Testcase #18679.473 ms9 MB + 200 KBAcceptedScore: 5

Testcase #19674.066 ms9 MB + 200 KBAcceptedScore: 5

Testcase #20671.239 ms9 MB + 200 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-05-06 02:39:07 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用