← Back to topics
Topic

z-stepen

b
bojana92
On the task z-stepen why do i get wrong result when i checked it a couple of times and it's correct
here's my code
program zstepen;
var s,o,p,z:longint;
i,m:integer;
begin
readln(o,s,m);
z:=1;
for i:=1 to s do
z:=z*o;
p:=z mod m;
writeln(p);
end.
N
Nocturne
@bojana92
Your should try to find a better solution. Imagine calculating 2000000000^2000000000 : it would take ages !
n
nikola
Yea, that's too big number, and you can't register it in memory in ordinary way. You need to ouput the number O^S with modulo M. So a*b mod M = (a mod M)*(b mod m). But you will still have Time Limit Exceeded. Try something smarter.
Hint:
a^b = (a^{b/2})^2 if b is an even number
a^b = (a^{b / 2})^2*a if b is an odd number
I think this will help you to solve the problem...
m
matteo123
program zstepen;
var s,o,p,z:longint;
i,m:integer;
begin
readln(o,s,m);
z:=1;
for i:=1 to s do
z:=z*o mod m;
p:=z mod m;
writeln(p);
end.
i think it will work
D
Diabolic
can somebody please check my code? I got all wrong results. Thanks in advance.
b
bojana92
mateo123 the code works on some tests by the way i figured it out
m
matteo123
@Diabolic:
i think it will be work
#include <iostream>
using namespace std;

int main() {
long long int o,s,g=1;
int m;
cin>>o>>s>>m;
for(int i=0;i<s/2;i++)
{
g=(g%m)*(o%m);
}
cout<<g;
return 0;
}
it this code wont work i think that nikola's idea is great