We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Formula is (n+m-1)!/(n!*(m-1)!)
1. AL1 (ArrayList) contains integers from 2 to n+m-1.
2. AL2 contains integers from 2 to n and from 2 to m-1.
3. There is function returning list with primes (like it turns list {1 2 3 4 5 6} to {1 2 3 2 2 5 2 3}).
4. Delete from AL1 all elements which occur in AL2 (AL2 becomes empty).
5. Multiply all elements of AL1 by mod.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int m = in.nextInt();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
for (int i = 2; i <= n + m - 1; i++) {
list1.add(i);
if (i <= n)
list2.add(i);
if (i <= m - 1)
list2.add(i);
}
list1 = primeList(list1);
list2 = primeList(list2);
for (int i = 0; i < list2.size(); i++)
list1.remove(list1.indexOf(list2.get(i)));
long res = 1;
int mod = (int) Math.pow(10, 9) + 7;
for (int i = 0; i < list1.size(); i++)
res = (res * list1.get(i)) % mod;
System.out.println(res);
}
}
public static ArrayList<Integer> primeList(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++) {
ArrayList<Integer> primes = new ArrayList<>();
for (int j = 2; j <= (int) Math.sqrt(list.get(i)); j++) {
if (list.get(i) % j == 0) {
primes.add(j);
list.set(i, list.get(i) / j);
j--;
}
}
list.addAll(primes);
}
return list;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Permutations
You are viewing a single comment's thread. Return to all comments →
Formula is (n+m-1)!/(n!*(m-1)!) 1. AL1 (ArrayList) contains integers from 2 to n+m-1. 2. AL2 contains integers from 2 to n and from 2 to m-1. 3. There is function returning list with primes (like it turns list {1 2 3 4 5 6} to {1 2 3 2 2 5 2 3}). 4. Delete from AL1 all elements which occur in AL2 (AL2 becomes empty). 5. Multiply all elements of AL1 by mod.