首页 > 其他分享 >P4774 [NOI2018] 屠龙勇士

P4774 [NOI2018] 屠龙勇士

时间:2024-10-14 19:11:52浏览次数:1  
标签:P4774 return 屠龙 int LL ans include NOI2018 define

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

#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;
}

标签:P4774,return,屠龙,int,LL,ans,include,NOI2018,define
From: https://www.cnblogs.com/incra/p/18464800

相关文章

  • 传奇血战屠龙十大陆单机版安装教程+GM+无需虚拟机
    今天给大家带来一款单机游戏的架设:传奇血战屠龙。另外:本人承接各种游戏架设(单机+联网)本人为了学习和研究软件内含的设计思想和原理,带了架设教程仅供娱乐。教程是本人亲自搭建成功的,绝对是完整可运行的,踩过的坑都给你们填上了。如果你是小白也没问题,跟着教程走也是可以搭建成功的......
  • 无敌屠龙战士队——团队展示
    |这个作业属于哪个课程|https://edu.cnblogs.com/campus/hniit/AI2022||这个作业要求在哪里||里|https://edu.cnblogs.com/campus/hniit/AI2022/homework/13280||团队名称|无敌屠龙战士队||这个作业的目标|展示团队风采||作业正文|要成功,......
  • 那些年,学过的屠龙术
    朱泙漫学屠龙于支离益,单千金之家,三年技成而无所用其巧。程序员的技能,比如:Windows平台编程,从入行业时至今,没有做过相关的业务,相关的知识只能当成谈资。Windows平台下应用的crash问题,如何分析。Windows平台下应用的内存泄漏问题,如何分析。常见工具、命令的使用。Linux平......
  • 倚天屠龙记(函数模板)
    题目描述江湖中有一个传言,只要倚天剑和屠龙刀中暗藏的秘密拼到一起,就能得到天下无敌的内功秘笈。设计一个函数模板,完成拼凑的功能(将倚天剑的秘密连接到屠龙刀的后面),并将秘笈输出.其中每个秘密由n个元素组成,类型为T。输入第一行输入t表示有t个测试实例第二行先输入一个大写......
  • 题解 LGP5397【[Ynoi2018] 天降之物】/ 第四分块
    题解P5397【[Ynoi2018]天降之物】/第四分块题目描述一个长为\(n\)的序列\(a\)。你需要实现\(m\)个操作,操作有两种:把序列中所有值为\(x\)的数的值变成\(y\)。找出一个位置\(i\)满足\(a_i=x\),找出一个位置\(j\)满足\(a_j=y\),使得\(|i-j|\)最小,并输出\(|i-......
  • #莫队二次离线,根号分治#洛谷 5398 [Ynoi2018] GOSICK
    题目\(m\)组询问求\(\sum_{l\leqi,j\leqr}[a_i\bmoda_j==0],n,m,a_i\leq5\times10^5\)分析设\(f(l,r,x)\)表示\(i或j\in[1,x],i或j\in[l,r]\)时的答案,\(g_x\)表示\([1,x]\)的答案,根号的做法可以通过三秒由于涉及区间内的求值,需要在莫队的基础上二次离线,那......
  • P4774 屠龙勇士 题解
    传送门显然每一只龙对应了唯一的一把剑。用multiset可以求出每一把剑。于是题目就变成了:\[\begin{cases}b_1x\equiva_1\pmod{m_1}\\b_2x\equiva_2\pmod{m_2}\\\dots\\b_nx\equiva_n\pmod{m_n}\end{cases}\]如果\(b_i=1\),直接EXCRT即可。现在\(b_i>1\),还是以EXCRT......
  • P4119 Ynoi2018 未来日记
    P4119Ynoi2018未来日记lxl出的题好duliu啊。感谢来自fr200110217102的博客题解P4119【Ynoi2018未来日记】。下标分块+值域分块+并查集其实一开始的方向应该是尝试线段树或者其它的动态维护的算法,直到时间复杂度和空间复杂度对不上,你才会想到——要分块!区间第\(k\)......
  • NOI2018 你的名字。
    您的名字。做法好像和其他题解不太一样。空间少个\(\log\),而且常数小。description给定长度为\(n\)的字符串\(S\)。\(Q\)次询问,第\(t\)次给定字符串\(T_t\)以及正整数\(l,r\in[1,n]\)。每次询问回答\(T_t\)有几个本质不同子串在\(S_{l\dotsr}\)中没有出现过......
  • P4786 [BalkanOI2018] Election 题解
    题意给定一个长度为\(n\)的字符串\(s\),有\(m\)个询问,每次询问最少需要删掉多少个字符才能使\(l\)到\(r\)组成的字符串当中的每一个前缀和后缀都满足C的数量不小于T的数量。思路因为要满足C的数量不小于T的数量,我们不妨设字符C的位置的值为\(1\),字符S的位......