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);
		}
	}

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

uva 10139 factovisors

Problem D: Factovisors The factorial function, n! is defined thus for n a non-negative integer: ...

Uva 10139 Factovisors

/* * url: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p...

UVa 10139 Factovisors (阶乘能否整除?)

10139 - Factovisors Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_online...

UVa Problem 10139 Factovisors (阶乘与整除)

// Factovisors (阶乘与整除)// PC/UVa IDs: 110704/10139, Popularity: A, Success rate: average Level: 2// V...

UVa 10139 Factovisors

这道题同样的代码用 JAVA 会超时,用 C++ 才通过。 #include #include #include using namespace std; // 素数筛的数组长度 long ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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