2
// List of primes upto sqrt(10e4)4
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 976
// Factors of integers upto 137
int[][] SMALL_INT_FACTORS = {8
null, {1}, {2}, {3}, {2, 2}, {5}, {2, 3}, {7}, {2, 2, 2}, {3, 3}, {2, 5}, {11}, {2, 2, 3}, {13}11
public int[] waysToFillArray(int[][] queries) {12
int n = queries.length;13
int[] ans = new int[n];14
for (int i = 0; i < n; ++i) ans[i] = dewIt(queries[i]);18
private int dewIt(int[] q) {20
int k = q[0], num = q[1];21
for (int p : PRIMES) {22
int n = 0; // Power of prime factor within this num23
while (num % p == 0) {27
ans = multiply(ans, getSplitCount(n, k));29
if (num > 1) // If any prime greater than sqrt(10e4) is a remaining factor of num30
ans = multiply(ans, k);34
// In how many ways can we split an integer n into k non-negative numbers?35
// Answer = combin(n+k-1, k-1) (11th grade math; non-trivial but google-able)36
// combin(n+k-1, k-1) smoothly turns into combin(n+k-1, n)37
private long getSplitCount(int n, int k) {38
int a = n + k - 1, b = k - 1;39
if ((b << 1) > a) b = a - b;43
// combin(n, r) = (n . n-1 . n-2 . n-3 ... n-r+1)/(1 . 2 . 3 ... r)44
// Brute force division:45
// We exploit the fact that r in this problem is at max 1346
// We try to cancel common prime factors of numerator and denominator terms47
// This leaves us with numerator elements alone (thus avoiding mod related issues wrt division)48
private long comb(int n, int r) {49
int[] numerators = new int[r];50
for (int i = 0, j = n; i < r; ++i, --j) numerators[i] = j;51
for (int i = 2; i <= r; ++i) {52
int[] factors = SMALL_INT_FACTORS[i];54
for (int j = 0; ; ++j)55
if (numerators[j] % f == 0) {61
for (int num : numerators) ans = multiply(ans, num);65
private long multiply(long v, long u) {66
return v * u % 1000000007;