GCD Strings

3.4

92 votes
Backtracking, Easy, Math, Recursion
Problem

Let P[0N1] be a binary string of length N. Then let's define S(P) as an infinite string with S[i]=P[i%N]  i0 (informally, S(P) is the concatenation of P with itself an infinite number of times).

Define the GCD-string of two integers a,b, with ab to be a binary string of length a that satisfies the following:

  • g(a,b) = 100000 (1 followed by a1 zeros ) if a is divisible by b
  • g(a,b) = First a characters of S(g(b,a mod b)) otherwise

We can define F(a,b) to be the value of the integer represented by the binary string g(a,b) in base-2. Given T pairs of integers (x,y), compute F(x,y) mod 109+7 for each pair.

Input Format:

The first line will contain the number of test cases T.
Each test case can be described with a single line containing two integers x,y.

Output Format:

Output T numbers, the answers to each problem.

Constraints

For all subtasks:
T104
1yx

File 1 (70 pts)
x100

File 2 (30 pts)
x109

Sample Input
5
3 1
3 2
5 2
10 4
100 3
Sample Output
4
5
21
546
986497880
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The base 2 results for the first four samples are as follows

  1. 100
  2. 101
  3. 10101
  4. 1000100010

Editor Image

?