51Nod X^2 Mod P

来源:互联网 发布:fanuc pmc编程说明书 编辑:程序博客网 时间:2024/04/18 14:16

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
Sample Input
13 3
Sample Output
4 9


这道题数据较大会爆int,将变量设为long long即可

AC代码:

#include<iostream>#include<cstring>#include<cmath>using namespace std;long long p, a;long long x[1000005];int main(){    while(cin>>p>>a)    {        long long index = 0;        for(long long i = 0; i <= p; i++)        {            if((i * i) % p == a)            {                x[index++] = i;            }        }        if(index == 0)            cout<<"No Solution"<<endl;        else        {            for(long long i = 0; i <index - 1; i++)                cout<<x[i]<<" ";            cout<<x[index - 1]<<endl;        }    }    return 0;}