【问题标题】:Fast computation of multi-category number of combinations [closed]多类别组合数的快速计算[关闭]
【发布时间】:2012-02-02 05:36:05
【问题描述】:

我必须为重复对象的排列评估以下公式

n!/(r1! * r2! * r3! * ......... * rn!)

wheren <= 5001 <= ri <= 10 (总共有n个对象,其中r1与第一种相似,r2与第二种相似,以此类推,公式表示这些对象的排列数)。

为此,我需要一个高效的编码解决方案,因为在 Java 中处理大整数并不能证明对于大型案例有成效。

提前致谢。

【问题讨论】:

  • 我天真地编码了它,通过使用 BigIntegers 和除法的正常乘法评估阶乘,但程序挂在边界情况上,这就是我需要它的原因:( :(
  • 嗯,减少这一比例的机会很多。
  • 我会尝试 C 库 gmp (gmplib.org)。
  • 我被要求扩展我关于如何减少分数的建议,所以这里。由于n <= 500,很有可能构建一个大小为n 的数组。使用 Eratosthenes 的筛子查找并存储直到 n 的所有非质数的因子对。制作另一个大小为n 的表,它代表每个因子的指数。现在,迭代每个阶乘。使用筛表将每个因子分解为素因子,将分子中的阶乘的指数表递增,分母中的每个阶乘递减。没有条目最终会是负面的。
  • 现在,使用BigInteger-type 类将未取消的因子相乘。任何时候都不需要除法运算。也没有任何分数。

标签: java c++ c combinatorics


【解决方案1】:

你可以在java中使用

public class Permutation

旨在解决您的一种问题。

查看此链接以获取您的reference

像这样:

private static Double calculatePermutationEntropy(List<Double> values, int baseOrder) {
 int valuesSize = values.size();
 if (baseOrder >= valuesSize + 1) {
   throw new RuntimeException("The size of the values is bigger than the order");
 }

 List<String> result = new ArrayList<String>();
 // iterate over the input
 for (int i = 0; i < valuesSize - baseOrder + 1; i++) {
   List<Double> neightbors = values.subList(i, i + baseOrder);

   List<Double> orderedValues = new ArrayList<Double>(neightbors);

   String window = "";
   for (int j = 0; j < neightbors.size(); j++) {
     // add the indexes in a string representation
     window += orderedValues.indexOf(neightbors.get(j));
   }
 result.add(window);
 }
 // use the shannon entropy calculation to get the result
 return calculateShannonEntropy(result);
}

source

【讨论】:

  • 你能解释一下你写了什么
  • 你访问过我指定的链接吗??
  • 组合数问题中不应该有分数。因此没有理由使用double
  • @Ben Viogt:我的错!!谢谢你指出..!!!
猜你喜欢
  • 2014-09-13
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多