110704 Factovisors

原创 2013年12月04日 13:13:33


import java.util.*;
import static java.lang.Math.*;

public class Main {
	
	private static boolean IsPrime(int x)
	{
		int mid = (int)(sqrt(x));
		for (int i = 2; i <= mid; ++i)
			if ((x % i) == 0)
				return false;
		return true;
	}
	
	private static int GetGCD(int n, int m)
	{
		if (n < m)
			return GetGCD(m, n);
		
		if (n == m)
			return m;
		
		while (true)
		{
			int tmp = n % m;
			if (tmp == 0)
				return m;
			
			n = m;
			m = tmp;
		}
	}
	
	private static boolean IsDividable(int n, int m)
	{
		if (m == 1)
			return true;
		
		if (m == 0)
			return false;
		
		if (n == 0)
			return false;
		
		boolean needToCheckPrime = true;
		while (m > n)
		{
			if (n <= 2)
				return false;
			
			if (needToCheckPrime && IsPrime(m))
				return false;
			
			int gcd = GetGCD(n, m);
			needToCheckPrime = (gcd > 1) ? true : false;
			m /= GetGCD(n, m);
			--n;
		}
		
		return true;
	}
	
	private static void Handle(int n, int m)
	{		
		if (IsDividable(n, m))
			System.out.println(m + " divides " + n + "!");
		else
			System.out.println(m + " does not divide " + n + "!");
	}
	
	public static void main(String[] args)
	{
		Scanner inScanner = new Scanner(System.in);
		while (inScanner.hasNextInt())
		{
			int n = inScanner.nextInt();
			int m = inScanner.nextInt();
			Handle(n, m);
		}
	}

}


内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:110704 Factovisors
举报原因:
原因补充:

(最多只允许输入30个字)