ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1110. Power

Why WA on Test 6?
Posted by Michael Medvedev 22 Jul 2004 03:38
program p1110;
var
  r,n,m,x,y:longint;
  flag:boolean;

function pow(x,y,n:longint):longint;
var
  res:integer;
begin
  res := 1;
  while (y>0) do
  begin
    if (y mod 2 = 1) then res := (res * x) mod n;
    y := y div 2;
    x := x * x;
  end;
  pow := res;
end;

begin
  readln(n,m,y);
  flag := False;
  for x:=1 to m-1 do
  begin
    r := pow(x,n,m);
    if r = y then
    begin
      if flag then write(' ');
      write(x);
      flag := True;
    end;
  end;
  if not flag then write('-1');
  writeln;
end.