luogu P1852 [National Team] jumping chess

luogu

Direct operationIt is impossibleConsidering discover some properties can be found if each transition pieces are on both sides, then a maximum of only one program, then put thus obtained was referred to as the father of the current state of the state, from a state to do so will end. then the final state of the same operation on both sides only if the two state reached, then it can be interchangeable

If the number of steps, as if this one tree, then the tree is a distance problem, it requires a deep depth of these two states in the trees and their lca. This operation is the direct simulation does not work, but if the middle pieces left coordinates and coordinate difference referred to as \ (a \) , and the difference is referred to as a right middle \ (B \) (assumed here that \ (a> B \) , similar to the contrary), then after each operation will be \ ((ab, b) \) , after the operation several times to get \ ((A \ MOD b, b) \) (if \ (b | a \) is \ ((b, b) \) ), then will the reverse operation until \ (a = b \) , so every time as long as the \ ((a, b) \ ) went \ ((a \ mod b, b) \) just fine, it will only take \ (log n \) times, then the distance to reach the root can also be calculated .lca passing, then put two states of the father in order to remember the stack, each stack bomb bomb to the last one and the same element, but it could really pay attention the lca may not \ ((a \ MOD b, b) \) , therefore I have a little talk about

#include<bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db double

using namespace std;
const int inf=1<<30;
int rd()
{
    int x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
}
struct node
{
    int x,y,z;
    node(){}
    node(int nx,int ny,int nz)
    {
        x=nx,y=ny,z=nz;
        if(x>y) swap(x,y);
        if(y>z) swap(y,z);
        if(x>y) swap(x,y);
    }
    bool operator == (const node &bb) const {return x==bb.x&&y==bb.y&&z==bb.z;}
}s1[50],s2[20];
int d1[50],d2[50],t1,t2,ans;
node getfa(node aa)
{
    int l1=aa.y-aa.x,l2=aa.z-aa.y;
    if(l1==l2) return node(-inf,-inf,-inf);
    if(l1<l2)
    {
        bool o=l2%l1==0;
        int dt=o?l1:0;
        ans+=l2/l1-o;
        return node(aa.z-dt-l2%l1-l1,aa.z-dt-l2%l1,aa.z);
    }
    else
    {
        bool o=l1%l2==0;
        int dt=o?l2:0;
        ans+=l1/l2-o;
        return node(aa.x,aa.x+dt+l1%l2,aa.x+dt+l1%l2+l2);
    }
}

int main()
{
    s1[0].x=-inf,s2[0].x=-inf-1;
    s1[++t1]=node(rd(),rd(),rd());
    s2[++t2]=node(rd(),rd(),rd());
    while(s1[t1].x>-inf) ++t1,s1[t1]=getfa(s1[t1-1]);
    --t1;
    while(s2[t2].x>-inf) ++t2,s2[t2]=getfa(s2[t2-1]);
    --t2;
    if(!(s1[t1]==s2[t2])) {puts("NO");return 0;}
    puts("YES");
    while(s1[t1-1]==s2[t2-1])
    {
        --t1,--t2;
        int l1=s1[t1].y-s1[t1].x,l2=s1[t1].z-s1[t1].y;
        if(l1<l2) ans-=2*(l2/l1-(l2%l1==0));
        else ans-=2*(l1/l2-(l1%l2==0));
    }
    --t1,--t2;
    if(t1&&t2)
    {
        if(s1[t1].x==s2[t2].x)
        {
            int b=s1[t1].z-s1[t1].y,a=min(s1[t1].y-s1[t1].x,s2[t2].y-s2[t2].x)-(s1[t1+1].y-s1[t1+1].x);
            ans-=2*(a/b);
        }
        else if(s1[t1].z==s2[t2].z)
        {
            int b=s1[t1].y-s1[t1].x,a=min(s1[t1].z-s1[t1].y,s2[t2].z-s2[t2].y)-(s1[t1+1].z-s1[t1+1].y);
            ans-=2*(a/b);
        }
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/smyjr/p/11360896.html