The 12th Blue Bridge Cup Java Group B Provincial Competition in 2021, the first round of real questions - goods placement

Problem Description】

    Xiaolan has an oversized warehouse that can store a lot of goods.
    Now, Xiaolan has n boxes of goods to be placed in the warehouse, and each box of goods is a regular cube. Xiaolan stipulates three mutually perpendicular directions of length, width, and height, and the sides of each box of goods must be strictly parallel to the length, width, and height.
    Xiaolan hopes that all the goods will eventually be arranged in one big cube. That is, stack L, W, and H goods in the directions of length, width, and height, satisfying n = L × W × H.
    Given n, how many schemes for stacking goods meet the requirements.
    For example, when n = 4, there are the following 6 schemes: 1×1×4, 1×2×2, 1×4×1, 2×1×2, 2×2×1, 4×1×1.
    Excuse me, when n = 2021041820210418 (note that there are 16 digits), how many schemes are there in total?
    Tip: It is recommended to use computer programming to solve the problem.

【Answer submission】

    This is a question of filling in the blanks with the result, you only need to calculate the result and submit it. The result of this question is an integer, only fill in this integer when submitting the answer, filling in redundant content will result in no scoring.

After reading the question , you can clearly understand that the essence of this question is how to multiply the three numbers L, W, and H to get 2021041820210418

Many friends will directly act violently, three for loops, then I can tell you clearly that you will not be able to get the answer before the end of the exam, this complexity is too high, 2021041820210418 is approximately equal to 2* 10^15 A for is to execute (2* 10^15) ^ 3 =8 * 10^45

This memory is burning and "smoking", of course it won't work, and you don't have so much time to run!

Next, I will introduce my method. If it is helpful to you, I hope to like, comment + follow ~ . ~

step1 --- First of all, we can start all the factors of this super large number,

step2 ---Secondly, we traverse and combine these factors, and accumulate once when it is equal to this super large number

Attach the code, as follows:

package test1;
import java.util.*;
import java.math.*;
public class test {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long n=2021041820210418L;
		/* 
		 * 创建因子数组,为什么是128长度,这是因为事先输出了 count = 64,
		 *  然后因为 因子是对称的,且 根号n 无法开整数方根,所以 因子的个数为 2 *count = 128
		 * */
		long[] zhi=new long[128]; 
		int count=0;			
		long i;						
		for(i=1;i*i<n;i++) {
			if(n%i==0) {
				zhi[count]=i; //如果n能整除 i ,则将i装入数组中
				count++; // 求因子个数,并且作为 数组的索引值
			}
		}
		int j=0;
		for(j=64;j<128;j++) {  //求n的另一部分因子,另一部分的因子 = n / 对称因子(比如63和64是对称的,62和65是对称的)
			zhi[j]=n/zhi[127-j];
		}
		
		int s=0;    //开始遍历因子数组,如果相乘等于 n ,则计数器计数一次
		for(int x=0;x<128;x++) {		
			for(int p=0;p<128;p++) {			
				if(zhi[x]*zhi[p]>n) break; // 减少一定的 “余坠”,大于n直接跳出,减少复杂度和运算时间
				for(int q=0;q<128;q++) {
					if(zhi[x]*zhi[p]*zhi[q]==n) {
						s++;
					}
				}
			}
		}
		System.out.println(s);
	}
}

Guess you like

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