Cod sursa(job #376277)

Utilizator alexandru92alexandru alexandru92 Data 21 decembrie 2009 10:44:35
Problema Submultimi Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#define nr_sub(x) 1<<(x)
#define bit_check(x, poz) ( (x) & ( 1 <<(poz) ) )

/*
 *
 */
using namespace std;
inline unsigned int get_next_n( unsigned x )
{
      unsigned smallest, new_smallest, ripple, ones;
      smallest=( x & -x );
      ripple= x+smallest;
      new_smallest=( ripple & -ripple );
      ones=( ( new_smallest/smallest)>>1 )-1;
      return ripple | ones;
}
int main()
{int m;
 unsigned int n, till, i, j, k;
    ifstream in("submultimi.in");
    in>>n;
    till=nr_sub(n);
    ofstream out("submultimi.out");
    for( i=1; i <= n; ++i )
    {
         for( j=(1<<i)-1; j < till; j=get_next_n(j) )
         {
             for( m=i, k=1; k <=n && m; ++k )
                if( bit_check( j, k-1 ) )
                {
                   --m;
                    out<<k<<' ';
                }
             out<<'\n';
         }
    }
    return 0;
}