Devu and Birthday Celebration (Codeforces 439E)
Tags: Math, combinatorics

Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).

Input: The first line contains an integer q representing the number of queries (1 ≤ q ≤ 105). Each of the next q lines contains two space space-separated integers n, f (1 ≤ f ≤ n ≤ 105).

Output: For each query, output a single integer in a line corresponding to the answer of each query.

Note: For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.

Sample Inputs:
5
6 2
7 2
6 3
6 4
7 4

Sample Outputs:
2
6
9
10
20

Solution (Python):
import itertools
import functools
import operator
N = 100001
P = 10 ** 9 + 7
fact = [1]
for i in range(1, N):
    fact.append(fact[-1] * i % P)
inv = [0, 1]
for i in range(2, N):
    inv.append(P - P // i * inv[P % i] % P)
inv_fact = [1]
for i in range(1, N):
    inv_fact.append(inv_fact[-1] * inv[i] % P)
least_div = [-1] * N
primes = []
for p in range(2, N):
    if least_div[p] == -1:
        primes.append(p)
        least_div[p] = p
    ldiv = least_div[p]
    for mult in primes:
        mark = mult * p
        if mult > ldiv or mark >= N:
            break
        least_div[mark] = mult
t = int(input())

def powerset(iterable):
    s = list(iterable)
    return itertools.chain.from_iterable((itertools.combinations(s, r) for r in range(len(s) + 1)))

@functools.lru_cache(maxsize=None)
def factor(n):
    ret = []
    while n != 1:
        tmp = least_div[n]
        if not (ret and ret[-1] == tmp):
            ret.append(tmp)
        n //= tmp
    return ret

@functools.lru_cache(maxsize=None)
def solve(n, k):
    divs = factor(n)
    ret = 0
    for subset in powerset(divs):
        div = functools.reduce(operator.mul, subset, 1)
        if n // div >= k:
            tmp = fact[n // div - 1] * inv_fact[n // div - k] % P * inv_fact[k - 1] % P
            ret += (-1 if len(subset) % 2 == 1 else 1) * tmp
            ret %= P
    return ret
for _ in range(t):
    (n, k) = map(int, input().split())
    print(solve(n, k))

Solution (Python):
import itertools
import functools
import operator
N = 100001
P = 10 ** 9 + 7
fact = [1]
for i in range(1, N):
    fact.append(fact[-1] * i % P)
inv = [0, 1]
for i in range(2, N):
    inv.append(P - P // i * inv[P % i] % P)
inv_fact = [1]
for i in range(1, N):
    inv_fact.append(inv_fact[-1] * inv[i] % P)
least_div = [-1] * N
primes = []
for p in range(2, N):
    if least_div[p] == -1:
        primes.append(p)
        least_div[p] = p
    ldiv = least_div[p]
    for mult in primes:
        mark = mult * p
        if mult > ldiv or mark >= N:
            break
        least_div[mark] = mult
t = int(input())

def powerset(iterable):
    s = list(iterable)
    return itertools.chain.from_iterable((itertools.combinations(s, r) for r in range(len(s) + 1)))

@functools.lru_cache(maxsize=None)
def factor(n):
    ret = []
    while n != 1:
        tmp = least_div[n]
        if not (ret and ret[-1] == tmp):
            ret.append(tmp)
        n //= tmp
    return ret

@functools.lru_cache(maxsize=None)
def solve(n, k):
    divs = factor(n)
    ret = 0
    for subset in powerset(divs):
        div = functools.reduce(operator.mul, subset, 1)
        if n // div >= k:
            tmp = fact[n // div - 1] * inv_fact[n // div - k] % P * inv_fact[k - 1] % P
            ret += (-1 if len(subset) % 2 == 1 else 1) * tmp
            ret %= P
    return ret
for _ in range(t):
    (n, k) = map(int, input().split())
    print(solve(n, k))

Solution (Python):
import itertools
import functools
import operator
N = 100001
P = 10 ** 9 + 7
fact = [1]
for i in range(1, N):
    fact.append(fact[-1] * i % P)
inv = [0, 1]
for i in range(2, N):
    inv.append(P - P // i * inv[P % i] % P)
inv_fact = [1]
for i in range(1, N):
    inv_fact.append(inv_fact[-1] * inv[i] % P)
least_div = [-1] * N
primes = []
for p in range(2, N):
    if least_div[p] == -1:
        primes.append(p)
        least_div[p] = p
    ldiv = least_div[p]
    for mult in primes:
        mark = mult * p
        if mult > ldiv or mark >= N:
            break
        least_div[mark] = mult
t = int(input())

def powerset(iterable):
    s = list(iterable)
    return itertools.chain.from_iterable((itertools.combinations(s, r) for r in range(len(s) + 1)))
memo_factor = dict()

def factor(n):
    if n in memo_factor:
        return memo_factor[n]
    ret = []
    while n != 1:
        tmp = least_div[n]
        if not (ret and ret[-1] == tmp):
            ret.append(tmp)
        n //= tmp
    memo_factor[n] = ret
    return ret

@functools.lru_cache(maxsize=None)
def solve(n, k):
    divs = factor(n)
    ret = 0
    for subset in powerset(divs):
        div = functools.reduce(operator.mul, subset, 1)
        if n // div >= k:
            tmp = fact[n // div - 1] * inv_fact[n // div - k] % P * inv_fact[k - 1] % P
            ret += (-1 if len(subset) % 2 == 1 else 1) * tmp
            ret %= P
    return ret
for _ in range(t):
    (n, k) = map(int, input().split())
    print(solve(n, k))