51 Nod 1014 X^2 Mod P (数论+二次剩余)

版权声明:低调地前行,越努力越幸运! https://blog.csdn.net/SSYITwin/article/details/83093645

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

1.二次剩余算法:点击转到

2.非二次剩余算法通过代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long p,a;
	int flag=0;
	cin>>p>>a;
	for(long long x=1;x<=p;x++)
	{
		if((long long)(x*x)%p==a)
		{
			printf(" %d",x);
			flag=1;
		}
	}
	if(!flag)
	  cout<<"No Solution";
	cout<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SSYITwin/article/details/83093645