Link to the question
Solution of BLOPER spoj - spoj Operators
Maximum & Minimum Sum possible with N natural number is n*(n+1)/2 and out of it we need to take S. then we'll left with N - S. So, our aim is to make the final N-S effect zero therefore we divide N - S in two equal part and it only possible when N-S is an even number hence would be "Impossible" for odd count. After dividing if possible let us say S1,S2 (equal in magnitude), all we'll have to do is make S1 - S2.
Rest i leave it upto you. And think over it when S is negative.
Solution of BLOPER spoj - spoj Operators
Maximum & Minimum Sum possible with N natural number is n*(n+1)/2 and out of it we need to take S. then we'll left with N - S. So, our aim is to make the final N-S effect zero therefore we divide N - S in two equal part and it only possible when N-S is an even number hence would be "Impossible" for odd count. After dividing if possible let us say S1,S2 (equal in magnitude), all we'll have to do is make S1 - S2.
Rest i leave it upto you. And think over it when S is negative.
#include<stdio.h>
int main()
{
int arr[505]={0},i,j,n,s;
scanf("%i%i",&n,&s);
if(s < 0)
{
j = (n*(n+1))/2;
if( (j+s)&1 || j <= -1*s )
{
printf("Impossible\n");
}
else
{
j = (j + s)/2 - 1;
i = n+1;
while(--i > 1 && j)
{
if( j - i > 1)
{
j -= i;
arr[i] = 1;
}
else if(j-i == 0)
{
j -= i;
arr[i] = 1;
break;
}
}
if(j)
printf("Impossible\n");
else
{
printf("1");
i = 1;
while(++i <= n)
{
arr[i] ? printf("+%i",i) : printf("-%i",i);
}
printf("\n");
}
}
}
else
{
j = (n*(n+1))/2;
if( (j-s)&1 || j < s )
{
printf("Impossible\n");
}
else
{
j = (j - s)/2;
i = n+1;
while(--i > 1 && j)
{
if( j - i > 1)
{
j -= i;
arr[i] = 1;
}
else if(j-i == 0)
{
j -= i;
arr[i] = 1;
break;
}
}
if(j)
{
printf("Impossible\n");
}
else
{
printf("1");
i = 1;
while(++i <= n)
{
arr[i] ? printf("-%i",i) : printf("+%i",i);
}
printf("\n");
}
}
}
return 0;
}
WTF, understood the logic but couldn't understand the uncommented code :/
ReplyDelete