This page is still under construction.

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

Number Sets (Large)

Time limit50sMemory limit512 MB

Summary
Given an interval of consecutive integers and a threshold P, merge pairs sharing a prime factor of at least P, then count the remaining sets.
Level

Medium6 of 10

Topics
Union-find, Number theory, Math, Prefix sum
Solved
No attempts yet

Problem

You are given an interval of consecutive integers and an integer PP. At the start, every integer in the interval sits in a set of its own.

Look at every pair of integers in the interval. If the two integers have a common prime factor that is at least PP, merge the two sets that contain them.

Report how many sets are left when the process ends.

Input

The first line contains an integer CC, the number of test cases.

Each of the next CC lines contains three single-space-separated integers AA, BB and PP. AA and BB are the first and last integers of the interval, and PP is the value described above.

Limits

  • 1≤C≤1001 \le C \le 100
  • 1≤A≤B≤10121 \le A \le B \le 10^{12}
  • B≤A+1000000B \le A + 1000000
  • 2≤P≤B2 \le P \le B

Output

For each test case, print one line of the form "Case #X: Y", where XX is the test case number starting from 1 and YY is the number of sets.

Examples3

  1. Example 1

    Input
    2
    10 20 5
    10 20 3
    
    Expected output
    Case #1: 9
    Case #2: 7
    
  2. Example 2

    Input
    1
    1 10 2
    
    Expected output
    Case #1: 3
    
  3. Example 3

    Input
    6
    100 200 7
    100 200 11
    100 200 13
    100 200 101
    100 200 199
    100 200 200
    
    Expected output
    Case #1: 51
    Case #2: 62
    Case #3: 70
    Case #4: 101
    Case #5: 101
    Case #6: 101