CF BUDDY
← Problems·

1933C · Turtle Fingers: Count the Values of k

1100 · brute force, implementation, math

Problem: You are given three positive integers aa, bb and ll (a,b,l>0a,b,l>0).

It can be shown that there always exists a way to choose non-negative (i.e. 0\ge 0) integers kk, xx, and yy such that l=kaxbyl = k \cdot a^x \cdot b^y.

Your task is to find the number of distinct possible values of kk across all such ways.

Input Format: The first line contains the integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The following tt lines contain three integers, aa, bb and ll (2a,b1002 \le a, b \le 100, 1l1061 \le l \le 10^6) — description of a test case.

Output Format: Output tt lines, with the ii-th (1it1 \le i \le t) line containing an integer, the answer to the ii-th test case.

Note: In the first test case, a=2,b=5,l=20a=2, b=5, l=20. The possible values of kk (and corresponding x,yx,y) are as follows:

  • Choose k=1,x=2,y=1k = 1, x = 2, y = 1. Then kaxby=12251=20=lk \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = l.
  • Choose k=2,x=1,y=1k = 2, x = 1, y = 1. Then kaxby=22151=20=lk \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = l.
  • Choose k=4,x=0,y=1k = 4, x = 0, y = 1. Then kaxby=42051=20=lk \cdot a^x \cdot b^y = 4 \cdot 2^0 \cdot 5^1 = 20 = l.
  • Choose k=5,x=2,y=0k = 5, x = 2, y = 0. Then kaxby=52250=20=lk \cdot a^x \cdot b^y = 5 \cdot 2^2 \cdot 5^0 = 20 = l.
  • Choose k=10,x=1,y=0k = 10, x = 1, y = 0. Then kaxby=102150=20=lk \cdot a^x \cdot b^y = 10 \cdot 2^1 \cdot 5^0 = 20 = l.
  • Choose k=20,x=0,y=0k = 20, x = 0, y = 0. Then kaxby=202050=20=lk \cdot a^x \cdot b^y = 20 \cdot 2^0 \cdot 5^0 = 20 = l.

In the second test case, a=2,b=5,l=21a=2, b=5, l=21. Note that l=21l = 21 is not divisible by either a=2a = 2 or b=5b = 5. Therefore, we can only set x=0,y=0x = 0, y = 0, which corresponds to k=21k = 21.

In the third test case, a=4,b=6,l=48a=4, b=6, l=48. The possible values of kk (and corresponding x,yx,y) are as follows:

  • Choose k=2,x=1,y=1k = 2, x = 1, y = 1. Then kaxby=24161=48=lk \cdot a^x \cdot b^y = 2 \cdot 4^1 \cdot 6^1 = 48 = l.
  • Choose k=3,x=2,y=0k = 3, x = 2, y = 0. Then kaxby=34260=48=lk \cdot a^x \cdot b^y = 3 \cdot 4^2 \cdot 6^0 = 48 = l.
  • Choose k=8,x=0,y=1k = 8, x = 0, y = 1. Then kaxby=84061=48=lk \cdot a^x \cdot b^y = 8 \cdot 4^0 \cdot 6^1 = 48 = l.
  • Choose k=12,x=1,y=0k = 12, x = 1, y = 0. Then kaxby=124160=48=lk \cdot a^x \cdot b^y = 12 \cdot 4^1 \cdot 6^0 = 48 = l.
  • Choose k=48,x=0,y=0k = 48, x = 0, y = 0. Then kaxby=484060=48=lk \cdot a^x \cdot b^y = 48 \cdot 4^0 \cdot 6^0 = 48 = l.

Sample Cases

Case 1

Input

11
2 5 20
2 5 21
4 6 48
2 3 72
3 5 75
2 2 1024
3 7 83349
100 100 1000000
7 3 2
2 6 6
17 3 632043

Output

6
1
5
12
6
11
24
4
1
3
24

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback