简体   繁体   English

分区编号 N分为M个分区

[英]Partitioning a no. N into M partitions

I'm trying a problem in which I have to partition a no. 我正在尝试一个必须对一个分区进行分区的问题。 N into M partitions as many as possible. N尽可能分成M个分区。

Example: 例:

N=1 M=3 , break 1 into 3 parts N = 1 M = 3,将1分成3部分

0 0 1 0 0 1
0 1 0 0 1 0
1 0 0 1 0 0

N=3 M=2 , break 3 into 2 parts N = 3 M = 2,将3分成2部分

2 1 2 1
1 2 1 2
3 0 3 0
0 3 0 3

N=4 M=4 , break 4 into 4 parts N = 4 M = 4,将4分成4部分

0 0 0 4 0 0 0 4
0 0 4 0 0 0 4 0
0 4 0 0 0 4 0 0
4 0 0 0 4 0 0 0
0 0 1 3 0 0 1 3
0 1 0 3 0 1 0 3
0 1 3 0 0 1 3 0
.
.
.

and so on. 等等。

I did code a backtrack algo. 我确实编写了回溯算法。 which produce all the possible compositions step by step, but it chokes for some larger input.Because many compositions are same differing only in ordering of parts.I want to reduce that.Can anybody help in providing a more efficient method. 它会逐步生成所有可能的合成,但是会占用较大的输入。因为许多合成只是在零件排序上有所不同。我想减少这一点。任何人都可以帮助您提供一种更有效的方法。

My method: 我的方法:

void backt(int* part,int pos,int n) //break N into M parts
{
    if(pos==M-1)
    {
        part[pos]=n;
        ppart(part);   //print part array
        return;
    }

    if(n==0)
    {
        part[pos]=0;
        backt(part,pos+1,0);
        return;
    }

    for(int i=0;i<=n;i++)
    {
        part[pos]=i;

        backt(part,pos+1,n-i);
    }
}

In my algo. 在我的算法中。 n is N and it fill the array part[] for every possible partition of N. n为N,它为N的每个可能分区填充数组part []。

What I want to know is once generating a composition I want to calculate how many times that composition will occur with different ordering.For ex: for N=1 ,M=3 ::: composition is only one : <0,0,1> ,but it occurs 3 times. 我想知道的是一旦生成一个合成,我想计算该合成将以不同顺序发生多少次。例如:对于N = 1,M = 3 :::合成只有一个:<0,0,1 >,但发生3次。 Thats what I want to know for every possible unique composition. 那就是我想知道的每种可能的独特构图。

for another example: N=4 M=4 再举一个例子:N = 4 M = 4

composition <0 0 0 4> is being repeated 4 times. 组成<0 0 0 4>被重复4次。 Similarly, for every unique composition I wanna know exactly how many times it will occur . 同样,对于每种独特的成分,我都想确切知道它将发生多少次。

Looks like I'm also getting it by explaining here.Thinking. 看起来我也可以通过在这里解释来理解它。

Thanks. 谢谢。

You can convert an int to a partitioning as follows: 您可以将int转换为分区,如下所示:

vector<int> part(int i, int n, int m)
{
    int r = n; // r is num items remaining to be allocated

    vector<int> result(m, 0); // m entries inited to 0

    for (int j = 0; j < m-1; j++)
    {
        if (r == 0) // if none left stop
            break;

        int k = i % r; // mod out next bucket
        i /= r; // divide out bucket
        result[j] = k; // assign bucket
        r -= k; // remove assigned items from remaining
    }

    result[m-1] = r; // put remainder in last bucket

    return result;
}

So you can use this as follows: 因此,您可以按以下方式使用它:

for (int i = 0; true; i++)
{
    vector<int> p = part(i, 3, 4);

    if (i != 0 && p.back() == 3) // last part
       break;

    ... // use p

};

It should be clear from this how to make an incremental version of part too. 由此也应该清楚如何制作零件的增量版本。

A much simpler and mathematical approach: 一种更简单,更数学的方法:

This problem is equivalent to finding the co-efficient of x^N in the expression f(x) = (1+x+x^2+x^3+....+x^N)^M 此问题等效于在表达式f(x)=(1 + x + x ^ 2 + x ^ 3 + .... + x ^ N)^ M中找到x ^ N的系数

f(x) = ((x^(N-1) - 1)/(x-1))^M differentiate it M times(d^Nf(x)/dx^N) and the co-efficient will be (1/n!)*(d^Nf(x)/dx^N) at x = 0; f(x)=((x ^(N-1)-1)/(x-1))^ M将其微分M倍(d ^ Nf(x)/ dx ^ N)且系数为( 1 / n!)*(d ^ Nf(x)/ dx ^ N)x = 0;

differentiation can be done using any numerical differentiation technique. 可以使用任何数值微分技术来完成微分。 So the complexity of the algorithm is O(N*complexity_of_differentiation).. 因此,算法的复杂度为O(N * complexity_of_differentiation)。

给定n个数字的排列数组,计算编号。 元组 (i,j,k,l,m) 使得 i <j<k<l<m and a[i]<a[k]<a[j]<a[m]<a[l]< div><div id="text_translate"><p> 对于软件开发人员面试的硬算法回合,我最近被问到这个问题。 我能够使用蛮力/递归来做到这一点,不确定预期的方法,对此有什么帮助吗?</p><blockquote><p> 给定n数字的排列数组,计算元组数(i,j,k,l,m)使得i&lt;j&lt;k&lt;l&lt;m和a[i]&lt;a[k]&lt;a[j]&lt;a[m]&lt;a[l] 。</p></blockquote><p> 我的方法-&gt; 使用递归从给定数组中尝试 (i,j,k,l,m) 的所有元组,并仅考虑满足条件的元组,例如 N 选择 5,因为这具有指数时间复杂性,我想让它成为三次方或二次方</p></div></j<k<l<m> - Given an array of permutation of n numbers calculate the no. of tuples (i,j,k,l,m) such that i<j<k<l<m and a[i]<a[k]<a[j]<a[m]<a[l]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图理解为什么使用 BFS 搜索网格的空间复杂度是 O(m * n),其中 m 不是。 行数,n 为否。 列数 - Trying to understand why the Space Complexity of searching through a grid using BFS is O(m * n), where m is no. of rows and n is no. of cols 具有给定 k 个分区的 Python 整数分区 - Python Integer Partitioning with given k partitions 公平地将集合S划分为k个分区 - fair partitioning of set S into k partitions 数数 O(n)中的单词数 - Count no. of words in O(n) 给定n个数字的排列数组,计算编号。 元组 (i,j,k,l,m) 使得 i <j<k<l<m and a[i]<a[k]<a[j]<a[m]<a[l]< div><div id="text_translate"><p> 对于软件开发人员面试的硬算法回合,我最近被问到这个问题。 我能够使用蛮力/递归来做到这一点,不确定预期的方法,对此有什么帮助吗?</p><blockquote><p> 给定n数字的排列数组,计算元组数(i,j,k,l,m)使得i&lt;j&lt;k&lt;l&lt;m和a[i]&lt;a[k]&lt;a[j]&lt;a[m]&lt;a[l] 。</p></blockquote><p> 我的方法-&gt; 使用递归从给定数组中尝试 (i,j,k,l,m) 的所有元组,并仅考虑满足条件的元组,例如 N 选择 5,因为这具有指数时间复杂性,我想让它成为三次方或二次方</p></div></j<k<l<m> - Given an array of permutation of n numbers calculate the no. of tuples (i,j,k,l,m) such that i<j<k<l<m and a[i]<a[k]<a[j]<a[m]<a[l] 有限制地将 n 分割为 k 个部分 - Partitions of n into k parts with restrictions 如何计算n的分区数? - How to calculate number of partitions of n? 长度为n的长度为3的不同子序列的数量 - No. of distinct subsequences of length 3 in an array of length n O中第N个自然数的总和(N中的位数) - Sum of 1st N natural numbers in O(no. of digits in N) O(N^2) 时间内的回文分区问题 - Palindrome Partitioning problem in O(N^2) time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM