传送门


思路

首先可以发现打每条龙的攻击值显然是可以提前算出来的,拿multiset模拟一下即可。

一般情况

可以搞出这么一些式子:

\[atk_i\times x=a_i(\text{mod}\ p_i)
\]

简单处理一下就变成这样:

\[atk_i\times x +p_i \times y=a_i
\]

显然可以扩欧搞出一组特解\((x',y')\),那么就有

\[x=x'(\text{mod}\ \frac{p_i}{\gcd(atk_i,a_i)})
\]

然后扩展中国剩余定理合并一下即可。

特殊情况

\(a_i>p_i\)时显然不能用上面的方法,但数据保证此时\(p_i\)等于1,那么直接输出\(max\{\lceil \frac{a_i}{atk_i}\rceil\}\)即可。

若全部\(a_i=p_i\)时会解出\(x=0\),此时要输出\(lcm\{a_i\}\)。

\(p_i|atk_i\)时一般是无解的,但如果同时满足\(p_i=a_i\)那么这个方程没有用,可以换成\(x=0(\text{mod}\ 1)\)。

然后就做完啦。


代码

  1. #include<bits/stdc++.h>
  2. clock_t t=clock();
  3. namespace my_std{
  4. using namespace std;
  5. #define pii pair<int,int>
  6. #define fir first
  7. #define sec second
  8. #define MP make_pair
  9. #define rep(i,x,y) for (int i=(x);i<=(y);i++)
  10. #define drep(i,x,y) for (int i=(x);i>=(y);i--)
  11. #define go(x) for (int i=head[x];i;i=edge[i].nxt)
  12. #define templ template<typename T>
  13. #define sz 110010
  14. typedef long long ll;
  15. typedef double db;
  16. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  17. templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
  18. templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
  19. templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
  20. templ inline void read(T& t)
  21. {
  22. t=0;char f=0,ch=getchar();double d=0.1;
  23. while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
  24. while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
  25. if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
  26. t=(f?-t:t);
  27. }
  28. template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
  29. char __sr[1<<21],__z[20];int __C=-1,__zz=0;
  30. inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
  31. inline void print(register int x)
  32. {
  33. if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
  34. while(__z[++__zz]=x%10+48,x/=10);
  35. while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
  36. }
  37. void file()
  38. {
  39. #ifndef ONLINE_JUDGE
  40. freopen("a.in","r",stdin);
  41. #endif
  42. }
  43. inline void chktime()
  44. {
  45. #ifndef ONLINE_JUDGE
  46. cout<<(clock()-t)/1000.0<<'\n';
  47. #endif
  48. }
  49. #ifdef mod
  50. ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
  51. ll inv(ll x){return ksm(x,mod-2);}
  52. #else
  53. ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
  54. #endif
  55. // inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
  56. }
  57. using namespace my_std;
  58. int n,m;
  59. ll a[sz],p[sz];
  60. ll _[sz];
  61. ll atk[sz];
  62. void work1()
  63. {
  64. ll ans=0;
  65. rep(i,1,n) chkmax(ans,(a[i]+atk[i]-1)/atk[i]);
  66. printf("%lld\n",ans);
  67. }
  68. struct hh{ll a,p;}s[sz]; /*x%p==a*/
  69. ll exgcd(ll a,ll b,ll &x,ll &y){if (!b) return x=1,y=0,a;ll ret=exgcd(b,a%b,y,x);y-=a/b*x;return ret;}
  70. ll mul(ll x,ll y,const ll &mod){x=(x%mod+mod)%mod,y=(y%mod+mod)%mod;ll ret=0;for (;y;y>>=1,x=(x+x)%mod) if (y&1) ret=(ret+x)%mod;return ret;}
  71. void excrt(ll &ret,ll &mod)
  72. {
  73. ll a=0,p=1,x,y;
  74. rep(i,1,n)
  75. {
  76. ll g=exgcd(p,s[i].p,x,y),s1=p/g,s2=s[i].p/g;
  77. if ((s[i].a-a)%g!=0) return (void)(ret=-1,mod=-1,puts("-1"));
  78. exgcd(s1,s2,x,y);x=(x%s2+s2)%s2;
  79. ll _p=p/g*s[i].p;
  80. a=(a+mul(p,mul(x,(s[i].a-a)/g,s2),_p))%_p;
  81. p=_p;
  82. }
  83. ret=a,mod=p;
  84. }
  85. void work2()
  86. {
  87. rep(i,1,n)
  88. {
  89. if (atk[i]%p[i]==0)
  90. {
  91. if (a[i]==p[i]) s[i]=(hh){0,1};
  92. else return (void)(puts("-1"));
  93. }
  94. ll x,y;
  95. ll gcd=exgcd(atk[i],p[i],x,y);
  96. if (a[i]%gcd!=0) return (void)(puts("-1"));
  97. ll mod=p[i]/gcd;
  98. x=mul(x,a[i]/gcd,mod);
  99. s[i]=(hh){x,mod};
  100. }
  101. ll x,mod;
  102. excrt(x,mod);
  103. if (x==-1&&mod==-1) return;
  104. if (!x)
  105. {
  106. ll ans=1,_,__;
  107. rep(i,1,n) ans=ans/exgcd(ans,a[i],_,__)*a[i];
  108. printf("%lld\n",ans);
  109. }
  110. else printf("%lld\n",x);
  111. }
  112. void work()
  113. {
  114. read(n,m);
  115. rep(i,1,n) read(a[i]);
  116. rep(i,1,n) read(p[i]);
  117. rep(i,1,n) read(_[i]);
  118. multiset<ll>s;
  119. int x;
  120. rep(i,1,m) read(x),s.insert(x);
  121. rep(i,1,n) { auto it=s.upper_bound(a[i]); if (it!=s.begin()) --it; atk[i]=*it; s.erase(it); s.insert(_[i]); }
  122. bool flg=1;
  123. rep(i,1,n) flg&=(p[i]==1);
  124. if (flg) work1();
  125. else work2();
  126. }
  127. int main()
  128. {
  129. file();
  130. int T;read(T);
  131. while (T--) work();
  132. return 0;
  133. }

洛谷P4774 [NOI2018]屠龙勇士 [扩欧,中国剩余定理]的更多相关文章

  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. LOJ2721 [NOI2018] 屠龙勇士 【扩展中国剩余定理】

    好久没写了,写一篇凑个数. 题目分析: 这题不难想,讲一下中国剩余定理怎么扩展. 考虑$$\left\{\begin{matrix}x \equiv a\pmod{b}\\ x \equiv c\pm ...

  6. P4774 [NOI2018]屠龙勇士

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

  7. luogu P4774 [NOI2018]屠龙勇士

    传送门 这题真的是送温暖啊qwq,而且最重要的是yyb巨佬在Day2前几天正好学了crt,还写了博客 然而我都没仔细看,结果我就同步赛打铁了QAQ 我们可以先根据题意,使用set维护,求出每次的攻击力 ...

  8. 【洛谷 P4777】 【模板】扩展中国剩余定理(EXCRT)

    注意一下:: 题目是 \[x≡b_i\pmod {a_i}\] 我总是习惯性的把a和b交换位置,调了好久没调出来,\(qwq\). 本题解是按照 \[x≡a_i\pmod {b_i}\] 讲述的,请注 ...

  9. BZOJ5418[Noi2018]屠龙勇士——exgcd+扩展CRT+set

    题目链接: [Noi2018]屠龙勇士 题目大意:有$n$条龙和初始$m$个武器,每个武器有一个攻击力$t_{i}$,每条龙有一个初始血量$a_{i}$和一个回复值$p_{i}$(即只要血量为负数就一 ...

随机推荐

  1. TOMCAT原理详解及请求过程(转载)

    转自https://www.cnblogs.com/hggen/p/6264475.html TOMCAT原理详解及请求过程 Tomcat: Tomcat是一个JSP/Servlet容器.其作为Ser ...

  2. c提高第六次课 文件读取

    一.基本概念1.文件分类 普通文件:存放在硬盘中的文件 设备文件:屏幕.键盘等特殊文件 文本文件:ASCII文件,每个字节存放一个字符的ASCII码,打开文件看到的是文本信息 二进制文件:数据按其在内 ...

  3. 关于mysql 自定义@row的使用

    应用场景:在对成绩或者积分排名时,往往需要显示排名; 成绩排名:相同分数的人,名次相同 Select s.Score, case when @rowtotal = s.Score then cast( ...

  4. python第十三天,函数的嵌套定义,global,nonlocal关键字的使用,闭包及闭包的运算场景,装饰器

    今日内容 1. 函数的嵌套定义 2.global,nonlocal关键字 3.闭包及闭包的运用场景 4.装饰器 函数的嵌套定义 1. 概念:在一个函数内部定义另一个函数 2 .为什么要有函数的嵌套定义 ...

  5. python json相关问题

    关于字典和字符转化问题. 1.使用eval()(一般不推荐) eval is evil. 这种方法可能直接执行恶意代码. 2.使用json.loads()(极度推荐) 这种方法是推荐的,但是要注意细节 ...

  6. Linux命令_sed_2

    2.替换(将包含"xxx"的行中的"yyy"替换成"zzz") 现有文件“replace_specified_contained_line” ...

  7. sql连接查询中的分类

    sql连接查询中的分类 1.内连接(结果不保留表中未对应的数据) 1.1等值连接:关联条件的运算符是用等号来连接的. 1.2不等值连接:连接条件是出等号之外的操作符 1.3自然连接:特殊的等值连接,在 ...

  8. python2和python3的区别

    python2和python3的区别 参考链接:http://www.runoob.com/python/python-2x-3x.html 1.源码上的区别 python2 python3 源码不规 ...

  9. Redux thunk中间件

    redux-thunk https://github.com/reduxjs/redux-thunk Why Do I Need This? Thunks are the recommended mi ...

  10. centos备份多个数据库

    #/bin/bash# the backup dateDATE=`date +%Y%m%d%H%M`#backup pathBACKUP_PATH=/home/backup/mysqldata#get ...