NOIP2017 14 three-city race simulation

Three-city

Title Description

Three-City is a huge city, so named because of the city dotted with countless fork in the road. (From the name to the topic and force people 0)

Specifically, the city has infinitely many intersections, each intersection there is only one positive integer numbers. In addition junctions 1, are connected each intersection 3 exactly the road to the other three junctions: intersection number x (x> 1) the even numbered road to 3 x * 2, x * 2 + 1, and x / 2 (rounded down) of three junctions. 1 connected only an intersection of two roads, are connected to the junctions 2 and 3.

All roads are can be two-way traffic, and the length is 1. Now, there are n problem: x shortest length from the intersection to intersection y is the number?

Input Format

         The first row contains an integer n, the number of interrogation;

         Next n lines, each line contains two positive integers x, y, x indicating an inquiry from the intersection to the intersection of the shortest length y.

Output Format

         N output lines, each line contains an integer representing the answer to each inquiry. If the path from x to y does not exist for a certain inquiry, -1 is output.

Sample input

3
5 7
2 4
1 1

Sample Output

4
1
0

Sample interpretation

         No. 5 No. 7 to the path junction intersection is: 5-> 2-> 1-> 3-> 7, a length of 4;

         No. 2 to the intersection of the path junctions 4: 2-> 4, a length of 1;

         1, the path length to the junction itself is 0;

data range

         30% of the data, x, y≤20;

         60% of the data, X, y≤10 . 5 , n ≤ 10;

         100% of the data, X, y≤10 . 9 , n ≤ 10 . 4 .

 Ideas:

According to the meaning of problems, we know this is a full binary tree, the situation can not reach the two points of each other does not exist.

A practice:

LCA thinking, we first calculate the depth of each point. First, the two points to reach the same depth, and then jump up at the same time, until equal.

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

int n,a,b,da,db,ans1,ans2;

long long read()
{
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main()
{
    freopen("city.in","r",stdin);
    freopen("city.out","w",stdout);
    n=read();
    for(int i=1;i<=n;++i)
    {
        a=read();b=read();
        ans1=0;ans2=0;
        double aa=a;
        double bb=b;
        da=1;db=1;
        while(aa>=1){aa/=2;if(aa>=1)da++;}
        while(bb>=1){bb/=2;if(bb>=1)db++;}
        if(da<db){swap(da,db);swap(a,b);}
        while(da!=db){da--;a>>=1;ans1++;}
        while(a!=b){a>>=1;b>>=1;ans2++;}
        printf("%d\n",ans1+2*ans2);
    }
    return 0;
}

Practice two:

Better, we repeatedly until 2 equal so far, the answer is the number of operations for a large number of divided

Guess you like

Origin www.cnblogs.com/-hhs/p/11441711.html