This repository was archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathnice_clique.cpp
More file actions
151 lines (111 loc) · 3.02 KB
/
nice_clique.cpp
File metadata and controls
151 lines (111 loc) · 3.02 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// https://www.hackerrank.com/challenges/niceClique
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;
#define MAXP 40000000
#define MAXD 1000000000000000l
#define MAXN 200
bool isPrime[MAXP], ppd[MAXN], psd[MAXN];
int primes[MAXP], nPrimes;
long long modMult(long long a, long long b, long long c) {
long long x = 0, y = a % c;
for ( ; b > 0; y = (y * 2) % c, b >>= 1)
if (b & 1)
x = (x + y) % c;
return x % c;
}
long long modPow(long long a, long long b, long long c) {
long long x = 1, y = a;
for ( ; b > 0; y = modMult(y, y, c), b >>= 1)
if (b & 1)
x = modMult(x, y, c);
return x % c;
}
bool isProbablePrime(long long p, int iteration = 20) {
if (p < 2) return false;
if (p != 2 && p % 2 == 0) return false;
long long a, temp, mod, s = p - 1;
while ((s & 1) == 0)
s >>= 1;
while (iteration--) {
a = rand() % (p - 1) + 1, temp = s;
for (mod = modPow(a, temp, p); temp != p - 1 && mod != 1 && mod != p - 1; temp <<= 1)
mod = modMult(mod, mod, p);
if (mod != p - 1 && temp % 2 == 0) return false;
}
return true;
}
void calcPrimes() {
memset(isPrime, true, sizeof(isPrime));
isPrime[1] = false;
int l = (int) sqrt(MAXD);
for (int i = 2; i <= (int) sqrt(l); i++) {
if (isPrime[i]) {
for (int j = i + i; j <= l; j += i)
isPrime[j] = false;
}
}
nPrimes = 0;
for (int i = 1; i <= l; i++) {
if (isPrime[i])
primes[nPrimes++] = i;
}
}
void calcParities(int i, long long n) {
while (n > 1) {
if ((n > primes[nPrimes - 1] && isProbablePrime(n)) ||
(n <= primes[nPrimes - 1] && isPrime[n])) {
ppd[i] ^= 1;
if (n > 2)
psd[i] = 0;
n /= n;
}
int l = (int) sqrt(n);
for (int j = 0; j < nPrimes && primes[j] <= l; j++) {
if (n % primes[j] == 0) {
int pp = psd[i];
while (n % primes[j] == 0) {
if (primes[j] > 2 && (pp % 2))
psd[i] ^= 1;
n /= primes[j];
}
ppd[i] ^= 1;
break;
}
}
}
}
int getMaximumSubsetSize(vector<long long> ds) {
calcPrimes();
memset(ppd, 0, sizeof(ppd));
memset(psd, 1, sizeof(psd));
int N = ds.size();
for (int i = 0; i < N; i++)
calcParities(i, ds[i]);
int ppd0 = 0;
int ppd1 = 0;
int psd0 = 0;
int psd1 = 0;
for (int i = 0; i < N; i++) {
if (ppd[i]) ppd1++;
else ppd0++;
if (psd[i]) psd1++;
else psd0++;
}
return max(ppd0, max(ppd1, max(psd0, psd1)));
}
int main() {
int n;
long long d;
vector<long long> ds;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> d;
ds.push_back(d);
}
cout << getMaximumSubsetSize(ds) << endl;
return 0;
}