This page is still under construction.

Parts of this page are still being built. What you see may change.

Exploding CPU

Time limit1sMemory limit128 MB

Summary
Count integers in a range that factor as p1*p2*...*pn (n>=3) where consecutive primes follow p_i = A*p_{i-1}+B, starting from p_0=1.
Level

Hard8 of 10

Topics
Number theory, Brute force, Math, Implementation
Solved
No attempts yet

Problem

A hardware company is preparing a high-performance CPU specialized for number theory. Among its instructions is PFACT, which takes a single argument and returns all of that number's prime factors at remarkable speed.

There is, however, a serious flaw: for certain special inputs the PFACT instruction malfunctions and makes the whole processor "explode". Investigation revealed that every number that triggers an explosion shares the same number-theoretic structure.

An explosive number is a number x=p0⋅p1⋅p2⋯pnx = p_0 \cdot p_1 \cdot p_2 \cdots p_n such that:

  • all pip_i are distinct prime numbers;
  • p0=1p_0 = 1;
  • pi=A⋅pi−1+Bp_i = A \cdot p_{i-1} + B for every i=1,2,…,ni = 1, 2, \ldots, n, where AA and BB are integers;
  • n≥3n \geq 3 (that is, there are at least three prime factors p1,…,pnp_1, \ldots, p_n).

The integers AA and BB may differ from one explosive number to another.

For example, 4505=1⋅5⋅17⋅534505 = 1 \cdot 5 \cdot 17 \cdot 53 is explosive: taking A=3A = 3 and B=2B = 2 gives 5=3⋅1+25 = 3 \cdot 1 + 2, 17=3⋅5+217 = 3 \cdot 5 + 2, and 53=3⋅17+253 = 3 \cdot 17 + 2, and 55, 1717, 5353 are all prime.

Write a program that counts how many explosive numbers lie within a given range of integers.

Input

The first line contains the number of test cases NN (0≤N≤1000 \leq N \leq 100).

Each of the following test cases is given on one line as two integers xLx_L and xHx_H separated by a single space (0≤xL≤xH≤2×1090 \leq x_L \leq x_H \leq 2 \times 10^9).

Output

For each test case, print on its own line the number of explosive numbers xx with xL≤x≤xHx_L \leq x \leq x_H.

Examples2

  1. Example 1

    Input
    2
    4505 4505
    0 5000
    
    Expected output
    1
    5
    
  2. Example 2

    Input
    1
    0 104
    
    Expected output
    0