Description
Little X, Little y and Little Z are playing checkers when Little Y is annoyed. So he wants the chessboard much bigger. Although Little Z insists the original version, Little X stands by Little Y. After they enlarge the chessboard, the chessboard turns to a infinite line.
The chessboard is like the number Axes now, with each of the integer point able-to-hold a checker. At initial status there is three checkers on three different integer points, and through the game there always is three Checkers. Every time, they can choose a checker a to jump across a pivot checker B to a new position (but the distance between old a and b equals to new A and B, and there should is no other checkers except B in the range [old A, new A]).
After playing for a while, they wonder whether an given status A,b,c can is transferred to X, Y, Z, obeying the rules. Since The checkers is considered the same, it's unnecessary for a must jump to X.
Input Format
The first line is a,b,c. The second line is x, Y, Z. They is all integers in range ( -10^9, 10^9) and the both status is valid.
Output Format
The first line was YES or NO, showing whether the transfer can be achieved. If it is YES and then output the least steps on the second line.
Sample Input
1 2 30) 3 5
Sample Output
YES2
A good question of thinking
Oneself in the draft paper to get started;
Set A<b<c
The next step is 2a-b a C or a C 2c-b;
If b-a<c-b; previous step is B 2b-a C
If b-a>c-b; the previous step is a 2b-c b;
It can be found that the middle point outward expands a B c distance increases;
conversely decrease;
The picture is a binary tree!!!!;
The next step is to extend the son; previous visit to Father;
Then consider the root node problem;
For each state it keeps going up a B c distance will decrease;
Until the b-a=c-b, the fast root node as long as the use of the Division is good;
So the first question of the topic is solved; just look for the root node on the line;
A second question;
Because this problem is built +LCA trouble and the data is also larger space is not allowed; then we can use a dichotomy +lca
First, the starting and ending states are adjusted to the same height;
Then the two points up the height of the upper walk to determine whether the same is OK;
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio>using namespace Std;struct state{int x,y,z,d;}; State St,ed;int n,i,j,l,k,m,r;void Sor (state &now) {if (NOW.X>NOW.Y) swap (NOW.X,NOW.Y), if (now.y>now.z) swap ( NOW.Y,NOW.Z); if (NOW.X>NOW.Y) swap (NOW.X,NOW.Y);} State root (state &now) {int q=-1,p=1,r;state pre=now;while (q!=p) {q=pre.y-pre.x;p=pre.z-pre.y;if (p<q) {r= (q-1) /p;pre.y-=r*p;pre.z-=r*p;} Else{r= (p-1)/q;pre.x+=r*q;pre.y+=r*q;} Now.d+=r;sor (pre);} Pre.d=0;return Pre;} BOOL Equr (state A,state b) {return a.x==b.x&&a.y==b.y&&a.z==b.z? True:false;}; State Now,int num) {int p=-1,q=1,r;while (num&&q!=p) {p=now.y-now.x;q=now.z-now.y;if (q<p) {r=min ( Num, (p-1)/q); now.y-=q*r;now.z-=q*r;} Else{r=min (num, (q-1)/p); now.x+=p*r;now.y+=p*r;} Num-=r;sor (now);} return now;} BOOL Check (int mid) {State nex1=st,nex2=ed;nex1=up (NEX1,MID+ST.D-ED.D); Nex2=up (Nex2,mid); if (Equr (NEX1,NEX2)) return True;return false;} int main () {//freopen ("Xx.in "," R ", stdin), while (scanf ("%d%d%d%d%d%d ", &st.x,&st.y,&st.z,&ed.x,&ed.y,&ed.z) ==6) { Sor (ST); Sor (ed); St.d=ed.d=0;if (!equr (Root (ST), Root (ed)) printf ("no\n"); Else{int Mid,ans;l=0;r=max (ST.D,ED.D); if (ST.D<ED.D) Swap (st,ed), while (l<=r) {mid= (l+r) >>1;if (check (mid)) R=mid-1,ans=mid;else l=mid+1;} printf ("yes\n%d\n", St.d-ed.d+ans*2);}}}
HDU 3830 Checkers