GCD!

Interview

Time limit1sMemory limit128 MB

Summary
For each pair n and k, compute gcd(n!, k); n can reach 1e9 so n! cannot be built, which forces a bound on the prime factors of k that matter.
Level

Medium7 of 10

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

Problem

The greatest common divisor (GCD) of two natural numbers is the largest natural number that divides both of them without a remainder. For example, the greatest common divisor of 88 and 1212 is gcd⁡(8,12)=4\gcd(8, 12) = 4, because 44 is the largest integer that divides both 88 and 1212. (The common divisors of 88 and 1212 are 1,2,41, 2, 4.)

The factorial of a natural number is the product of every positive integer less than or equal to it. For example, the factorial of 55 is 5!=1×2×3×4×5=1205! = 1 \times 2 \times 3 \times 4 \times 5 = 120. (By definition, 0!=10! = 1.)

Given two numbers nn and kk, write a program that computes the greatest common divisor of n!n! and kk. For example, if n=3n = 3 and k=10k = 10, then gcd⁡(n!,k)=gcd⁡(3!,10)=gcd⁡(6,10)=2\gcd(n!, k) = \gcd(3!, 10) = \gcd(6, 10) = 2.

Input

The input consists of several lines. Each line contains two integers nn and kk separated by a space, and the input continues until the end of the file. (0≤n≤1,000,000,0000 \le n \le 1{,}000{,}000{,}000, 1≤k≤1,000,000,0001 \le k \le 1{,}000{,}000{,}000)

Output

For each line of the input, print the greatest common divisor of n!n! and kk on its own line.

Examples1

  1. Example 1

    Input
    3 10
    10 240
    12 364
    100 2351
    629 163547
    
    Expected output
    2
    240
    28
    1
    67