-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelc.cpp
92 lines (77 loc) · 1.97 KB
/
delc.cpp
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
vb prime(1e6 + 8, true);
void solve() {
ll n, e;
cin >> n >> e;
vi a(n);
for(ll i = 0; i < n; i++) {
cin >> a[i];
}
vb all1(n);
vb works(n);
vi sum(n);
vi sum2(n);
ll tot = 0;
for (ll i = 0; i < n; i++) {
ll last = i - e;
if(last < 0) {
if (a[i] == 1) {
all1[i] = true;
sum[i] = 1;
} else if (prime[a[i]]) {
works[i] = true;
sum[i] = 1;
}
} else {
if(all1[last]) {
if(a[i] == 1) {
all1[i] = true;
sum[i] = sum[last] + 1;
} else if (prime[a[i]]) {
works[i] = true;
sum[i] = sum[last] + 1;
}
} else if(works[last]) {
if(a[i] == 1) {
works[i] = true;
sum[i] = sum[last];
sum2[i] = sum2[last] + 1;
}
}
}
if(!works[i] && !all1[i]) {
if (a[i] == 1) {
all1[i] = true;
sum[i] = 1;
} else if (prime[a[i]]) {
works[i] = true;
sum[i] = max((ll)1, sum2[last] + (ll)1);
tot += sum[i] - 1;
}
} else {
if(works[i] && last >= 0) tot += sum[last];
}
}
cout << tot << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
prime[0] = prime[1] = false;
for (ll i = 2; i < (1e6+8); i++) {
if (prime[i] && (long long)i * i < (1e6+8)) {
for (ll j = i * i; j < (1e6+8); j += i)
prime[j] = false;
}
}
ll t;
cin >> t;
while(t--) solve();
return 0;
}