Petya and Divisors (Codeforces 111B)
Tags: Others, number theory

Little Petya loves looking for numbers' divisors. One day Petya came across the following problem:You are given n queries in the form "xi yi". For each query Petya should count how many divisors of number xi divide none of the numbers xi - yi, xi - yi + 1, ..., xi - 1. Help him.

Input: The first line contains an integer n (1 ≤ n ≤ 105). Each of the following n lines contain two space-separated integers xi and yi (1 ≤ xi ≤ 105, 0 ≤ yi ≤ i - 1, where i is the query's ordinal number; the numeration starts with 1). If yi = 0 for the query, then the answer to the query will be the number of divisors of the number xi. In this case you do not need to take the previous numbers x into consideration.

Output: For each query print the answer on a single line: the number of positive integers k such that 

Note: Let's write out the divisors that give answers for the first 5 queries:1) 1, 2, 4 2) 33) 54) 2, 65) 9, 18

Sample Inputs:
6
4 0
3 1
5 2
6 2
18 4
10000 3

Sample Outputs:
3
1
1
2
2
22

Solution (Python):
N = 100000
arr = [[i, 1] for i in range(N + 1)]
arr[1] = [1]
i = 2
while i <= N // 2:
    j = 2
    while j <= N // i:
        arr[i * j].append(i)
        j += 1
    i += 1
use = [-1] * (N + 1)
n = int(input())
for t in range(n):
    (a, b) = list(map(int, input().split()))
    res = 0
    for i in arr[a]:
        if use[i] < t - b:
            res += 1
        use[i] = t
    print(res)

Solution (Python):
maxn = 100000
div = [0] * (maxn + 1)
last = [-maxn] * (maxn + 1)
for i in range(maxn + 1):
    div[i] = list()
for i in range(2, maxn + 1):
    for j in range(i, maxn + 1, i):
        div[j].append(i)
t = int(input())
for k in range(0, t):
    (x_i, y_i) = input().split(' ')
    x_i = int(x_i)
    y_i = int(y_i)
    if y_i == 0:
        print(len(div[x_i]) + 1)
    else:
        print(sum((1 for v in div[x_i] if last[v] < k - y_i)))
    for d in div[x_i]:
        last[d] = k

Solution (Python):
import heapq
from math import pi, sqrt, ceil, floor, log2, log10
from collections import deque, defaultdict, Counter
from sys import setrecursionlimit
setrecursionlimit(1000000)

def gcd(a, b):
    while b:
        (a, b) = (b, a % b)
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

def minput():
    return map(int, input().split())

def linput():
    return list(map(int, input().split()))
MOD = int(1000000000.0) + 7
sz = int(100000.0) + 5
INT_MAX = float('inf')

def main():
    n = int(input())
    div = defaultdict(set)
    for i in range(2, sz):
        for j in range(i, sz, i):
            div[j].add(i)
    prev = [-1] * sz
    for j in range(n):
        (x, y) = minput()
        res = 0
        if y == 0:
            print(len(div[x]) + 1)
            for i in div[x]:
                prev[i] = j
            continue
        for i in div[x]:
            if prev[i] < j - y:
                res += 1
            prev[i] = j
        print(res)
if __name__ == '__main__':
    main()