## 11121 - Base -2

Moderator: Board moderators

hamedv
Learning poster
Posts: 98
Joined: Mon May 07, 2007 8:30 am
In line 21 :
Change
for(k=1;k<ks;k++){
to
for(k=1;k<=ks;k++){
lovemagic
Learning poster
Posts: 52
Joined: Thu Oct 02, 2003 11:38 am
thanx khobaib
bishop
New poster
Posts: 43
Joined: Fri May 04, 2007 12:57 pm

### why 11121 Base -2WA

can anyone tell me why WA

Code: Select all

``````

``````
Last edited by bishop on Fri Aug 03, 2007 8:07 am, edited 1 time in total.
kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Contact:
check ur code for these input:

Code: Select all

``````5

8
9
-9
-11
-13``````
correct output will be:

Code: Select all

``````11000
11001
1011
110101
110111``````
best of luck TD! Sanjana
turcse143
Learning poster
Posts: 81
Joined: Wed May 09, 2007 9:59 pm
i got AC by using the recursion algorithem.
simple comparing the numbers.
use this example:
Keep in mind this simple algerbraic formula: if a / b = c remainder d, then bc + d = a.

Thus, to expand decimal 2 in negabinary:

2 / -2 = -1 remainder 0
-1 / -2 = 1 remainder 1
1 / -2 = 0 remainder 1

Taking those remainders in reverse order we get 110. So 110 is the negabinary representation of decimal 2.
hope it helps.
''I want to be most laziest person in the world''
New poster
Posts: 32
Joined: Tue Feb 13, 2007 1:31 pm

### Re: 11121 - Base -2

test cases

Code: Select all

``````4
1000000000
-1000000000
1024
-1024
``````

Code: Select all

``````Case #1: 1001100111011111101111000000000
Case #2: 11000101101001010100101000000000
Case #3: 10000000000
Case #4: 110000000000
``````
hope it helps.
snape
New poster
Posts: 5
Joined: Sat Dec 03, 2011 4:18 pm

### 11121-Base -2

11121,plz help. after hard work i code this,bt get TLE && thn RE
if anyone can tell me if i will need to change my logic,thn i will.
if any one can tell me in this logic how i recover this RE & TLE.

my code is here:

#include <stdio.h>
#include <math.h>
#define N 2000000
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
long long int d=-2,e=2,n,b,z,i,sum=0,c,r=0;
char a[N];
int main()
{
//d=-2;
//e=2;
scanf("%lld",&n);
getchar();
while(r<n)
{
scanf("%lld",&b);

z=b;

i=0;

if(i==0)
{
if(b%2==0)
{
a='0';

c=0;

sum+=(a-48);

i++;

}
else{
a='1';

c=1;

sum+=(a-48);

i++;

}
}

while(1)
{
if(sum==z)
break;

if(b>0)
{
b=(b-c)/d;

if(b%2!=0)
{
a='1';

c=1;

sum+=((a-48)*pow(d,i));

i++;
}
else{
a='0';
c=0;
sum+=((a-48)*pow(d,i));

i++;

}
}

if(b<0)
{
b=(b-c)/e;

if(b%2!=0)
{
a='1';

c=1;

sum+=((a-48)*pow(d,i));

i++;

}
else{
a[i]='0';

c=0;

sum+=((a[i]-48)*pow(d,i));

i++;

}
}

}
i=i-1;
printf("Case #%lld: ",r+1);
reverse(a,a+strlen(a));
printf("%s",a);

if(r<(n-1))
printf("\n");
r++;

sum=0;
}

return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11121-Base -2

Use the existing thread. Use the code blocks.

Your program fails for the sample input and you also don't print a newline after the last case. It looks like you're not re-initializing all your variables.
Check input and AC output for thousands of problems on uDebug!
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm