啊我死了。

肝了三天的毒瘤题......他们考场怎么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. JavaScript charAt() 方法

    <script> var str="abcdef"; alert(str[0]); //a,高版本浏览器兼容 alert(str.charAt(0)); //a,兼容所 ...

  2. github上测试服出现bug,如何回滚并获得合并之前的分支

    使用场景: 当我们提交了一个pr,但是该pr合并之后,经过在测试测试有问题,需要回滚.这个时候主master代码将会被回滚到提交你的pr之前的代码.而你的pr由于已经被合并过了,所以无法继续提交. 这 ...

  3. k8s使用Glusterfs动态生成pv

    一.环境介绍 [root@k8s-m ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4 ...

  4. Postman & API

    Postman & API https://www.getpostman.com/ https://www.getpostman.com/downloads/ Postman Canary h ...

  5. video maker & video tutorials

    video maker & video tutorials 视频课程制作工具 https://ke.qq.com/agency/personal/intro.html 成为网络老师 https ...

  6. 一、Dev

    一.获取选中的表格 // MessageBox.Show(gridview_Parent.GetFocusedDataRow()["series"].ToString());//获 ...

  7. codeforces492C

    Vanya and Exams CodeForces - 492C Vanya wants to pass n exams and get the academic scholarship. He w ...

  8. Nginx 滑动窗口与缓冲区

    L:125

  9. linux下如何安装mysql和redis

    linux下如何安装mysql(mariadb) linux下如何安装软件? 1. yum安装软件也得注意,一个是配置yum源 1.我们当前的是阿里云的yum源(下载速度特别快) 通过 yum ins ...

  10. http协议状态码大全

    100 Continue:初始的请求已经接受,客户应当继续发送请求的其余部分.  101 Switching Protocols:服务器将遵从客户的请求转换到另外一种协议.  200 OK:一切正常, ...