Waterloo Cup ADV-205 algorithm to improve the use of Candy Java version __ algorithm

Source: Internet
Author: User
Problem DescriptionMother bought n pieces of sugar for little b. But she doesn't allow little b to eat directly.
Assuming that there is an M-block sugar, a small B can take a P-block sugar at a time, where p is a mass factor of m less than the root. At this time, the mother will be in small B take the p block sugar and then from the sugar heap to take the p block sugar. Then little b can take the sugar.
Now little B wants to know how much sugar you can get. Input FormatAn integer n output FormatHow much sugar can you take? Sample Input15 Sample Output6 Data size and ConventionN <= 100000
Import java.util.ArrayList;
Import java.util.List;

Import Java.util.Scanner;

	public class Main {private static list<integer> prime = new arraylist<> ();
		public static void Main (string[] args) {Scanner in = new Scanner (system.in);
		int n = in.nextint ();
		In.close ();

		Setprime (n);
		Int[] dp = new Int[n + 1]; for (int i = 0; i < prime.size (); i++) {for (int j = 1; J <= N; j +) {if (j% prime.get (i) = 0 && (J >= 2 * prime.get (i)))
				{Dp[j] = Integer.max (Dp[j], dp[j-2 * Prime.get (i)] + prime.get (i));
	}} System.out.println (Dp[n]);

		} private static void Setprime (int n) {boolean[] p = new Boolean[n + 1];
		for (int i = 2; i < p.length i++) {P[i] = true;
			for (int i = 2; i < P.length. i++) {for (int j = 2; I * J < P.length; J +) {P[i * j] = false;
			for (int i = 2; I * I <= n; i++) {if (p[i)) {prime.add (i); }
		}
	}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.