I Hate Number Theory

Time limit1sMemory limit128 MB

Summary
For each query interval [L, U] below one million, find the maximum of a score built from prime-factor counts over all subintervals [a, b].
Level

Medium7 of 10

Topics
Number theory, Prefix sum, Dynamic programming
Solved
No attempts yet

Problem

After a number-theory midterm, a student who had skipped exactly one topic, Euler's totient function (φ\varphi), was dismayed to find every question was about it. So the student decided to define a personal totient function.

For an integer n≥2n \ge 2, let F(n)F(n) be the non-decreasing list of primes whose product is nn. For example, F(8)=⟨2,2,2⟩F(8) = \langle 2, 2, 2 \rangle, F(60)=⟨2,2,3,5⟩F(60) = \langle 2, 2, 3, 5 \rangle, and F(71)=⟨71⟩F(71) = \langle 71 \rangle. Let O(n)O(n) be the length of F(n)F(n), i.e. the number of prime factors of nn counted with multiplicity. Thus O(8)=3O(8) = 3, O(60)=4O(60) = 4, and O(71)=1O(71) = 1.

Now, for a positive integer nn, define p(n)p(n) as follows.

p(n)={0(n=1)−1(n is prime)O(n)(otherwise)p(n) = \begin{cases} 0 & (n = 1) \\ -1 & (n \text{ is prime}) \\ O(n) & (\text{otherwise}) \end{cases}

The table below lists the first 2020 values of p(n)p(n).

nn11223344556677889910101111121213131414151516161717181819192020
p(n)p(n)00−1-1−1-122−1-122−1-1332222−1-133−1-1222244−1-133−1-133

For two positive integers aa, bb with a≤ba \le b, define the totient function φ(a,b)\varphi(a, b) as follows.

φ(a,b)=(∑k=abp(k))−(b−a+1)\varphi(a, b) = \left( \sum_{k=a}^{b} p(k) \right) - (b - a + 1)

For example, φ(1,4)=−4\varphi(1, 4) = -4, φ(16,16)=3\varphi(16, 16) = 3, and φ(8,12)=4\varphi(8, 12) = 4.

Given an interval [L,U][L, U], write a program that finds the maximum value of φ(a,b)\varphi(a, b) over all aa, bb with L≤a≤b≤UL \le a \le b \le U. For instance, over the interval [1,20][1, 20] the maximum is 77, attained at φ(8,16)\varphi(8, 16).

Input

The input consists of several test cases, at most 7 0007\,000 of them. Each test case is a single line containing two integers LL and UU. (1≤L≤U<1 000 0001 \le L \le U < 1\,000\,000)

The last line contains two values of −1-1; this line is not processed.

Output

For each test case, print on its own line the maximum value of φ(a,b)\varphi(a, b) obtainable over the interval [L,U][L, U]. Each line has the form <test case number>. <maximum>, where the test case number is counted from 11.

Examples4

  1. Example 1

    Input
    1 5
    1 20
    10 20
    900000 901000
    -1 -1
    
    Expected output
    1. 1
    2. 7
    3. 5
    4. 2551
    
  2. Example 2

    Input
    16 16
    -1 -1
    
    Expected output
    1. 3
    
  3. Example 3

    Input
    8 12
    -1 -1
    
    Expected output
    1. 4
    
  4. Example 4

    Input
    1 4
    8 12
    16 16
    -1 -1
    
    Expected output
    1. 1
    2. 4
    3. 3