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

直接看,数据不大,可以暴力枚举。


代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

int main()
{
    int p,a;
    scanf("%d%d",&p,&a);
    int k=0;
    long long int i;
    for(i=1;i<=p;i++)
    {
        if(i*i%p == a)
        {
            if(k++!=0) cout<<" ";
            cout<<i;
        }
    }
    if(k==0) printf("No Solution\n");
    else printf("\n");
    return 0;
}

posted @ 2017-08-20 14:14  让你一生残梦  阅读(392)  评论(0编辑  收藏  举报