-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1978F.cpp
More file actions
132 lines (110 loc) · 2.53 KB
/
Copy path1978F.cpp
File metadata and controls
132 lines (110 loc) · 2.53 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
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 1000005;
const int MOD = 1e9 + 7; // Define MOD if the problem requires modular
// arithmetic, otherwise ignore
// Debugging function
void dbg(const char *fmt, ...)
{
#ifdef DBG
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
#endif
}
int p[MAXN];
void init_p()
{
int n = MAXN;
for (int i = 2; i < n; i++) {
if (p[i] == 0) {
p[i] = i;
}
for (int j = 0; j < n; j += i) {
if (p[j] == 0)
p[j] = i;
}
}
for (int i = 0; i < 25; i++) {
dbg("p[%d] = %d\n", i, p[i]);
}
}
vector<int> factorize(int x)
{
vector<int> ret;
while (x != 1) {
int xp = p[x];
ret.push_back(p[x]);
while (x % xp == 0)
x /= xp;
}
return ret;
}
// Global variables
int t, n, k;
vector<int> a;
// sdu
int fa[MAXN * 2];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y)
{
dbg("merge %d %d\n", x, y);
fa[find(x)] = find(y);
}
// Function to clear global input data
void clearData() { a.clear(); }
// Function to solve each test case
void solve()
{
// Placeholder for the main logic of the solution.
// Access input using global variables n, k, and a.
for (int i = 0; i < n * 2; ++i) {
fa[i] = i;
}
map<int, int> prev;
for (int i = 1; i < n * 2; i++) {
auto factors = factorize(a[i % n]);
for (auto fac : factors) {
if (prev.find(fac) == prev.end()) {
prev[fac] = i;
continue;
}
if (i - prev[fac] <= k) {
merge(i, prev[fac]);
}
prev[fac] = i;
}
}
ll ans = 0;
for (int i = 1; i < n * 2; i++) {
if (a[i % n] == 1) {
dbg("found 1 at %d\n", i);
if (i <= n)
ans += n;
} else if (find(i) == i) {
dbg("found root at %d\n", i);
ans++;
}
}
printf("%lld\n", ans);
}
int main()
{
init_p();
scanf("%d", &t);
for (int test_case = 0; test_case < t; ++test_case) {
// Read input for each test case
scanf("%d %d", &n, &k);
a.resize(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
// Solve the test case
solve();
// Clear input data for the next test case
clearData();
}
return 0;
}