1. 程式人生 > >51Nod - 1014 x的取餘

51Nod - 1014 x的取餘

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

解題思路:直接暴力列舉就過了,注意的地方就是x*x會超int,定義成long long 就行了,交第一次時WA了,for迴圈中i沒有定義c成long long ,導致結果錯誤

AC程式碼:

#include<iostream>
using namespace std;
int main()
{
	long long x,p,a,ans;
	int k=0;
	cin>>p>>a;
	for(long long i=1;i<=p;i++)
	{
		if(i*i%p==a)
		{
			if(!k)
			{
				cout<<i;
				k=1;
			}
			else
				cout<<' '<<i;
		}
	}
	if(k==0)
		cout<<"No Solution";
	cout<<endl;
	return 0;
}