poj-3641

题意:给两个数p和a。先判断p是否是素数,如果是返回no,如果不是则再判断a的p次方取模p是否等于a,如果是返回yes,其他返回no。

题解:快速幂。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define ull unsigned long long
#define ll long long
#define INF 0x3f3f3f3f

using namespace std;

bool is_prime(int n){
	for(int i = 3 ; i <= sqrt(n+0.0);i++){
		if(n%i==0){
			return 0;
		}
	}
	return 1;
}

ll poww(ll a,ll p){
	ll ans = 1,mark = a,mod = p;
	while(p){
		if(p&1!=0){
			ans*=mark;
			ans%=mod;
		}
		mark*=mark;
		mark%=mod;
		p >>= 1;
	}
	return ans;
}

int main(){
	ll a,p;
	while(cin>>p>>a&&(a+p)){
		if(is_prime(p)){
			cout<<"no"<<endl;
		}else if(poww(a,p)==a){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值