Dynamic programming dp algorithm classic buns make up the number java

content

topic buns make up

dynamic programming idea

specific code


Topic  buns make up the number

Xiao Ming eats breakfast at a steamed bun shop almost every morning. He found that this steamed bun shop has NN kinds of steamers, of which the iith steamer can hold A_iAi​ steamed buns exactly. Each type of steamer has a very large number of cages, which can be considered as infinite cages. 

Whenever a customer wants to buy XX steamed buns, the uncle who sells steamed buns will quickly select several cages of steamed buns, so that there are exactly XX steamed buns in these cages. For example, there are 33 kinds of steamers, which can hold 3, 43, 4 and 55 buns respectively. When a customer wants to buy 1111 buns, the uncle will choose 22 cages of 33 plus 11 cages of 55 (may also choose 11 cages of 33 plus 22 cages of 44). 

Of course, sometimes Uncle Baozi can't make up the quantity that customers want to buy anyway. For example, there are 33 kinds of steamers, which can hold 4, 54, 5 and 66 buns respectively. And when the customer wanted to buy 77 buns, the uncle couldn't come up with them. 

Xiao Ming wanted to know how many kinds of numbers there were that Uncle Baozi couldn't figure out.

enter

The first line contains an integer N. (1 <= N <= 100)
Each of the following N lines contains an integer Ai. (1 <= Ai <= 100) 

2  
4  
5  

output

 An integer representing the answer. If there are infinite numbers that cannot be made up, output INF.

6  

dynamic programming idea

Before talking about the idea of ​​dynamic programming, this question also uses the classical number theory Ax+By=C (x, y>0) problem: if A and B are relatively prime, then there are infinite C equations that have no solution. Otherwise there may be a solution. Generalization to a,b,...,n holds at the same time. You can use this idea to find out whether there are an infinite number of buns that cannot be made up.

So in this question, you can use a1, a2,...an to represent the number of buns that can be put, and find the appropriate x, y,....n to make C. If the equation has a solution, it can be figured out, otherwise it cannot be figured out. As for finding the greatest common divisor, you can use tossing and dividing.

Then this question can be similar to the knapsack problem , using a boolean[] dp, and the subscript index indicates whether the index buns can be made up . When dp[i] means i buns can be made up, then j buns + the i-th steamer can just fit Ai buns: arr[i] can also be made up, that is, index=( j+arr[i] ) The buns can also be made up: dp[j + arr[i]] = true; the idea of ​​dynamic programming is reflected in this, and in order to save time, the dp[] data can be updated while the data is being entered.

specific code

import java.util.Scanner;

public class _包子凑数 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();//N种蒸笼
		int yueshu = 0;//最大公约数
		int[] arr = new int[n + 1];//以下N行每行包含一个整数Ai
		boolean[] dp = new boolean[10000];//下标index表示index个包子是否能够凑出
		dp[0] = true;//默认0个包子可以凑出来
		//在录入数据的同时,即可对dp[index]index个包子是否能够凑出来进行更新处理。
		for (int i = 1; i <= n; i++) {
			arr[i] = scanner.nextInt();//录入数据
			/**
			 * 下面if-else语句动态求输入的数据的最大公约数
			 * 求当前第i种蒸笼恰好能放Ai个包子和前一个蒸笼能放的包子个数的最大公约数
			 */
			if (i == 1)
				yueshu = arr[1];
			else
				yueshu = yue(arr[i], yueshu);
			//更新dp[]数组
			for (int j = 0; j < 10000; j++) {
				/**
				 * 如果dp[j],即j个包子能够凑出来,
				 * 那么j个包子+第i种蒸笼恰好能放Ai个包子:arr[i]也能够凑出来
				 * 即index=(j+arr[i])个包子也能够凑出来。
				 */
				if (dp[j])
					dp[j + arr[i]] = true;//向后递推,动态规划的体现
			}
		}
		//当最大公约数!=1  说明Ai不互质,则有无限个包子数凑不出来
		if (yueshu != 1)
			System.out.println("INF");
		else {
			//否则统计个数
			int sum = 0;
			for (int i = 0; i < dp.length; i++) {
				if (!dp[i])
					sum++;
			}
			System.out.println(sum);
		}
	}
	/**
	 * 辗转相除法求a,b两个数的最大公约数
	 * @param a
	 * @param b
	 * @return
	 */
	public static int yue(int a, int b) {
		if (b == 0)
			return a;
		else
			return yue(b, a % b);
	}
}

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123720424