BISHOPS - Bishops

Yesterday was Sam's birthday. The most interesting gift was definitely the chessboard. Sam quickly learned the rules of chess and defeated his father, all his friends, his little sister, and now no one wants to play with him any more.

So he decided to play with another birthday gift – a Book of Math Problems for Young Mathematicians. He opened the book somewhere in the middle and read the following problem: "How many knights can be placed on a chessboard without threatening each other?" After a while he realized that this was trivial and moved on to the next problem: "How many bishops can be placed on a chessboard without threatening each other?". Sam is in trouble here. He is not able to solve this problem and needs your help.

Sam's chessboard has size N x N. A bishop can move to any distance in any of the four diagonal directions. A bishop threatens another bishop if it can move to the other bishop's position. Your task is to compute the maximum number of bishops that can be placed on a chessboard in such a way that no two bishops threaten each other.


The input file consists of several lines. The line number i contains a single positive integer N representing the size of the i-th chessboard. [1 <= N <= 10^100]


The output file should contain the same number of lines as the input file. The i-th line should contain one number – the maximum number of bishops that can be placed on i-th chessboard without threatening each other.




hide comments
amankumarjha: 2018-01-01 12:34:13

Learnt to use left shift and, python3 stackoverflow: You can but you shouldn't try: ... except: block, it accepts all exceptions. String is another alternative.

swarup03: 2017-07-16 12:15:15

Using a language like Java or Python makes it very simple but one must give it a try with C/C++ if you are a beginner.

Simran Saha: 2017-07-13 21:12:57

what is the corner case for this problem?!? Handled for n == 1 and used strings too.

manmeet_rana: 2017-07-13 11:28:09

those who are getting nzec in python run loop for 1024 times

Anand Kumar: 2017-06-25 12:26:12

finally solved the problem in c++, 3 WA.. :(

ramesh_961: 2017-06-04 08:59:55

Easy ! Get the logic! then look for one corner case!!! And handle when n==1.
In c/c++ use while(cin>>str)
{ //your code //

Last edit: 2017-06-04 09:04:39
ayushgupta1997: 2017-05-29 18:51:47

cakewalk in java :) ,notice n=1,costed me 1 wa, indeed learn about equality of bigintegers :)

sagnik_66: 2017-05-25 19:50:55

Easy :D

sid_1462: 2017-05-23 12:47:29

getting WA in cpp even after using strings, any suggestions ?

sushantgokhale: 2017-04-04 09:37:23

@harshit. Can u mail me your code at

Added by:Roman Sol
Time limit:1s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Resource:IPSC 2004