-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUnit GCD.cpp
92 lines (87 loc) · 2.3 KB
/
Unit GCD.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 <iostream>
using namespace std;
#include <iostream>
#include <vector>
#include <sstream> // for string streams
#include <string>
#define REP(i, n) for (int i = 0; i < (n); i++)
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
cin.sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
bool status[n + 1] = {false}; // 0 th index left not considered
int start_pointer = 2;
string str = "";
int c = 0;
vector<int> container;
container.push_back(1);
bool work_on_pointer = true;
while (work_on_pointer)
{
container.clear();
container.push_back(1);
for (int i = start_pointer; i <= n; i++)
{
bool k = true;
for (int j = 0; j < container.size(); j++)
{
if (!(gcd(i, container[j]) == 1))
{
k = false;
}
}
if (k == true && status[i] == false)
{
container.push_back(i);
status[i] = true;
}
//
for (int j = 2; j <= n; j++) //search for next start pointer
{
if (status[j] == false)
{
start_pointer = j;
break;
}
}
}
str += to_string(container.size()) + " ";
for (int j = 0; j < container.size(); j++)
{
str += (to_string(container[j])) + " ";
}
str += "\n";
c++;
bool temp = true;
for (int j = start_pointer; j <= n; j++) //search for next start pointer
{
if (status[j] == false)
{
start_pointer = j;
work_on_pointer = true;
break;
}
else
{
work_on_pointer = false;
}
}
}
//cout << "Final Answer\n";
cout << c << "\n"
<< str;
}
return 0;
}