Chef loves number theory very much. Now it is time to solve a new kind of problem.
There is given a natural number N. Chef has to answer Q queries of the form T K.
Here T is the type of query and K is the natural number.

If T=1, Chef must find the number of natural numbers which is divisor of both N and K.

If T=2, Chef must find the number of natural numbers which is divisor of N and is divisible by K.

If T=3, Chef must find the number of natural numbers which is divisor of N and is not divisible by K.

Chef can solve all these queries, but you will be hungry for night if this happens, because Chef will not have free time to cook a meal. Therefore you compromise with him and decided that everyone must do his/her own job. You must program and Chef must cook.

------ Input ------ 

There will be 2 numbers in the first line: N and Q. 

Q lines follow with 2 numbers each: T and K

------ Output ------ 

For each of the Q lines you must output the result for corresponding query in separat line.

------ Constraints ------ 

1≤N≤10^{12}
1≤Q≤5*10^{5}
1≤T≤3

1≤K≤10^{12}

----- Sample Input 1 ------ 
12 6
1 6
1 14
2 4
2 3
3 12
3 14
----- Sample Output 1 ------ 
4
2
2
3
5
6
----- explanation 1 ------ 
Numbers for each query:
{1,2,3,6}
{1,2}
{4,12}
{3,6,12}
{1,2,3,4,6}
{1,2,3,4,6,12}

Source: codechef | Difficulty: HARD
Tags: Number theory

Mathematics


Solutions:
import sys
input = sys.stdin.readline
(n, q) = map(int, input().split())
pfactors = []
num = n
i = 2
while i * i <= num:
	if num % i == 0:
		num //= i
		pfactors.append(i)
	else:
		i += 1
if num != 1:
	pfactors.append(num)
nd = dict()
kd = dict()
for i in pfactors:
	nd[i] = 0
	kd[i] = 0
for i in pfactors:
	nd[i] += 1
for _ in range(q):
	(t, k) = map(int, input().split())
	if t == 1:
		if n == 1:
			print(1)
		elif k == 1:
			print(1)
		else:
			for i in pfactors:
				if k % i == 0:
					kd[i] += 1
					k //= i
			ans = 1
			for i in kd:
				ans *= kd[i] + 1
			print(ans)
			for i in kd:
				kd[i] = 0
	elif t == 2:
		if n == 1:
			if k == 1:
				print(1)
			else:
				print(0)
		elif k == 1:
			ans = 1
			for i in nd:
				ans *= nd[i] + 1
			print(ans)
		elif n % k != 0:
			print(0)
		else:
			for i in nd:
				kd[i] = nd[i]
			for i in pfactors:
				if k % i == 0:
					kd[i] -= 1
					k //= i
			ans = 1
			for i in kd:
				ans *= kd[i] + 1
			print(ans)
			for i in kd:
				kd[i] = 0
	elif n == 1:
		if k == 1:
			print(0)
		else:
			print(1)
	elif k == 1:
		print(0)
	elif n % k != 0:
		ans = 1
		for i in nd:
			ans *= nd[i] + 1
		print(ans)
	else:
		ans1 = 1
		for i in nd:
			ans1 *= nd[i] + 1
		for i in nd:
			kd[i] = nd[i]
		for i in pfactors:
			if k % i == 0:
				kd[i] -= 1
				k //= i
		ans2 = 1
		for i in kd:
			ans2 *= kd[i] + 1
		print(ans1 - ans2)
		for i in kd:
			kd[i] = 0