-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.java
92 lines (74 loc) · 2.76 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
//declaring static variables which will be same for all the test cases
static long M = 1000000007;
static boolean[] f = new boolean[8200];
static ArrayList<Integer> P= new ArrayList<Integer>();
// Complete the primeXor function below.
static int primeXor(int[] a, long[] cnt) {
//to store the count of subsets
long[][] dp = new long[1005][8200];
dp[0][3500] = (cnt[0] + 1)/2;
dp[0][0] = (cnt[0] + 2) / 2;
for(int i=1; i<1005; i++){
//check all the values upto 8200
for(int j=0; j<8200; j++){
// it may give you ArrayIndexOutOfBoundsException to avoid it use try catch
try{
dp[i][j] = (dp[i-1][j]*((cnt[i]+2)/2) + dp[i-1][(j^(i+3500))]*((cnt[i]+1)/2) ) % M ;
}
catch(ArrayIndexOutOfBoundsException e){}
}
}
long ans = 0;
for(int p : P){
ans += dp[1004][p];
//take modulo of the large values
ans %= M;
}
return (int)(ans%M);
}
//function to make all the numbers till 8200 (round of for 8192 i.e. 2^13) prime
//As the xor-sum of any subset of array elements will not exceed 8191
static void makePrime(){
f[0] = f[1] = true;
for(int i=2; i*i < 8200; i++){
if(!f[i]){
for(int j=i+i; j< 8200; j+=i)
f[j] = true;
}
}
for(int i=2; i<8200; i++) if(!f[i]) P.add(i);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
makePrime();
for (int qItr = 0; qItr < q; qItr++) {
long[] cnt = new long[1005];
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int[] a = new int[n];
String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
cnt[a[i]-3500]++;
}
int result = primeXor(a, cnt);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}