您的位置:首页 > 其它

51nod 2级算法题-1014

2017-04-25 11:15 267 查看

1014 X^2 Mod P

X*X mod P = A,其中P为质数。给出P和A,求<=P的所有X。


Input

两个数P A,中间用空格隔开。(1 <= A < P <= 1000000, P为质数)


Output

输出符合条件的X,且0 <= X <= P,如果有多个,按照升序排列,中间用空格隔开。
如果没有符合条件的X,输出:No Solution


Input示例

13 3


Output示例

4 9


考虑P的范围就直接暴力枚举了

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define endl "\n"
const long long maxn=1e6+100;
int main(){
ios::sync_with_stdio(false);
long long p,x;
cin>>p>>x;
int flag=0;
for(long long  i=0;i<p;i++){
if(flag && (i*i)%p==x){
cout<<" "<<i;
}
if(!flag && (i*i)%p==x){
cout<<i;
flag=1;
}
}
if(!flag){
cout<<"No Solution";
}
cout<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: