Sunteți pe pagina 1din 4

The very first step of getting started with online judge is to understand:

How to read input data?


How to output the answer?
If, input data is read from standard input stream (STDIN) and results are printed to standard
outputstream (STDOUT). Most of the questions will deal with either integers or strings.
An example C code to read an integer from STDIN and printing it out to STDOUT is shown
below.
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n); //read input integer from STDIN
printf("%d",n); //print output integer to STDOUT
return 0;
}

Sample code snippet to read integer for all other languages are given in the code editor below.
An example C code to read a string from STDIN and printing it out to STDOUT.
#include <stdio.h>
int main()
{
char a[100]; //assuming that string size will not exceed 100
scanf("%s",a); //read a string in array a
printf("%s",a); //print out array a
return 0;
}

Careful not to print any thing else in the output, such as printf("The answer is
%d",n); because the online judge directly compares your output to expected output like normal
string comparison. Every problem will also have Constraints section which helps in determining
what size of array to be created or what datatypes to use, say int or long long.

basics of Bit Manipulation


by Anand Jaisingh

Bits are the fundamental unit of data storage in any computer based system. Large number
of bits come together to form bytes, large number of bytes come together to form kilobytes and so on. Thus, this fundamental unit of storage , that is bits have a very important role to
play in solving a variety of problems and many a times make much more complex tasks simpler.
It is known that 11 byte comprises of 88 bits and any integer or character can be represented
using bits in computers, which is known as its binary form contains only 11 or 00 in its
base 22 form.
Example:
1) 1414 = 1110211102 = 123+122+121+020123+122+121+020
= 1414
2) 2020 = 101002101002
= 124+023+122+021+020124+023+122+021+020 = 2020
For characters, we can use ASCII representation, which are in the form of integers which again
can be represented using bits as explained above.
Bitwise Operators:
Bitwise operator are used to perform opertions on individual bits. As we use the addition,
subtraction operators on real numbers, similarily bitwise operators are used to do the same with
individual bits.
There are a variety of bitwise operators that are as follows:
NOT ( ~ ): Bitwise NOT is an unary operator that flips the bits of the number i.e., if the ithith bit
is 00, it will change it to 11 and vice versa. Bitwise NOT is nothing but simply the ones
complement of a number. Consider an example.

NN = 55 = 10121012 ~NN = ~55 = ~10121012 = 01020102=22


AND ( & ): Bitwise AND is a binary operator that operates on two equal-length bit patterns. If
both bits in the compared position of the bit patterns are 11, the bit in the resulting bit pattern
is 11, otherwise 00.
AA = 55 = 10121012, BB = 33 = 01120112 A & B A & B = 10121012 & 01120112=

00120012=11
OR ( | ): Bitwise OR is also a binary operator that operates on two equal-length bit patterns,
similar to bitwise AND. If both bits in the compared position of the bit patterns are 00, the bit in
the resulting bit pattern is 00, otherwise 11.
AA = 55 = 10121012 , BB = 33 = 01120112 A|BA|B = 10121012 | 01120112 = 11
121112=77
XOR ( ^ ): Bitwise XOR also takes two equal-length bit patterns. If both bits in the compared
position of the bit patterns are 00 or 11, the bit in the resulting bit pattern is 00, otherwise 11.
AA = 55 = 10121012 , BB = 33 = 01120112 AA^BB = 10121012 ^ 01120112 =1102
1102= 66
Left Shift ( << ): Left shift operator is a unary operator which shifts some number of bits, in the
given bit pattern, to the left and append 00 at the end. Left shift is equivalent to multiplying the
bit pattern with 2k2k (if we are shiftingkk bits).

1<<11<<1 = 22 = 2121
1<<21<<2 = 44 = 2222
1<<31<<3 = 88 = 2323
1<<41<<4 = 1616 = 2424
1<<n1<<n = 2n2n

Right Shift ( >> ): Right shift operator is a unary operator which shifts some number of bits, in
the given bit pattern, to the right and append 00 at the end. Right shift is equivalent to dividing
the bit pattern with 2k2k (if we are shifting kk bits).

4>>14>>1 = 22
6>>16>>1 = 33
5>>15>>1 = 22
16>>416>>4 = 11

Bitwise operators are good for saving space and sometimes to cleverly remove dependencies.
Have a look at this simple application of this technique for finding the number of 11s in binary
representation of a number.
The basic approach to evaluate the binary form of a number is to traverse on it and count the
number of ones. But this approach takes log2Nlog2N time in every case.
Why log2Nlog2N time?
As to get a number in its binary form, we have to divide it by 22, until it gets 00, which will
take log2Nlog2N time.
With bitwise operations, we can use an algorithm whose running time depends on the number of
ones present in the binary form of the given number. This algorithm is much better, as it will
reach to logNlogN, only in its worst case.
int count_one (int n)
{
while(n)
{
n = n & (n-1);
count++;
}
return count;
}

Why this algorithm works ?


As explained in the previous algorithm, the relationship between the bits of xx and x1x1. So
as in x1x1, the rightmost 11 and bits right to it are flipped, then by performing x&(x-

1) x&(x-1), and storing it in xx, will reduce xx to a number containing number of ones (in its
binary form) less than the previous state of xx, thus increasing the value of count in each
iteration.
Example:
nn = 2323 = 101112101112 1. Initially, count = 00

1. Now, n will change to n&(n-1) n&(n-1). As n1n1 = 2222 = 101102101102 ,


then n&(n-1) n&(n-1) will be 101112101112 & 101102101102, which will
be 101102101102 which is equal to 2222. Therefore nn will change to 2222 and
count to 11.
2. As n1n1 = 2121 = 101012101012 , then n&(n-1) n&(n-1) will
be 101102101102 & 101012101012, which will be 101002101002 which is
equal to 2020. Therefore nn will change to 2020 and count to 22 .
3. As n1n1 = 1919 = 100112100112 , then n&(n-1) n&(n-1) will
be 101002101002 & 100112100112, which will be 100002100002 which is
equal to 1616. Therefore nn will change to 1616 and count to 33.
4. As n1n1 = 1515 = 011112011112 , then n&(n-1) n&(n-1) will
be 100002100002 & 011112011112, which will be 000002000002 which is
equal to 00. Therefore nn will change to 00 and count to 44.
5. As nn = 00, the the loop will terminate and gives the result as 44.
Complexity: O(K)O(K), where KK is the number of ones present in the binary form of the
given number.

S-ar putea să vă placă și