21_蓝桥杯_java_B组_D题(货物摆放)

D、货物摆放

【问题描述】

小蓝有一个超大的仓库,可以摆放很多货物。
现在,小蓝有 n 箱货物要摆放在仓库,每箱货物都是规则的正方体。小蓝
规定了长、宽、高三个互相垂直的方向,每箱货物的边都必须严格平行于长、
宽、高。
小蓝希望所有的货物最终摆成一个大的立方体。即在长、宽、高的方向上
分别堆 L、W、H 的货物,满足 n = L × W × H。
给定 n,请问有多少种堆放货物的方案满足要求。
例如,当 n = 4 时,有以下 6 种方案:1×1×4、1×2×2、1×4×1、2×1×2、 2 × 2 × 1、4 × 1 × 1。
请问,当 n = 2021041820210418 (注意有 16 位数字)时,总共有多少种
方案?
提示:建议使用计算机编程解决问题。

思路

先进行质因数分解得到
2^1+3^3+17^1+131^1+2857^1+5882353^1或者2*3*3*3*17*131*2857*5882353
然后DFS枚举,结果set去重。

代码

import java.io.BufferedInputStream;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
	public static Scanner cin = new Scanner(new BufferedInputStream(System.in));
	public static long n;
	public static int[] p = new int[100];
	public static int[] num = new int[100];
	public static int[] factor = new int[100];
	public static int cnt = 0;
	public static int fcnt = 0;
	public static int res = 0;
	public static Set<Node> s = new TreeSet<>();

	public static void dfs(long x, long y, long z, int num) {
		if (num == 0) {
			System.out.println(x + "*" + y + "*" + z + "=" + n);
			s.add(new Node(x, y, z));
			res++;
			return;
		}
		dfs(x * factor[num], y, z, num - 1);
		dfs(x, y * factor[num], z, num - 1);
		dfs(x, y, z * factor[num], num - 1);
	}

	public static void decompose(long x) {
		for (int i = 2; i * i <= x; i++) {
			if (x % i != 0)
				continue;
			int tmp = 0;
			while (x % i == 0) {
				x /= i;
				tmp++;
			}
			p[++cnt] = i;
			num[cnt] = tmp;
		}
		if (x != 0) {
			p[++cnt] = (int) x;
			num[cnt] = 1;
		}
		for (int i = 1; i <= cnt; i++) {
			System.out.print(p[i] + "^" + num[i]);
			if (i != cnt)
				System.out.print("+");
			for (int j = 1; j <= num[i]; j++) {
				factor[++fcnt] = p[i];
			}
		}
		System.out.println();
		for (int i = 1; i <= fcnt; i++) {
			System.out.print(factor[i]);
			if (i != fcnt)
				System.out.print("*");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		n = new Long("2021041820210418");
		decompose(n);
		dfs(1, 1, 1, fcnt);
		System.out.println("枚举出来的所有的可能数目(存在重复)" + res);
		System.out.println("不重复的的结果数目" + s.size());
	}
	// 2021041820210418
}

class Node implements Comparable {
	long x, y, z;

	public Node(long x, long y, long z) {
		super();
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public long getX() {
		return x;
	}

	public void setX(long x) {
		this.x = x;
	}

	public long getY() {
		return y;
	}

	public void setY(long y) {
		this.y = y;
	}

	public long getZ() {
		return z;
	}

	public void setZ(long z) {
		this.z = z;
	}

	@Override
	public String toString() {
		return "Node [x=" + x + ", y=" + y + ", z=" + z + "]";
	}

	@Override
	public int compareTo(Object o) {
		Node b = (Node) o;
		long res = this.x != b.x ? this.x - b.x : this.y != b.y ? this.y - b.y : this.z - b.z;
		if (res > 0)
			return 1;
		else if (res < 0)
			return -1;
		return 0;
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值