Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.PriorityQueue;
- class Pair
- {
- public long value;
- public long count;
- public Pair(long value,long count)
- {
- this.value = value;
- this.count = count;
- }
- }
- public class Main
- {
- private static int MOD = 1000000007;
- private static long prefixNCR[];
- private static void preComputePrefixNCR(int size)
- {
- prefixNCR = new long[size+1];
- prefixNCR[0] = 1;
- prefixNCR[2] = 1;
- prefixNCR[4] = 3;
- for(int i = 6;i <= size;i += 2)
- {
- prefixNCR[i] = ((i-1) * prefixNCR[i-2])%MOD;
- }
- }
- private static long countPairings(long skillSet[],int persons)
- {
- HashMap<Long,Long> map = new HashMap<>();
- for(int i = 0;i < persons;i++)
- {
- if(map.containsKey(skillSet[i]))
- {
- map.put(skillSet[i],map.get(skillSet[i])+1);
- }
- else
- {
- map.put(skillSet[i],(long)1);
- }
- }
- PriorityQueue<Pair> pq = new PriorityQueue<>((Pair p1,Pair p2) -> {
- if(p1.value < p2.value)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- });
- for(Map.Entry<Long,Long> entry : map.entrySet())
- {
- long value = entry.getKey();
- long count = entry.getValue();
- pq.add(new Pair(value,count));
- }
- long count = 1;
- while(!pq.isEmpty())
- {
- Pair max = pq.poll();
- if (max.count % 2 == 0)
- {
- count = (count * prefixNCR[(int)max.count]) % MOD;
- }
- else
- {
- count = (count * (max.count * prefixNCR[(int)(max.count - 1)]) % MOD) % MOD;
- Pair secondMax = pq.poll();
- if (secondMax.count % 2 != 0)
- {
- count = (count * (secondMax.count * prefixNCR[(int)(secondMax.count - 1)])%MOD) % MOD;
- }
- else
- {
- count = (count * secondMax.count) % MOD;
- secondMax.count -= 1;
- pq.add(secondMax);
- }
- }
- }
- return count;
- }
- public static void main(String[] args) throws IOException
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
- StringBuilder outputString = new StringBuilder();
- preComputePrefixNCR(100000);
- int testCases = Integer.parseInt(br.readLine());
- while(testCases --> 0)
- {
- int persons = Integer.parseInt(br.readLine());
- long skillSet[] = Arrays.stream(br.readLine().split(" ")).mapToLong(c->Long.parseLong(c)).toArray();
- outputString.append(countPairings(skillSet,persons)).append("\n");
- }
- bw.write(outputString.toString());
- bw.close();
- }
- }
Add Comment
Please, Sign In to add comment