113 - Power of Cryptography

All about problems in Volume 1. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
40019JR
New poster
Posts: 9
Joined: Fri Dec 12, 2003 11:41 pm

Why Wrong Answer? 113

Post by 40019JR » Sat Dec 13, 2003 8:40 pm

[cpp]
This is my C++ answer for 113.
I got WA. However, I don't understand why. I thought it perfectly solves the problem. Can anyone help me?
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double n, k, p;

while (cin >> n >> p)
{

k = pow(p,(1/n));
cout << k << endl;
}
return 0;
} :oops: [/cpp]

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Sun Dec 14, 2003 6:17 am

Hi, I corrected the following line and got AC.

[cpp]cout << k << endl;[/cpp]

Most probably the default number digit after decimal is 6, so in UVA compiler your ans. for
2
16
would be
4.000000 and not 4.

Change it to the following line.
[cpp]printf("%.0lf\n",k); [/cpp]

40019JR
New poster
Posts: 9
Joined: Fri Dec 12, 2003 11:41 pm

I got compiler errors

Post by 40019JR » Sun Dec 14, 2003 6:31 am

Thanks for your reply.
I corrected the line and sent it. However, I got following compiler errors.
Also, when I run it on VC, I get no error but the output is funny. I mean, this doesn't solve the problem.
So if you have any thought on that, I appreciate it.
02143378_24.c: In function `int main ()':
02143378_24.c:14: `printf' undeclared (first use this function)
02143378_24.c:14: (Each undeclared identifier is reported only once for
each function it appears in.)

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

Post by Almost Human » Sun Dec 14, 2003 2:48 pm

the library iostream used by the judge's compiler doesn't support the printf function, so you have to include another library ( stdio.h )

I hope it helped you

40019JR
New poster
Posts: 9
Joined: Fri Dec 12, 2003 11:41 pm

Thanks.

Post by 40019JR » Sun Dec 14, 2003 5:10 pm

Thanks everyone. It worked.
But like how did you know that their compiler interpret it down to 6 decimal points and stuff like that?
Just out of curiosity.

Almost Human
Learning poster
Posts: 93
Joined: Sun Jan 12, 2003 3:30 pm

Post by Almost Human » Mon Dec 15, 2003 5:01 am

I guess experience would be the greatest teacher in that case. :wink:

Jer
New poster
Posts: 10
Joined: Wed Nov 19, 2003 2:07 pm

where is the bug?? can you help me 113~~~

Post by Jer » Fri Jan 02, 2004 6:46 pm

help me~~ plz~~

[cpp]
#include <iostream.h>
#include <math.h>
int main(void)
{
double p,n;
while(cin>>n>>p) {
cout<<pow(p,(double)1/n)<<endl;
}
return 0;
}[/cpp]

pavelph
Learning poster
Posts: 57
Joined: Wed Dec 10, 2003 7:32 pm
Location: Russia, Saint-Petersburg

Post by pavelph » Fri Jan 02, 2004 7:43 pm

1<=p<10^101
I think that double not support so big numbers.

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Fri Jan 02, 2004 11:36 pm

No, double is enough. I have a similar solution, but with two more lines at the begining of the main function:
[cpp] cout.setf(ios::fixed);
cout.precision(0); [/cpp]
I don't remember why I put them there, but there must have been a reason :-)

Jer
New poster
Posts: 10
Joined: Wed Nov 19, 2003 2:07 pm

Post by Jer » Sat Jan 03, 2004 10:19 am

Krzysztof Duleba wrote:No, double is enough. I have a similar solution, but with two more lines at the begining of the main function:
[cpp] cout.setf(ios::fixed);
cout.precision(0); [/cpp]
I don't remember why I put them there, but there must have been a reason :-)
I fix the code
[cpp] cout<<(long)pow(p,(double)1/n)<<endl [/cpp]
and some answers will be decrease like 1234->1233
so i think the answers' form i calculate are wrong......

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Jan 03, 2004 3:18 pm

So you finally got accepted?

Jer
New poster
Posts: 10
Joined: Wed Nov 19, 2003 2:07 pm

Post by Jer » Sat Jan 03, 2004 4:27 pm

Oh!!yes....~~thank you~~

aditya
New poster
Posts: 5
Joined: Mon Jan 12, 2004 12:44 pm

Post by aditya » Thu Feb 12, 2004 5:16 pm

i am trying to use the double data type but if i input a number having more than say 20 digits and then just print it ( no processing ) , i am not getting the same number that i had entered. after 20 digits the precision is lost.
could anyone pls suggest why it is so?

p.s. i have even tried long double

Sayutee
New poster
Posts: 9
Joined: Wed Jun 04, 2003 7:50 am
Location: Bangladesh

Post by Sayutee » Tue Feb 17, 2004 8:57 pm

Your code is too much complex i think- the problem is not so much complex.

I got WA with double but AC with Long Double.

I used natural log instead of base 10.

k = exp(log(p)/n) is enough to solve it.

best of luck

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Wed Feb 18, 2004 5:57 am

I used double and got AC.

I think, the judge's data has no such case where precision will be lost.

Post Reply

Return to “Volume 1 (100-199)”