「NOI2018」屠龙勇士(CRT)
/*
首先杀每条龙用到的刀是能够确定的, 然后我们便得到了许多形如 ai - x * atki | pi的方程
而且限制了x的最小值
那么exgcd解出来就好了
之后就是扩展crt合并了
因为全T调了一个小时 结果是没加文件??
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
#include<iostream>
#define ll long long
#define M 100010
#define mmp make_pair
using namespace std;
ll read()
{
ll nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
ll n, q, a[M], m[M], p[M], g[M], atk[M], tp, maxx;
multiset<ll> st;
ll mul(ll a, ll b, ll mod)
{
b = (b % mod + mod) % mod;
ll ans = 0, tmp = a;
for(; b; b >>= 1, tmp = (tmp + tmp) % mod) if(b & 1) ans = (ans + tmp) % mod;
return ans;
}
ll gcd(ll a, ll b)
{
return !b ? a : gcd(b, a % b);
}
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if(!b)
{
x = 1, y = 0;
return a;
}
else
{
ll d = exgcd(b, a % b, x, y);
ll tmp = x;
x = y, y = tmp - a / b * y;
return d;
}
}
ll inv(ll a, ll p)
{
ll x, y;
ll d = exgcd(a, p, x, y);
if(d != 1) return -1;
return (x % p + p) % p;
}
void init()
{
st.clear();
tp = maxx = 0;
n = read(), q = read();
for(int i = 1; i <= n; i++) a[i] = read();
for(int i = 1; i <= n; i++) p[i] = read();
for(int i = 1; i <= n; i++) g[i] = read();
for(int i = 1; i <= q; i++) st.insert(read());
for(int i = 1; i <= n; i++)
{
multiset<ll>::iterator it = st.upper_bound(a[i]);
if(it != st.begin()) it--;
atk[i] = *it;
st.erase(it);
st.insert(g[i]);
}
}
ll excrt()
{
ll a1 = a[1], m1 = m[1], a2, m2;
if(tp == 0)
{
a1 = 0;
}
else
{
for(int i = 2; i <= tp; i++)
{
a2 = a[i], m2 = m[i];
ll d = gcd(m1, m2);
ll c = a2 - a1;
if(c % d) return -1;
ll k = inv(m1 / d, m2 / d);
m2 = m1 / d * m2;
a1 = mul(mul(m1 / d, c, m2), k, m2) + a1;
a1 %= m2;
m1 = m2;
}
}
return max(a1, maxx);
}
void cz(int i)
{
// a[i] - x * atk[i] + k * pi = 0
// a[i] = x * atk[i] - k * p[i]
// x * atk[i] = a[i] mod p[i]
//先处理gcd, 然后处理逆元
if(p[i] == 1)
{
maxx = max(maxx, (a[i] + atk[i] - 1) / atk[i]);
}
else
{
tp++;
ll gcdd = gcd(atk[i], p[i]);
if(a[i] % gcdd)
{
a[tp] = -1;
}
else
{
atk[i] /= gcdd, p[i] /= gcdd;
a[i] /= gcdd;
a[tp] = mul(a[i], inv(atk[i], p[i]), p[i]);
m[tp] = p[i];
}
}
}
void work()
{
for(int i = 1; i <= n; i++)
{
cz(i);
if(a[tp] == -1)
{
puts("-1");
return;
}
}
cout << excrt() << "\n";
}
int main()
{
freopen("dragon.in", "r", stdin);
freopen("dragon.out", "w", stdout);
//freopen("dragon1.in", "r", stdin);
int t = read();
while(t--)
{
init();
work();
}
return 0;
}
「NOI2018」屠龙勇士(CRT)的更多相关文章
- loj#2721. 「NOI2018」屠龙勇士
题目链接 loj#2721. 「NOI2018」屠龙勇士 题解 首先可以列出线性方程组 方程组转化为在模p意义下的同余方程 因为不保证pp 互素,考虑扩展中国剩余定理合并 方程组是带系数的,我们要做的 ...
- 「NOI2018」屠龙勇士
「NOI2018」屠龙勇士 题目描述 小\(D\)最近在网上发现了一款小游戏.游戏的规则如下: 游戏的目标是按照编号\(1-n\)顺序杀掉\(n\) 条巨龙,每条巨龙拥有一个初始的生命 值ai .同时 ...
- 「NOI2018」屠龙勇士(EXCRT)
「NOI2018」屠龙勇士(EXCRT) 终于把传说中 \(NOI2018D2\) 的签到题写掉了... 开始我还没读懂题目...而且这题细节巨麻烦...(可能对我而言) 首先我们要转换一下,每次的 ...
- LOJ #2721. 「NOI2018」屠龙勇士(set + exgcd)
题意 LOJ #2721. 「NOI2018」屠龙勇士 题解 首先假设每条龙都可以打死,每次拿到的剑攻击力为 \(ATK\) . 这个需要支持每次插入一个数,查找比一个 \(\le\) 数最大的数(或 ...
- POJ1061 青蛙的约会 和 LOJ2721 「NOI2018」屠龙勇士
青蛙的约会 Language:Default 青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 133470 Accep ...
- 「NOI2018」屠龙勇士 解题报告
「NOI2018」屠龙勇士 首先对于每个龙用哪个剑砍,我们可以用set随便模拟一下得到. 然后求出拿这个剑砍这条龙的答案 \[ atk_ix-p_iy=a_i \] 其中\(atk_i\)是砍第\(i ...
- LOJ 2721 「NOI2018」屠龙勇士——扩展中国剩余定理
题目:https://loj.ac/problem/2721 1.注意别一输入 p[ i ] 就 a[ i ] %= p[ i ] ,因为在 multiset 里找的时候还需要真实值. 2.注意用 m ...
- 【BZOJ5418】【NOI2018】屠龙勇士(数论,exgcd)
[NOI2018]屠龙勇士(数论,exgcd) 题面 洛谷 题解 考场上半个小时就会做了,一个小时就写完了.. 然后发现没过样例,结果大力调发现中间值爆\(longlong\)了,然后就没管了.. 然 ...
- 「NOI2018」你的名字
「NOI2018」你的名字 题目描述 小A 被选为了\(ION2018\) 的出题人,他精心准备了一道质量十分高的题目,且已经 把除了题目命名以外的工作都做好了. 由于\(ION\) 已经举办了很多届 ...
随机推荐
- react-router v4 实现路由拦截的一种思路(待验证)
出处:https://segmentfault.com/q/1010000010905474
- 高阶组件 Higher-order Components (HOC) 知识点
官方介绍地址:https://reactjs.org/docs/higher-order-components.html
- 利用pipeline批量插入数据到redis
在推荐系统中,推荐候选集格式一般是,itemid itemid_list.要把itemid作为key,推荐列表作为value批量插入到redis. 比如文件cf.data为: cf_763500210 ...
- linux 自总结常用命令(centos系统)
查看apache2的命令 httpd -V 其中HTTPD_ROOT和SERVER_CONFIG_FILE 就可以确定httpd.conf(Apache配置文件)的路径了 apache启动.停止.重 ...
- Elastic Story(一)
关于_all 当索引一个文档的时候,Elasticsearch 取出所有字段的值拼接成一个大的字符串,作为 _all 字段进行索引.例如,当索引这个文档时: { "tweet": ...
- 商品和订单中使用MQ
一.将Product服务增加到配置中心 1.添加引用 <dependency> <groupId>org.springframework.cloud</groupId&g ...
- tornado输入-get_query_argument()等 笔记
最外面的代码结构 import tornado.web import tornado.ioloop import tornado.options import tornado.httpserver f ...
- 深入理解ASP.NET MVC(6)
系列目录 Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, acti ...
- Remote error: VAR and OUT arguments must match parameter type exactly'
在XE10中 downloadfile(filename: string; out FileStream: TStream; out FileSize: int64)是没有问题的,升级到delphi ...
- Java通过webservice接口获取天气信息
通过SOAP请求的方式获取天气信息并解析返回的XML文件. 参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx import java.io ...