Cod sursa(job #376283)

Utilizator alexandru92alexandru alexandru92 Data 21 decembrie 2009 10:53:10
Problema Combinari Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 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 i, j, n, k, till, m;
	ifstream in("combinari.in");
	in>>n>>k;
	till=nr_sub(n);
        ofstream out("combinari.out");
	for( i=(1<<k)-1; i < till; i=get_next_n(i) )
        {
	    for( m=k, j=1; j <= n && m ; ++j )
                if( bit_check( i, j-1 ) )
                {
                   --m;
		   out<<j<<' ';
                }
            out<<'\n';
        }
	return 0;
}