## 545

Moderator: Board moderators

malloc() declares memory but not initializes it. So, you must initialize y[0] before using it. Hope it helps.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru

Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm

I did it, but it still doesn't work WA. Do you have any others ideas?
amine.hamdaoui
New poster

Posts: 10
Joined: Tue Aug 07, 2007 7:33 pm

### Re: 545

amine.hamdaoui wrote:Why do i get WA. I use exact calculation. I have tested my code for all the tests in this topic, and it match!!!!
Code: Select all
`#include <stdio.h>#include <stdlib.h>int main(){int i,j,r,n,*y,a;double *x;y = (int *) malloc(sizeof(int )*1000001);x = (double *) malloc(sizeof(double)*1000001);x[0] = 1;y[0] = 0;for(j=1 ; j<1000001 ; j++){x[j] =x[j-1]/2.0;if(x[j]<1){x[j] *=10.0;y[j]=y[j-1]+1;}else y[j]=y[j-1];}scanf("%d",&r);for(i=1 ; i<=r ; i++){scanf("%d",&n);printf("2^-%d = %1.3fE-%d\n",n,x[n],y[n]);}return 0;}`

I would recommand you to use interget to substitute double and perform exact calculation, since it maybe lost precison due to the using of double.

P.S. After two times W.A. by using the formula of 474, I decided to use exact calculation by interger and got A.C. finally.
Have you ever...

Wanted to work at best companies?
Struggled with interview problems that could be solved in 15 minutes?
Wished you could study real-world problems?
If so, you need to read Elements of Programming Interviews.
DD
Experienced poster

Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California

### Re: 545

I solved both 545 and 474 without log or log10 or pow or bigint. For 474, after you calculate the result, use the following snippet to output your result.
cout << showpoint << setprecision(4) << result;
The correct output for 6 is 1.562e-2 and not 1.563e-2 for 474. This is because the number is not rounded in this problem.

For 545, I got Wrong Answer thrice before I got Accepted. After I got Accepted, I thought I would compare the output of my programs and I wanted to post here some special cases, but guess what? The two outputs incisively matched on codeblocks. If you want to get accepted then one hint (I got accepted after adding 3 lines):

Consider the numbers 1.11450, 1.11550, 1.11650 and 1.11650.
Say x = 1.11450. Now if you print x with %0.3lf then the outputs for the four numbers will be:
1.115, 1.115, 1.117 and 1.117
Modify your code so that the output for these four numbers are instead:
1.115, 1.116, 1.117 and 1.118

Even after this modification, all the values from 1 to 9000 still remain unchanged(at least on my compiler) but for some eerie reason, the online judge doesn't accept without this rounding alteration.
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

plamplam
Experienced poster

Posts: 151
Joined: Fri May 06, 2011 11:37 am

### Re: 545

I got AC after replacing long double with double ! Used log10, pow and cin<<fixed and cin.precision(3) cin.precision(0)
xander7b
New poster

Posts: 3
Joined: Wed Aug 25, 2010 4:57 pm

### Re: 545

I honestly do not know what's wrong with my code. I used the square and double method, which worked perfectly well on 474, and is failing hard here. Eventually I took the suggestion of using BigInt, and got it accepted. I still have no idea what's wrong though, since both code gives the same output for all 9000 values.
achan8501
New poster

Posts: 6
Joined: Mon Nov 05, 2012 9:13 pm

Previous