1014 X^2 Mod P(数论 枚举)(2级算法题)

1014 X^2 Mod P(数论 枚举)(2级算法题

基准时间限制: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
思路:x范围比较小 计算起来不复杂 直接枚举判断就好
#include <cstdio>

#define LL long long 

bool quickmod(LL x , LL y , LL A){
    if(x*x%y==A)
        return true ; 
    return false ; 
}

int main(){
    LL p , a ; 
    while(~scanf("%lld%lld" , &p , &a)){
        int times = 0 ; 
        int number = 0 ; 
        for(int i=0 ; i<=p ; i++){
            if(quickmod(i,p,a)){
                number++ ; 
                if(times == 0 ){
                    printf("%lld" , i ) ;
                    times ++ ;  
                } else {
                    printf(" %lld" , i) ; 
                }
                
            }
        }
        if(number == 0 ){
            printf("No Solution
") ; 
        }
    }
    return 0 ; 
} 
原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7553628.html