Topic 1886: The 8th Blue Bridge Cup 2017 Real Questions - Baozi Make Up the Numbers

Time Limit: 1Sec Memory Limit: 128MB Commits: 2378 Solved: 789

topic description

       Xiao Ming eats breakfast at a bun shop almost every morning. He found that this steamed bun store has N kinds of steamers, of which the i-th steamer can just hold Ai steamed buns. Each steamer has so many cages that it can be thought of as infinite cages.

        Whenever a customer wants to buy X buns, the uncle who sells buns will quickly select a number of steamed buns, so that there are exactly X buns in these cages. For example, there are 3 kinds of steamers, which can hold 3, 4 and 5 buns respectively. When a customer wants to buy 11 buns, the uncle will choose 2 cages of 3 plus 1 cage of 5 (may also choose 1 cage of 3 plus 2 cages of 4).

        Of course, sometimes Uncle Baozi can't make up the quantity that customers want to buy anyway. For example, there are 3 kinds of steamers, which can hold 4, 5 and 6 buns respectively. And when the customer wanted to buy 7 steamed buns, the uncle couldn't get them together.

        Xiao Ming wants to know how many numbers there are that Uncle Baozi can't make up.

enter

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

output

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

Sample input copy

2
4
5

Sample output copy

6

        After reading this question, many students are particularly distressed. How to do this? Many students will start with violence, but violence is certainly not acceptable for this question. In fact, this question in 2017 is very similar to a question in the 2013 Blue Bridge Cup. I don’t know if the students have discovered it. In 2017, this question was called Baozi that couldn't be made up, and in 2013, the question was called Number that couldn't be made up. The core of its thinking remains unchanged. Next, I will talk about how to solve this type of questions. I hope that students who have received help will like me. Your likes are the driving force for my continuous updates~.~

        First of all, we analyze the input sample 2 4 5, 2 means that there are two integers, so what numbers can't be made up by 4 5, we use a simple calculation as follows

        Then we will find the following rules:

 

        We will find that the two numbers 4 5 can be made up except for the six numbers 1 2 3 6 7 11. Let's take a look at the two numbers 4 6. What numbers can they not make up? Don't even think about it, first of all, they can't even make up all the odd numbers, why do you say that, because the two numbers are even numbers, the result of 4x+6y must be even, so it is impossible to make up odd numbers, so There are infinite numbers that cannot be made up.

        To sum up this point , we will find that if the common divisor of two numbers is not 1, then there are infinite numbers that cannot be made up! There is no doubt about this, so we can first perform gcd() on N numbers to find the common divisor. This method is also called the rolling and dividing method to find the common divisor, as follows

        

        Then let's come to understand one more point, that is, if 4 can be made up, then 4+4 equals 8, and 8 must also be made out, because 8 can be made out, then 8+4 is equal to 12, and so is 12 It can be made up, and so on. 16, 20, 24... can also be made up. After knowing this, the code is attached next, as follows:

package test;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		int[] arr=new int[N];
		int zhi=0;
		boolean[] f=new boolean[10000];  //定义数组f,此数组为布尔型数组,用了表示当前的索引能否凑出来,凑出来就为true,否则为false
		f[0]=true;  //并定义首个索引为true
		for(int i=0;i<N;i++) {
			arr[i]=sc.nextInt();
		
			if(i==0) {
				zhi=arr[0];
			}
			else {			//求arr数组中所有元素的公约数
				zhi=gcd(arr[i],zhi);
			}
			for(int j=0;j<10000-arr[i];j++) {	
				if(f[j]) {	//以此递归,例如当f[0]为真,那么f[4+0]=true,即4能凑出来,以此类推,当f[4]=true,那么f[4+4]=true,所以8凑出来,以此类推12,16,20均可以凑出来
					f[arr[i]+j]=true;
				}
			}		
		}
		if(zhi!=1) {  //如果arr数组中所有元素的公约数不等于1,那么必然凑不出偶数,则输出“INF”
			System.out.println("INF");
			return;
		}
		int count=0;
		for(int i=0;i<10000;i++) {
			if(f[i]==false) count++; 
		}
		System.out.println(count);
	}
	public static int gcd(int a,int b) {    //利用辗转相除法求公约数
		if(b==0) {
			return a;
		}
		else {
			return gcd(b,a%b);
		}
	}
}

If you guys have better ideas, peerless martial arts, welcome to leave your footprints in the message area~.~

Guess you like

Origin blog.csdn.net/qq_49174867/article/details/123747648