啊我死了。

肝了三天的毒瘤题......他们考场怎么A的啊。


大意:

给你若干个形如 的方程组,求最小整数解。

嗯......exCRT的变式。

考虑把前面的系数化掉:

然后就是exCRT板子了。

我TM想要自己写出一个板子,然后GG了......

我快疯了。

然后抄了板子(滑稽)

注意细节,快速幂/乘的时候,b位置不要传负数。

 #include <cstdio>
#include <set>
#include <algorithm> typedef long long LL;
const int N = ; std::multiset<LL> S;
std::multiset<LL>::iterator it;
int n, m, Time, vis[N];
LL a[N], p[N], awd[N], use[N]; LL Val;
LL gcd(LL a, LL b) {
if(!b) {
return a;
}
return gcd(b, a % b);
}
inline LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
void exgcd(LL a, LL b, LL &x, LL &y) {
if(!b) {
x = Val / a;
y = ;
return;
}
exgcd(b, a % b, x, y);
std::swap(x, y);
y -= (a / b) * x;
return;
}
inline LL mod(LL a, LL c) {
while(a >= c) {
a -= c;
}
while(a < ) {
a += c;
}
return a;
}
inline LL mul(LL a, LL b, LL c) {
LL ans = ;
a %= c;
b %= c;
while(b) {
if(b & ) {
ans = mod(ans + a, c);
}
a = mod(a << , c);
b = b >> ;
}
return ans;
}
inline LL qpow(LL a, LL b, LL c) {
LL ans = ;
a %= c;
while(b) {
if(b & ) {
ans = mul(ans, a, c);
}
a = mul(a, a, c);
b = b >> ;
}
return ans;
}
inline LL abs(LL a) {
return a < ? -a : a;
}
inline LL inv(LL a, LL c) {
LL x, y;
Val = ;
exgcd(a, c, x, y);
return mod(x, c);
}
//----math---- inline void solve_p1() {
LL ans = ;
for(int i = ; i <= n; i++) {
LL temp = (a[i] - ) / use[i] + ;
ans = std::max(ans, temp);
}
printf("%lld\n", ans);
return;
} inline void solve_n1() {
LL g = gcd(use[], p[]);
if(a[] % g) {
puts("-1");
return;
}
LL x, y;
Val = a[];
exgcd(use[], p[], x, y);
// use[1] * x + p[1] * y == a[1]
LL gap = (use[] / g);
//printf("gap = %lld y = %lld \n", gap, y);
LL yy = (y % gap - gap) % gap;
//printf("yy = %lld \n", yy);
LL dt = (y - yy) / gap;
//printf("dt = %lld \n", dt);
x += dt * (p[] / g);
//printf("x = %lld \n", x);
printf("%lld\n", x);
return;
} inline void solve_a() {
LL large = ;
for(int i = ; i <= n; i++) {
large = std::max(large, (a[i] - ) / use[i] + );
LL g = gcd(use[i], p[i]);
if(a[i] % g) {
puts("-1");
return;
}
if(p[i] == ) {
vis[i] = Time;
continue;
}
//printf("%lld %lld %lld \n", use[i], p[i], a[i]);
a[i] /= g;
p[i] /= g;
use[i] /= g;
use[i] %= p[i];
a[i] %= p[i];
if(!use[i]) {
if(!a[i]) {
vis[i] = Time;
continue;
}
else {
puts("-1");
//printf("%lld %lld %lld \n", use[i], p[i], a[i]);
return;
}
}
LL Inv, temp;
Val = ;
exgcd(use[i], p[i], Inv, temp);
Inv = mod(Inv, p[i]);
//printf("Inv = %lld \n", Inv);
a[i] = mul(Inv, a[i], p[i]);
}
// x = a[i] (mod p[i]) /*for(int i = 1; i <= n; i++) {
printf("x === %lld mod %lld \n", a[i], p[i]);
}*/ bool fd = ;
LL A = , P = ;
for(int i = ; i <= n; i++) {
//printf("%d \n", i);
if(vis[i] == Time) {
continue;
}
if(!fd) {
A = a[i];
P = p[i];
fd = ;
continue;
}
// merge i
LL x, y;
LL C = ((a[i] - A) % p[i] + p[i]) % p[i];
LL g = gcd(P, p[i]);
if(C % g) {
puts("-1");
return;
}
Val = g;
exgcd(P, p[i], x, y);
x = mul(x, C / g, P / g * p[i]);
A += mul(x, P, P / g * p[i]);
P *= p[i] / g;
A = (A + P) % P;
}
// x = A (mod P)
// 1 * x + y * P = A LL x, y;
Val = A;
exgcd(, P, x, y);
x = mod(x, P);
if(x < large) {
x += P * ((large - x - ) / P + );
}
printf("%lld\n", x);
return;
} inline void solve() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%lld", &a[i]);
}
for(int i = ; i <= n; i++) {
scanf("%lld", &p[i]);
}
for(int i = ; i <= n; i++) {
scanf("%lld", &awd[i]);
}
for(int i = ; i <= m; i++) {
LL x;
scanf("%lld", &x);
S.insert(x);
}
for(int i = ; i <= n; i++) {
it = S.upper_bound(a[i]);
if(it != S.begin()) {
it--;
}
use[i] = (*it);
S.erase(it);
S.insert(awd[i]);
} //-------------------- p = 1 30pts -------
bool f = ;
for(int i = ; i <= n; i++) {
if(p[i] > ) {
f = ;
break;
}
}
if(f) {
solve_p1();
return;
} //-------------------- n = 1 30pts -------
if(n == ) {
solve_n1();
return;
} solve_a();
return;
} int main() { int T;
scanf("%d", &T);
for(Time = ; Time <= T; Time++) {
solve();
if(Time < T) {
S.clear();
}
} return ;
}

AC代码

一共提交了14次.......

洛谷P4774 屠龙勇士的更多相关文章

  1. [洛谷P4774] [NOI2018]屠龙勇士

    洛谷题目链接:[NOI2018]屠龙勇士 因为markdown复制过来有点炸格式,所以看题目请戳上面. 题解: 因为杀死一条龙的条件是在攻击\(x\)次,龙恢复\(y\)次血量\((y\in N^{* ...

  2. 洛谷 P4774 [NOI2018] 屠龙勇士

    链接:P4774 前言: 交了18遍最后发现是多组数据没清空/ll 题意: 其实就是个扩中. 分析过程: 首先发现根据题目描述的选择剑的方式,每条龙对应的剑都是固定的,有查询前驱,后继(在该数不存在前 ...

  3. (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)

    前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...

  4. 洛谷P4774 BZOJ5418 LOJ2721 [NOI2018]屠龙勇士(扩展中国剩余定理)

    题目链接: 洛谷 BZOJ LOJ 题目大意:这么长的题面,就饶了我吧emmm 这题第一眼看上去没法列出同余方程组.为什么?好像不知道用哪把剑杀哪条龙…… 仔细一看,要按顺序杀龙,所以获得的剑出现的顺 ...

  5. 洛谷P4774 [NOI2018]屠龙勇士 [扩欧,中国剩余定理]

    传送门 思路 首先可以发现打每条龙的攻击值显然是可以提前算出来的,拿multiset模拟一下即可. 一般情况 可以搞出这么一些式子: \[ atk_i\times x=a_i(\text{mod}\ ...

  6. 洛谷 P4774 / loj 2721 [NOI2018] 屠龙勇士 题解【同余】【exgcd】【CRT】

    推导过程存在漏洞+exCRT板子没打熟于是期望得分÷实际得分=∞? 题目描述 小 D 最近在网上发现了一款小游戏.游戏的规则如下: 游戏的目标是按照编号 \(1\sim n​\) 顺序杀掉 \(n​\ ...

  7. 扩展中国剩余定理学习笔记+模板(洛谷P4777)

    题目链接: 洛谷 题目大意:求同余方程组 $x\equiv b_i(mod\ a_i)$ 的最小正整数解. $1\leq n\leq 10^5,1\leq a_i\leq 10^{12},0\leq ...

  8. NOI2018Day2T1 屠龙勇士 set 扩展欧几里德 中国剩余定理

    原文链接https://www.cnblogs.com/zhouzhendong/p/NOI2018Day2T1.html 题目传送门 - 洛谷P4774 题意 题解 首先我们仔细看一看样例可以发现如 ...

  9. P4774 [NOI2018]屠龙勇士

    P4774 [NOI2018]屠龙勇士 先平衡树跑出打每条龙的atk t[] 然后每条龙有\(xt \equiv a[i](\text{mod }p[i])\) 就是\(xt+kp[i]=a[i]\) ...

随机推荐

  1. idea 最新破解亲测有效

    选择License serverLicense server address: 填:http://active.chinapyg.com/ (不能用了) 2018-5-7更新 新增一个 http:// ...

  2. java 中Excel的导入导出

    部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字  的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...

  3. python数学第二天【泰勒展开式】

    1. 泰勒展开式 推论1: 泰勒展开式的应用 推论2: 推论3:

  4. 老男孩python学习自修【第三天】列表用法

    列表的使用: list.append(value) 向列表增加元素 list.insert(index, value) 向列表指定元素插入元素 list.extend(newlist) 用新的列表扩展 ...

  5. RuntimeError: cryptography requires setuptools 18.5 or newer, please upgrade to a newer version of setuptool

    setuptool 太老了,更新下: pip install --upgrade setuptools

  6. solr配置ik中文分词(二)

    上一篇文章主要介绍了solr的安装与配置,这篇文章主要记录如何使用ik分词器对中文进行分词. 步骤: 1.下载ik分词jar包:ik-analyzer-solr5-5.x.jar. 2.将下载的jar ...

  7. python numpy笔记(重要)

    1.np.array 的shape (2,)与(2,1)含义 ndarray.shape:数组的维度.为一个表示数组在每个维度上大小的整数元组.例如二维数组中,表示数组的“行数”和“列数”. ndar ...

  8. DotNetty 实现 Modbus TCP 系列 (三) Codecs & Handler

    本文已收录至:开源 DotNetty 实现的 Modbus TCP/IP 协议 DotNetty 作为一个半成品,我们不需要关注细节的实现,只需要关注自己的业务即可,所以最主要的就是处理 Codecs ...

  9. Vue混合mixins

    前面的话 本文将详细介绍Vue混合mixins 概述 混合 (mixins) 是一种分发 Vue 组件中可复用功能的非常灵活的方式.混合对象可以包含任意组件选项.以组件使用混合对象时,所有混合对象的选 ...

  10. Codeforces Round #540 Div. 3 F2

    考虑将每种颜色构成的极小连通块缩点,然后直接跑树形dp即可,即f[i][0/1]表示子树内是否有颜色向上延伸时删边的方案数.dp时需要去除某点的贡献,最好用前后缀积的做法而不是求逆. 至于如何缩点,假 ...