CF232C.Doe Graphs

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

John Doe decided that some mathematical object must be named after him. So he invented the Doe graphs. The Doe graphs are a family of undirected graphs, each of them is characterized by a single non-negative number — its order.

We'll denote a graph of order kk as D(k)D(k) , and we'll denote the number of vertices in the graph D(k)D(k) as D(k)|D(k)| . Then let's define the Doe graphs as follows:

  • D(0)D(0) consists of a single vertex, that has number 11 .
  • D(1)D(1) consists of two vertices with numbers 11 and 22 , connected by an edge.
  • D(n)D(n) for n>=2n>=2 is obtained from graphs D(n1)D(n-1) and D(n2)D(n-2) . D(n1)D(n-1) and D(n2)D(n-2) are joined in one graph, at that numbers of all vertices of graph D(n2)D(n-2) increase by D(n1)|D(n-1)| (for example, vertex number 11 of graph D(n2)D(n-2) becomes vertex number 1+D(n1)1+|D(n-1)| ). After that two edges are added to the graph: the first one goes between vertices with numbers D(n1)|D(n-1)| and D(n1)+1|D(n-1)|+1 , the second one goes between vertices with numbers D(n1)+1|D(n-1)|+1 and 11 . Note that the definition of graph D(n)D(n) implies, that D(n)D(n) is a connected graph, its vertices are numbered from 11 to D(n)|D(n)| .

The picture shows the Doe graphs of order 1, 2, 3 and 4, from left to right.John thinks that Doe graphs are that great because for them exists a polynomial algorithm for the search of Hamiltonian path. However, your task is to answer queries of finding the shortest-length path between the vertices aia_{i} and bib_{i} in the graph D(n)D(n) .

A path between a pair of vertices uu and vv in the graph is a sequence of vertices x1x_{1} , x2x_{2} , ...... , xkx_{k} (k>1) such, that x1=ux_{1}=u , xk=vx_{k}=v , and for any ii (i<k) vertices xix_{i} and xi+1x_{i+1} are connected by a graph edge. The length of path x1x_{1} , x2x_{2} , ...... , xkx_{k} is number (k1)(k-1) .

输入格式

The first line contains two integers tt and nn ( 1<=t<=105; 1<=n<=1031<=t<=10^{5}; 1<=n<=10^{3} ) — the number of queries and the order of the given graph. The ii -th of the next tt lines contains two integers aia_{i} and bib_{i} ( 1<=ai,bi<=10161<=a_{i},b_{i}<=10^{16} , aibia_{i}≠b_{i} ) — numbers of two vertices in the ii -th query. It is guaranteed that ai,bi<=D(n)a_{i},b_{i}<=|D(n)| .

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

输出格式

For each query print a single integer on a single line — the length of the shortest path between vertices aia_{i} and bib_{i} . Print the answers to the queries in the order, in which the queries are given in the input.

输入输出样例

  • 输入#1

    10 5
    1 2
    1 3
    1 4
    1 5
    2 3
    2 4
    2 5
    3 4
    3 5
    4 5
    

    输出#1

    1
    1
    1
    2
    1
    2
    3
    1
    2
    1
    
首页