1110. Powertime limit:0.5 Second
Memory limit:64 MB
You are given the whole numbers
N,
Mand
Y. Write a program that'll find all whole numbers
XIn the interval [0,
M? 1] such that
XN MoD
M=
Y. Inputthe input contains a single line with
N,
Mand
Y(0 <
N< 999, 1 <
M< 999, 0 <
Y< 999) separated with one space. Outputoutput all numbers
XSeparated with space on one line. The numbers must is written in ascending order. If No such numbers exist then output? 1.Sample
problem Source:Bulgarian National Olympiad Day #1
Tags:()
Test Instructions: x is present, so that X's n%m equals y, and all x is present. There is no output negative one. water problem, run a fast power.
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < Algorithm>using namespace Std;int m;int fast_mi (int a,int k) { int res=1; while (k) { if (k&1) res=res*a%m; a=a*a%m; k>>=1; } return res;} int main () { int n,y,i; int a[1000]; while (Cin>>n>>m>>y) { int flag=0; memset (A,0,sizeof (a)); int len=0; for (i=0; i<=m-1; i++) { if (Fast_mi (i,n)%m==y) { flag=1; a[len++]=i; } } if (flag) {for (i=0; i<len; i++) { if (!i) cout<<a[i]; else cout<< "" <<a[i]; } cout<<endl; } else cout<<-1<<endl; } return 0;}
Ural 1110 Power