P4774 [NOI2018] 屠龙勇士

典题。
题目不难,注意细节就行。

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cassert>
#include <chrono>
#include <random>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define pf push_front
#define desktop "C:\\Users\\MPC\\Desktop\\"
#define IOS ios :: sync_with_stdio (false),cin.tie (0),cout.tie (0)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> PII;
const int dx[] = {1,0,-1,0},dy[] = {0,-1,0,1};
template <typename T1,typename T2> bool tomax (T1 &x,T2 y) {
    if (y > x) return x = y,true;
    return false;
}
template <typename T1,typename T2> bool tomin (T1 &x,T2 y) {
    if (y < x) return x = y,true;
    return false;
}
LL power (LL a,LL b,LL p) {
    LL ans = 1;
    while (b) {
        if (b & 1) ans = ans * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return ans;
}
int fastio = (IOS,0);
#define endl '\n'
#define puts(s) cout << s << endl
const int N = 100010;
int n,m;
LL a[N],b[N],p[N],t[N];
multiset <LL> s;
LL exgcd (LL a,LL b,LL &x,LL &y) {
	if (!b) {
		x = 1,y = 0;
		return a;
	}
	LL d = exgcd (b,a % b,y,x);
	y -= a / b * x;
	return d;
}
int main () {
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> m;
		s.clear ();
		for (int i = 1;i <= n;i++) cin >> a[i];
		for (int i = 1;i <= n;i++) cin >> p[i];
		for (int i = 1;i <= n;i++) cin >> t[i];
		for (int i = 1;i <= m;i++) {
			LL x;
			cin >> x;
			s.insert (x);
		}
		LL maxx = 0;
		for (int i = 1;i <= n;i++) {
			auto it = s.upper_bound (a[i]);
			if (it != s.begin ()) it--;
			b[i] = *it;
			s.erase (it);
			s.insert (t[i]);
			tomax (maxx,(a[i] + b[i] - 1) / b[i]);
		}
		LL ans = 0,lcm = 1;
		bool no = false;
		for (int i = 1;i <= n;i++) {
			LL A = (__int128)b[i] * lcm % p[i];
			LL B = p[i];
			LL C = (a[i] % p[i] - (__int128)b[i] * ans % p[i] + p[i]) % p[i];
			LL x,y;
			LL d = exgcd (A,B,x,y);
			x = (x % B + B) % B;
			if (C % d) {
				no = true;
				break;
			}
			ans += (__int128)(C / d) * x % (B / d) * lcm % (lcm *= B / d);
			ans %= lcm;
		}
		if (ans < maxx) ans += (maxx - ans + lcm - 1) / lcm * lcm;
		if (no) puts ("-1");
		else cout << ans << endl;
	}
	return 0;
}
posted @   incra  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示