题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1014
1014 X^2 Mod P
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注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
此题简单,直接暴力
不废话上代码
#include <iostream>
#include <cmath>
using namespace std;
int main()
{ long long i,p,a,flag,q;
while(cin>>p>>a)
{ flag=0;q=0;
for(i=0;i<=p;i++)
{
if(i*i%p==a)
{
if(flag==0)
cout<<i;
else cout<<" "<<i;
flag=1;q=1;
}
}
if(q==0) cout<<"No Solution";
cout<<endl;
}
return 0;
}