| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
贴Java代码。 意外的轻松1A了 ,带注释
import java.math.BigInteger;
import java.util.*;
public class Main{
// 判断n是否质数
static boolean is_prime(int n){
for(int i = 2; i*i <= n; i++){
if(n % i == 0) return false;
}
return n != 1;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(true){
int p = s.nextInt();
int a = s.nextInt();
if(p==0 & a==0) break;
// 如果是质数直接输出no
if(is_prime(p)){
System.out.println("no");
continue;
}
// 用BigInteger自带的方法高速求余后判断是否相等,完
BigInteger A = new BigInteger(a+"");
BigInteger P = new BigInteger(p+"");
BigInteger Y = A.modPow(P, P);
if(Y.equals(A)){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator