#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// Function to calculate the sum of divisors of a number
int sumDiv(int num)
{
int sum = 0;
for (int i = 1; i * i <= num; i++)
{
if (num % i == 0)
{
sum += i;
if (i != num / i)
{
// Add the complementary divisor
sum += num / i;
}
}
}
return sum;
}
unsigned int getFirstSetBitPos(int n)
{
return log2(n & -n) + 1;
}
// Topological Sort using DFS
void dfs(int v, vector<vector<int>>& adj, vector<bool>& visited, vector<int>& ans)
{
visited[v] = true;
for (auto u : adj[v])
{
if (!visited[u])
{
dfs(u, adj, visited, ans);
}
}
ans.push_back(v); // Add to result after visiting all neighbors
}
// Check if there is a cycle in the graph
bool hasCycle(int v, vector<vector<int>>& adj, vector<bool>& visited, vector<bool>& recStack)
{
visited[v] = true;
recStack[v] = true;
for (auto u : adj[v])
{
if (!visited[u] && hasCycle(u, adj, visited, recStack))
return true;
else if (recStack[u]) // If the node is in the recursion stack, cycle is present
return true;
}
recStack[v] = false;
return false;
}
// Function to check if the graph is a DAG
bool isDAG(vector<vector<int>>& adj, int n)
{
vector<bool> visited(n + 1, false), recStack(n + 1, false);
for (int i = 1; i <= n; ++i)
{
if (!visited[i] && hasCycle(i, adj, visited, recStack))
return false;
}
return true;
}
// Function to construct the dependency graph
void buildGraph(int n, vector<vector<int>>& adj, int C)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j) continue;
if (i % j == 0)
adj[i].push_back(j); // i must appear before j
else if (getFirstSetBitPos(i) >getFirstSetBitPos(j))
adj[i].push_back(j); // i must appear before j
}
}
}
void testcase()
{
cout << getFirstSetBitPos(6)<<endl;
int n, C;
cin >> n >> C;
vector<vector<int>> adj(n + 1);
buildGraph(n, adj, C);
if (!isDAG(adj, n))
{
cout << "Graph contains a cycle. Topological sort not possible.\n";
return;
}
// Perform topological sort
vector<int> res;
vector<bool> visited(n + 1, false);
for (int i = 1; i <= n; ++i)
{
if (!visited[i])
{
dfs(i, adj, visited, res);
}
}
reverse(res.begin(), res.end());
// Output the result
for (auto x : res)
{
cout << x << " ";
}
cout << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
speed
int t = 1;
while (t--)
{
testcase();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIHNwZWVkIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwoKLy8gRnVuY3Rpb24gdG8gY2FsY3VsYXRlIHRoZSBzdW0gb2YgZGl2aXNvcnMgb2YgYSBudW1iZXIKaW50IHN1bURpdihpbnQgbnVtKQp7CiAgICBpbnQgc3VtID0gMDsKICAgIGZvciAoaW50IGkgPSAxOyBpICogaSA8PSBudW07IGkrKykKICAgIHsKICAgICAgICBpZiAobnVtICUgaSA9PSAwKQogICAgICAgIHsKICAgICAgICAgICAgc3VtICs9IGk7CiAgICAgICAgICAgIGlmIChpICE9IG51bSAvIGkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vIEFkZCB0aGUgY29tcGxlbWVudGFyeSBkaXZpc29yCiAgICAgICAgICAgICAgICBzdW0gKz0gbnVtIC8gaTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBzdW07Cn0KCnVuc2lnbmVkIGludCBnZXRGaXJzdFNldEJpdFBvcyhpbnQgbikKewogICAgcmV0dXJuIGxvZzIobiAmIC1uKSArIDE7Cn0KLy8gVG9wb2xvZ2ljYWwgU29ydCB1c2luZyBERlMKdm9pZCBkZnMoaW50IHYsIHZlY3Rvcjx2ZWN0b3I8aW50Pj4mIGFkaiwgdmVjdG9yPGJvb2w+JiB2aXNpdGVkLCB2ZWN0b3I8aW50PiYgYW5zKQp7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