|
|
back to boardWhats wrong in this program #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 Thank you, I got accepted |
|
|