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

×

What is the bothering factor in these 2 submissions?

Hi, I tried submitting a solution for the Calculator problem from the recently concluded July Challenge. The first submission fetched me a partial result whereas the second submission fetched full points. The only change I did was that I added long long to the third argument of the function findans. Can someone explain the intricacy behind this please?

asked 18 Jul '17, 21:35

chari407's gravatar image

2★chari407
43726
accept rate: 0%


Its this line-

long long int ans=(n-(b*x))*x;

To explain it clearly, let me first recount a very common example-

int b=5,c=2;
double a;
a=b/c;

We all know that value of a here is 2, not 2.5, despite being double, and we explain it by saying that both 5 and 2 are int, so data is kept in int-type and not promoted to double before storing.

Similarly, here, when you do (n-(b*x))*x it keeps the result in int (just like case above), DOES NOT promote it to long long int, due to which overflow happens. Then this overflowed result is stored in variable, which is declared long long int. But when x is long long int, it automatically promotes the expression to higher data type (long long int) and this prevents overflow.

link

answered 18 Jul '17, 23:12

vijju123's gravatar image

4★vijju123 ♦
12.6k1425
accept rate: 19%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×243

question asked: 18 Jul '17, 21:35

question was seen: 231 times

last updated: 18 Jul '17, 23:12