Rokua P4774 BZOJ5418 LOJ2721 [NOI2018] Dragon Slayer (extended Chinese remainder theorem)

Source: Internet
Author: User
Tags gcd

Topic Links:

Rokua

Bzoj

Loj

The topic is so long, let me be spared . emmm

At first glance, it seems impossible to list the congruence equations. Why? It seems that I don't know which sword to kill which dragon ...

Look carefully, to kill the dragon in order , so the order of the obtained sword is also fixed.

Then if you can kill all the dragons, you can simulate which sword killed the dragon.

(The maximum value for all the numbers except the $n, m$, is $v $)

$O (NM) $?

No, I found that the restriction of the sword here is actually to give an upper bound, to use Lower_bound.

Insert and don't be too violent. What do we think? handwriting Balance Tree multiset!

This part of the complexity is $O (n\log m) $.

The next is a congruence equation group: $atk _ix\equiv hp_i (\operatorname{mod}\ rec_i) $.

($atk _i$ represents the attack on the first $i $ dragon, $HP _i$ represents the amount of blood for the $i $ dragon, $rec _i$ represents the recovery capability of the $i $ dragon)

But it's not as simple as that! There are some magical situations ... To be a special judge!

In the examination room, I can't believe I'm going to judge anything.

First of all you should know that $x \equiv a_i (\operatorname{mod}\ b_i) $ equivalent to $x \equiv a_i\ \operatorname{mod}\ b_i (\operatorname{mod}\ b_i) $.

But not in this problem! You can't cut a dragon's blood volume out of thin air!

So what happens when $hp _i>rec_i$ (when the feature 1 in the topic description is not satisfied)?

We found that the points that do not meet the feature 1 are satisfied $rec _i=1$ ... That is, as long as the amount of his blood hit less than 0 won!

At this point the answer is $\max\limits_{1\le i\le n} (\lceil\frac{hp_i}{atk_i}\rceil) $.

In addition, the solution we solved at all $HP _i=rec_i$ was 0! What kind of magical qigong?

So this is the answer to all $\lceil\frac{hp_i}{atk_i}\rceil$ 's $\operatorname{lcm}$.

This concludes all the special sentences.

Our general Excrt Equation Group form is $x \equiv a_i (\operatorname{mod}\ b_i) $.

But there's a nasty $atk _i$!. More annoying is that there may be no inverse, not directly in addition to the past!

Let's take a look at how to deform:

$ATK _ix\equiv hp_i (\operatorname{mod}\ rec_i) $

$ATK _ix+rec_iy=hp_i$

At this point, you can exgcd the minimum positive integer solution of $x $ $x _0$.

According to a theorem (what is the theorem?) The general solution can be $x =x_0+\frac{rec_i}{\gcd (atk_i,rec_i)}k (k\in n^+) $.

So it became the $x \equiv x_0 (\operatorname{mod}\ \frac{rec_i}{\gcd{atk_i},rec_i}) $.

This part of the complexity is $O (N\log v) $.

The next is the bare excrt!

This part of the complexity is $O (N\log v) $.

Wait a minute, there is a hint not to see:

The intermediate result you use may be very large, note the type of variable that holds the intermediate result.

Suddenly panic

Calmly analysis, we in various algorithms in the modulus can be more than the int range, if a multiply ...

That...... Only on the turtle speed ride. The complexity of time is unchanged, but constant ...

Fortunately, it's not Wys's problem, otherwise the time limit is 1s.

All right, all finished, on the code.

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 Const intmaxn=100010;5Multiset<ll> MTS;//Analog Sword-holding6 intt,n,m;7 BOOLSpec,exist,same;8ll HP[MAXN],REC[MAXN],A[MAXN],B[MAXN];//A, b indicates that the final equations are x=a_i (mod\ b_i)9 intAWARD[MAXN],ATK[MAXN];//Award is the Sword of reward.Tenll Qmul (ll a,ll b,ll MoD) {//* Speed Multiplier Onell ans=0; A      for(; b;b>>=1, A= (a+a)%mod)if(b&1) ans= (ans+a)%MoD; -     returnans; - } thell EXGCD (ll a,ll b,ll &x,ll &y) {//Templates -     if(!B) {x=1; y=0;returnA;} -ll D=EXGCD (b,a%b,y,x); y-=a/b*x;returnD; - } +ll GCD (ll A,ll b) {//(Don't ask me why I have this) -     if(!B)returnA; +     returnGCD (b,a%b); A } at intMain () { -scanf"%d",&t); -      while(t--){ -Spec=false; same=exist=true;//Spec is Rec=1,same is hp=rec,exist is there is no solution - mts.clear (); -scanf"%d%d",&n,&m); in          for(intI=1; i<=n;i++) scanf ("%lld", hp+i); -          for(intI=1; i<=n;i++) {scanf ("%lld", rec+i);if(Rec[i]true;}//rec to          for(intI=1; i<=n;i++) scanf ("%d", award+i); +          for(intI=1; i<=m;i++){ -             intx; thescanf"%d",&x); *Mts.insert (x);//all in . $         }Panax Notoginseng          for(intI=1; i<=n;i++){ -Multiset<ll>:: iterator it; the             if(Hp[i]<*mts.begin ()) It=mts.begin ();//If there is no sword smaller than the amount of blood, use the first (smallest) +             ElseIt=mts.upper_bound (Hp[i]), it--;//Otherwise, use the first one greater than 1 (that is, the last one is less than or equal to it) Aatk[i]=*it; the Mts.erase (it); +Mts.insert (Award[i]);//take it and pick it. -         } $         if(spec) {//Special Award Hp>rec $ll ans=0; -              for(intI=1; i<=n;i++) Ans=max (Ans,ll (Ceil (1.0*hp[i]/Atk[i] )); -printf"%lld\n", ans); the             Continue; -         }Wuyi          for(intI=1; i<=n;i++){ the             if(!atk[i]) {Puts ("-1"); exist=false; Break;}//can't attack? No solution -             if(Hp[i]!=rec[i]) same=false;//Hp=rec? Wull X,Y,D=EXGCD (atk[i],rec[i],x,y);//Solution x0 -             if(hp[i]%d) {Puts ("-1"); exist=false; Break;}//No solution AboutX= (X+rec[i])%rec[i];b[i]=rec[i]/d;a[i]=qmul (X,hp[i]/d,b[i]);//really x0 launched B=REC/GCD (ATK,REC), a=x0 $         } -         if(same) {//Special Award Hp=rec -ll Ans=ll (Ceil (1.0*hp[1]/atk[1])); -              for(intI=2; i<=n;i++){ All X=ceil (1.0*hp[i]/atk[i]), d=gcd (ans,x); +ans=ans/d*x; the             } -printf"%lld\n", ans);Continue; $         } the         if(!exist)Continue; thell ans=a[1],mod=b[1];//start EXCRT (The following template, not explained, can go to my other blog to see) the          for(intI=2; i<=n;i++){ thell X,Y,D=EXGCD (mod,b[i],x,y), r= ((A[i]-ans)%b[i]+b[i])%b[i],tmp=mod/d*B[i]; -             if(r%d) {Puts ("-1"); exist=false; Break;} inX= (X+b[i])%b[i];x=qmul (x,r/d,b[i]); theAns= (Qmul (mod,x,tmp) +ans)%tmp; theMod=tmp; About         } the         if(!exist)Continue; theprintf"%lld\n", ans);//Finally, no. the     } +}
Noi2018d2t1_dragon (EXCRT)

Finally, ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.