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

Whats wrong in this program
Posted by Karthick P 17 Jan 2006 11:48
#include<stdio.h>

long power(int x,int y)
{
  int i;
  long result=x;;
  if (y==0) return 1;
  for(i=2;i<=y;i++)
    result*=x;
  return result;
}

int main()
{
  long n,m,y;
  long x;
  int flag=0;

  scanf("%ld %ld %ld",&n,&m,&y);
  for(x=0;x<m;x++)
  {
    if ( power(x,n) % m == y)
    {
      printf("%ld ",x);
      flag=1;
    }
  }
  if ( !flag )
    printf("-1");
  return 0;
}

Its giving wrong Test #4.
Re: Whats wrong in this program
Posted by wwwwww 17 Jan 2006 15:30
You think x^y fits in long when x is 100 and y is 100?
100^100...
You need (x^y) mod m (not x^y)!
Use this:

long power(int x,int y,int m)
{
int i;
long result=x;
if (y==0) return 1%m;
for(i=2;i<=y;i++)
result=(result*x)%m;
return result%m;
}
Re: Whats wrong in this program
Posted by Karthick P 18 Jan 2006 20:30
Thank you, I got accepted