啊我死了。

肝了三天的毒瘤题......他们考场怎么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. 下拉框、下拉控件之Select2

    一.Select2的功能简介 select2插件给我们带来了更加友好的交互方式,比如查询控件展开后可通过关键字进行检索 例如: Select2也可以选择带查询控件的选择框... Select2也可以选 ...

  2. 2.请介绍一下List和ArrayList的区别,ArrayList和HashSet区别

    第一问: List是接口,ArrayList实现了List接口. 第二问: ArrayList实现了List接口,HashSet实现了Set接口,List和Set都是继承Collection接口. A ...

  3. 剑指offer(16)栈的压入、弹出序列

    题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈 ...

  4. GitHub创建仓库,并与git本地仓库关联

    登录后头像右上角点击: 起名再create 后 会跳转到下面页面: 先在git上复制执行第一条指令,创建一个readme文档 然后再用第二条初始化仓库 第三步将readme文档添加至暂存区 然后提交一 ...

  5. C# 中那些常用的工具类(Utility Class)(三)

    今天来接着写这个系列的文章,这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候 ...

  6. CSS3圆角详解(border-radius)

    1.CSS3圆角的优点 传统的圆角生成方案,必须使用多张图片作为背景图案.CSS3的出现,使得我们再也不必浪费时间去制作这些图片了,而且还有其他多个优点: 减少维护的工作量.图片文件的生成.更新.编写 ...

  7. 【python练习题】程序7

    #题目:将一个列表的数据复制到另一个列表中. l = [1,2,3,4,5,6,7,8] m = [] m = l[:] print (m)

  8. java web 开发入门 --- tomcat/servlet/jsp

    在做java web 开发时,要先安装tomcat.它是一个web服务器,也叫web容器,我们把写好的jsp, html页面放到它里面,然后启动它,就可以用浏览器访问这些页面,地址栏中输入localh ...

  9. MySQL各版本解释和下载

    MySQL 的官网下载地址:http://www.mysql.com/downloads/ 个人理解: 1.不要再纠结是否是5.1还是5.5.5.6.5.7这些,一般选择时不要选择太新,选择5.1或者 ...

  10. js判断一个元素是否在数组中

    js判断一个元素是否在数组中 var arr = ['a','s','d','f']; console.info(isInArray(arr,'a'));//循环的方式 function isInAr ...