51nod 2级算法题-1014

本文介绍了一个简单的算法,用于解决模P下二次方程X*XmodP=A的问题,其中P为质数。通过暴力枚举的方法寻找所有可能的解,并按升序输出这些解。如果不存在解,则输出特定提示。

1014 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

Input示例

13 3

Output示例

4 9

考虑P的范围就直接暴力枚举了

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
#define endl "\n"
const long long maxn=1e6+100;
int main(){
    ios::sync_with_stdio(false);
    long long p,x;
    cin>>p>>x;
    int flag=0;
    for(long long  i=0;i<p;i++){
        if(flag && (i*i)%p==x){
            cout<<" "<<i;
        }
        if(!flag && (i*i)%p==x){
            cout<<i;
            flag=1;
        }
    }
    if(!flag){
        cout<<"No Solution";
    }
    cout<<endl;
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值