您的位置:首页 > 其它

Power URAL - 1110

2017-04-10 17:55 295 查看
You are given the whole numbers N, M and Y. Write a program that will find all whole numbers X in the interval [0, M − 1] such that XN mod M = Y.
Input
The input contains a single line with N, M and Y (0 <  N < 999, 1 <  M < 999, 0 <  Y < 999) separated with one space.
Output
Output all numbers X separated with space on one line. The numbers must be written in ascending order. If no such numbers exist then output −1.
Example
input	output
2 6 4
2 4

//这题虽然水,但是我没做出来,做了好几次
AC代码
#include<iostream>
#include<iomanip>
#include<math.h>
#include<string.h>
using namespace std;

int b[1005];

int main()
{
int n,m,y,a;
memset(b,0,sizeof(b));
cin>>n>>m>>y;

int count=0;
int i;
for(i=0;i<m;i++)
{
//a=pow(i,n);
a=1;
for(int j=0;j<n;j++)
{//利用a*b%c=((a%c)*b)%c,每一步都进行这种处理
a*=(i%m);
a=a%m;
}
if(a%m==y)
{
count++;
b[count]=i;
}
}
if(count==0)
cout<<"-1"<<endl;
else
{
for(int k=1;k<=count;k++)
{
if(k==count)
cout<<b[k];
else
cout<<b[k]<<" ";
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: