Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴Java代码。 意外的轻松1A了 ,带注释

Posted by haqishen at 2015-01-11 10:59:05 on Problem 3641
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator