This Forum is in read only mode now.

You are not logged in. Please login at to post your questions!


Why both code having same logic don't work? [SUBPRNJL]

I got WA when used

m = ceil((float(k)/float(r-l+1));

but got AC when used

m = k/(r-l+1) + k%(r-l+1)==0?0:1;

Can somebody tell me the difference between the two?

My submission using both: - using ceiling function - using ternary operator

asked 18 Mar, 22:23

karasu's gravatar image

accept rate: 0%

edited 20 Mar, 03:05

float type is single-precision floating point and it has significand precision of 24 bits. This means that it cannot accurately represent integers above $2^{24}$, which is much lower than the maximum value of $k$ which is $10^9$. This introduces an error in your ceil calculation.

An easy way to compute ceil(a / b) for integers a, b without involving floats is (a + b - 1) / b.


answered 24 Mar, 05:16

meooow's gravatar image

6★meooow ♦
accept rate: 48%

edited 24 Mar, 05:20

Thank you tried with with long double and it worked and also thanks for the tip for calculating ceil function. Much appreciated.

(25 Mar, 11:08) karasu4★

Loved the (a+b-1)/b tip!

(25 Mar, 12:25) akashbhalotia5★

Let k=8, (r-l+1)=3

Then the first value of m is 4. While the second line checks whether 8/3 + 8%3 == 0, which simplifies to 4==0. I haven't seen the rest of your code, but this observation might help you.


answered 21 Mar, 12:02

udayan14's gravatar image

accept rate: 0%

the value of m in 1st is 3, ceil(8/3)=ceil(2.6)=3 You calculated wrong.

(24 Mar, 03:52) karasu4★

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 18 Mar, 22:23

question was seen: 3,329 times

last updated: 25 Mar, 12:45