Codeforces Round #250 (Div. 2), problem: (B) The Child and Set editorial: http://codeforces.com/blog/entry/12513
Codeforces Round #250 (Div. 2), problem: (B) The Child and Set solution: http://ideone.com/J22uP6
#include <iostream> #include <cstdio> using namespace std; int main() { int sum, lim, low_bit, n=0, a[100005]; scanf("%d%d", &sum, &lim); for(int i=lim; i>=1; i--) { low_bit = i & -i; if(sum>=low_bit) a[n]=i, n++, sum-=low_bit; if(sum==0) break; } if(sum>0) printf("-1"); else { printf("%d\n", n); while(n--) printf("%d ", a[n]); } return 0; }
note: to get lowest bit of binary representations of numbers, please look: http://www.allaboutcircuits.com/vol_4/chpt_2/3.html
hey my code is giving correct ans on my machine everytime , but is giving runtime error on the sample test case, when submitting:
ReplyDelete#include
#include
#include
#include
#include
#include
using namespace std;
struct storagee
{
long int valuee;
long int low_bit_value;
int flag;
};
struct storagee *memoryy;
long int set_low_bit_value(long int num)
{
long int i,temp,temp1;
for (i = 0; pow(2,i)<= num; i++)
{
temp=num&(1<low_bit_value;
int r = ((struct storagee *)q)->low_bit_value;
return (r - l);
}
int main()
{
long int summ,limitt,count=0,i,j,flag2=0;
scanf("%ld %ld",&summ,&limitt);
memoryy=(struct storagee*)malloc((limitt+1)*sizeof(struct storagee));
for (int i = 1; i <=limitt; i++)
{
memoryy[i].flag=0;
memoryy[i].valuee=i;
memoryy[i].low_bit_value=set_low_bit_value(i);
}
qsort (memoryy+1, limitt+1, sizeof(struct storagee), mem_sorter);
for (int i = 1; i <=limitt; i++)
{
if(summ==0 && flag2==0)
{
printf("%ld\n",count);
for(j=1;j<=limitt;j++)
{
if(memoryy[j].flag==1)
printf("%ld ",memoryy[j].valuee);
}
printf("\n");
flag2=1;
break;
}
else
{
summ-=memoryy[i].low_bit_value;
memoryy[i].flag=1;
count++;
if(summ<0)
{
summ+=memoryy[i].low_bit_value;
memoryy[i].flag=0;
count--;
}
}
}
if(summ!=0)
printf("-1\n");
if(summ==0 && flag2==0)
{
printf("%ld\n",count);
for(j=1;j<=limitt;j++)
{
if(memoryy[j].flag==1)
printf("%ld ",memoryy[j].valuee);
}
printf("\n");
}
return 0;
}