110705 Summation of Four Primes

原创 2013年12月04日 14:40:43


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

public class Main {
	private static Map<Integer, Boolean> s_PrimesInfo;
	static
	{
		s_PrimesInfo = new HashMap<Integer, Boolean>();
	}
	
	private static boolean IsPrime(int x)
	{
		Boolean result = s_PrimesInfo.get(x);
		if (result != null)
			return result;
		
		int mid = (int)sqrt(x);
		result = true;
		for (int i = 2; i <= mid; ++i)
			if ((x % i) == 0)
				result = false;

		s_PrimesInfo.put(x, result);
		return result;
	}
	
	private static void GetTwoPrimes(int x, List<Integer> results)
	{
		int mid = x/2;
		for (int i = 2; i <= mid; ++i)
		{
			if (IsPrime(i) && IsPrime(x - i))
			{
				results.add(i);
				results.add(x - i);
				return;
			}
		}
	}
	
	private static List<Integer> GetFourPrimes(int x)
	{
		if (x <= 7)
			return null;
		List<Integer> results = new LinkedList<Integer>();
		results.add(2);
		x -= 2;
		if ((x % 2) == 0)
		{
			results.add(2);
			x -= 2;
		}
		else
		{
			results.add(3);
			x -= 3;
		}
		
		GetTwoPrimes(x, results);
 		return results;
	}
	
	private static void Handle(int x)
	{
		List<Integer> results = GetFourPrimes(x);
		if (results == null)
			System.out.println("Impossible.");
		else
			System.out.println(results.get(0) + " " + results.get(1) + " " + results.get(2) + " " + results.get(3)); 
	}
	
	public static void main(String[] args)
	{
		Scanner inScanner = new Scanner(System.in);
		while (inScanner.hasNextInt())
		{
			int a = inScanner.nextInt();
			Handle(a);
		}
	}

}


相关文章推荐

UVa Problem 10168 Summation of Four Primes (四素数之和)

// Summation of Four Primes (四素数之和)// PC/UVa IDs: 110705/10168, Popularity: A, Success rate: average...

uva 10168 summation of four primes

Problem A Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 s...

UVa 10168 Summation of Four Primes(数论-哥德巴赫猜想)

题目大意: 给定一个数n,问能用4个质因素相加表示,输出任意一种方案。 解题思路:这题比较有趣 (1)如果 n < 8,明显无解,最小的质数为2。 (2)如果 n >= 8,我们认为肯定有答案。 根据...
  • wujy47
  • wujy47
  • 2014年08月03日 17:49
  • 480

Numerical Summation of a Series

Produce a table of the values of the series Equation 1 for the 2001 values of x, x= 0.000, 0.001, 0...

light oj 1220 Fantasy of a Summation

Fantasy of a Summation Description If you think codes, eat codes then sometimes you ma...

LightOJ 1213 Fantasy of a Summation

题目链接:点我If you think codes, eat codes then sometimes you may get stressed. In your dreams you may see...
  • cccruel
  • cccruel
  • 2017年07月29日 11:05
  • 88

ZOJ 1007 Numerical Summation of a Series

Produce a table of the values of the series Equation 1 for the 2001 values of x, x= 0.000, 0.001,...

Fantasy of a Summation

Fantasy of a Summation If you think codes, eat codes then sometimes you may get stressed....

ZOJ1007.Numerical Summation of a Series

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=7

ZOJ--1007:Numerical Summation of a Series

Numerical Summation of a Series Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special...
  • yf224
  • yf224
  • 2017年06月09日 23:13
  • 124
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:110705 Summation of Four Primes
举报原因:
原因补充:

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