Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | C ++ program for the above approach ; Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; update n ; Print the ans in sorted order ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <stdio.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE void printSubset ( lli n , int k ) { vector < lli > answer ; while ( n > 0 ) { lli p = log2 ( n ) ; answer . push_back ( pow ( k , p ) ) ; n %= ( int ) pow ( 2 , p ) ; } reverse ( answer . begin ( ) , answer . end ( ) ) ; for ( auto x : answer ) { cout << x << " ▁ " ; } } int main ( ) { lli n = 5 ; int k = 4 ; printSubset ( n , k ) ; }