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

Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor »

I don't know what is your problem, but I used

Code: Select all

while (scanf("%lf", &d) == 1)
{
  /* code here */
}
And it got accepted.

Ivor

NONAME_SUST
New poster
Posts: 8
Joined: Tue Jul 23, 2002 9:15 am

THIS IS NOT MASSIVE

Post by NONAME_SUST »

Hi,
No..The problem is in ur declare. Declare the variable as double and then use "%lf" to read the inputs. Like this way...

double d;
scanf("%lf",&d);

You may also truncate the zeroes after decimal.
HEY WANT TO GET SOL.TRY ON http://www.uvexam.zzn.com from 1st September

taj79
Learning poster
Posts: 74
Joined: Sun Jun 09, 2002 11:56 am
Location: India
Contact:

Post by taj79 »

i m stiil getting error
my p is 4357186184021382204544

when
double p;
scanf("%Lf",p);
printf("%0.0f",p);

ANS is 0


when
double p;
scanf("%Lf",p);
printf("%0.0Lf",p);

ANS is 280585630299556481757617220275852364727636170628410927600364713659709368033262555286057473837891584000

when
double p;
scanf("%lf",p);
printf("%0.0lf",p);

ANS is 0

when
double p;
scanf("%lf",p);
printf("%0.0f",p);

ANS is 0



what should i do???

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

use:
double p;
scanf("%lf",&p);
printf("%.0lf",p);

zhartley
New poster
Posts: 8
Joined: Tue Jul 23, 2002 2:38 am
Location: High Point, North Carolina / Goodlettsville, Tennesee
Contact:

Post by zhartley »

Any idea why I get wrong answers for both of the following?:

[cpp]

/* @JUDGE_ID: 21301HT 113 C++ */
#include <iostream>
#include <cmath>
using namespace std;

int main(void) {

register long long n, p, k;
while (cin >> n >> p) {
k = static_cast<long long>(pow(p, (1.0 / n)));
cout << k << endl;
}

return 0;

}
@end_of_source_code
[/cpp]

or this (same thing using long doubles):

[cpp]

/* @JUDGE_ID: 21301HT 113 C++ */
#include <iostream>
#include <cmath>
using namespace std;

int main(void) {

register long double n, p, k;
while (cin >> n >> p) {
k = pow(p, (1.0 / n));
cout << k << endl;
}

return 0;

}
@end_of_source_code
[/cpp]

Any ideas?

taj79
Learning poster
Posts: 74
Joined: Sun Jun 09, 2002 11:56 am
Location: India
Contact:

Post by taj79 »

i have tried what adrian has said....but still error.....
my p is 4357186184021382204544

but my prog output is 4357186184021382004736

the difference is in the last few digits......
i have noticed that i had forgotten & in scanf earlier but this time i did what adrian had told me..

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

Why do you worry about this? Of course the precision of double is not enough for all these digits, but it would be sufficient if only the 10 first digits were correct. I read the first 10 digits manually into double and got ACCEPTED, I used the exact length of the number in the calculation.

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

Long double is not enough if you calculate the answer this way. Try another approach, with log like mentioned above.

zhartley
New poster
Posts: 8
Joined: Tue Jul 23, 2002 2:38 am
Location: High Point, North Carolina / Goodlettsville, Tennesee
Contact:

Post by zhartley »

ok....i tried using logarithms....

[cpp]
@begin_of_source_code
/* @JUDGE_ID: 21301HT 113 C++ */
#include <iostream>
#include <cmath>
using namespace std;

int main(void) {

register long double n, p, k;
while (cin >> n >> p) {
k = exp( log(p) / n );
cout << k << endl;
}

return 0;

}
@end_of_source_code
[/cpp]

....and it got me Yet Another Wrong Answer :cry:

everything seems to work on this end:
zth@littledaemon$ cat input
2
16
3
27
7
4357186184021382204544
zth@littledaemon$ a.out < input
4
3
1234
Any ideas?

Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

That can't work if the input contains really big numbers. Consider, how you can use the length of a number when calculating log10 of that number. And I think that log and exp are only defined for double, not for long double, so I would not use long double. I used double and cut off all digits after the tenth.

zhartley
New poster
Posts: 8
Joined: Tue Jul 23, 2002 2:38 am
Location: High Point, North Carolina / Goodlettsville, Tennesee
Contact:

Post by zhartley »

zth@littledaemon$ grep "exp" /usr/include/g++/cmath | grep "long double"
long double exp (long doulbe);
long double frexp(long double, int*);
long double frexp(long double, int*);
zth@littledaemon$ grep "log" /usr/include/g++/cmath | grep "long double"
long double log (long double);
long double log10(long double);
So, apparently, they can use long doubles (FreeBSD 4.5-Release using gcc version 2.95.3 20010315 (release) [FreeBSD])

As far as the rest of your post, I'm confused. you only considered the first ten digits of any number? And I unfortuneately do not know of any way to use the length of a number in regards to logarithms. And I have been re-reading my math textbooks on logarithms to make sure I am not missing anything. (Btw, thanks for taking the time to reply)

zhartley
New poster
Posts: 8
Joined: Tue Jul 23, 2002 2:38 am
Location: High Point, North Carolina / Goodlettsville, Tennesee
Contact:

Beguiling WA for 113

Post by zhartley »

Yes, I know this problem already has a thread, however this is a slightly different problem...

Does _anybody_ know how to explain how I get an accepted with this:

[c]
#include <stdio.h>
#include <math.h>

int main(void) {
int n;
double p;
while (scanf("%d %lf", &n, &p) == 2) {
printf("%.0lf\n", exp(log(p)/(double)n));
}
return 0;
}
[/c]

while i get a wrong answer with this:
[cpp]
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main(void) {
int n;
double p;
while (cin >> n >> p) {
cout << setprecision(0) << exp(log(p)/(double)n) << endl;
}
return 0;
}
[/cpp]

Near as I can tell, they are identical.

Daniel Chia
New poster
Posts: 12
Joined: Mon Jul 29, 2002 3:04 pm
Contact:

Post by Daniel Chia »

i get the same problem :( and btw..as far as my measly knowledge of c++ is concerned... setprecisio(0) sets it back to the default of infinite precision, no?

zhartley
New poster
Posts: 8
Joined: Tue Jul 23, 2002 2:38 am
Location: High Point, North Carolina / Goodlettsville, Tennesee
Contact:

Post by zhartley »

well, i've been re-reading my copy of The C++ Programming language (and re-reading it and re-reading it...) and I'm still extremely confused (the documentation sucks (or i do :))). In any event, this is what I am working with right now (which still gets a WA)

[cpp]
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main(void) {
double n, p;
cout.unsetf(ios::showpoint);
while (cin >> n >> p) {
cout << exp(log(p)/n) << endl;
}
return 0;
}[/cpp]

laboni
New poster
Posts: 12
Joined: Sat Sep 14, 2002 9:22 pm
Location: India

113- power of cryptography - What is the algorithm?

Post by laboni »

this problem is a nightmare! P is Such a big number!!
There must be some easy algorithm in number theory to solve such problem.Can anyone help me plz? :(

Post Reply

Return to “Volume 1 (100-199)”