📌  相关文章
📜  将N以下的所有整数表示为总和所需的最小数字

📅  最后修改于: 2021-04-29 13:53:05             🧑  作者: Mango

我们有一个整数N。我们需要将N表示为K个整数的总和,这样,通过将这些整数中的一些(或全部)相加,我们可以获得范围[1,N]中的所有数字。 K的最小值是多少?

例子:

Input  : N = 7
Output : 3
Explanation : Three integers are 1, 2, 4. By adding some(or all) of these groups we can get all number in the range 1 to N. 
1; 2; 1+2=3; 4; 1+4=5; 2+4=6; 1+2+4=7

Input  : N = 32
Output : 6
Explanation : Six integers are 1, 2, 4, 8, 16, 1.

第一,我们手工解决小批量的问题。
n = 1:1
n = 2:1,1
n = 3:1、2
n = 4:1、2、1
n = 5:1、2、2
n = 6:1、2、3
n = 7:1、2、4
n = 8:1、2、4、1

如果我们仔细检查,我们可以看到N=2^{m}-1那么整数是[1, 2, 4..., 2^{m-1}] 。这只是另一种说法2^0+2^1+2^2+...+2^{m-1} = \frac{2^{m}-1}{2-1} = 2^{m}-1所以现在我们知道2^{m}-1 K的最小值是m。

现在我们检查发生了什么2^{m} 。为了2^{m}我们只是将一个新的整数1添加到我们的整数列表中。意识到每个数字2^{m} to 2^{m+1}-1我们可以将新添加的整数增加1,这将是最佳的整数列表。为了验证从N = 4到N = 7,最小K不变;每步只增加最后一个整数。

当然,我们可以在O(log N)时间内以迭代方式实现此目标(通过在列表中插入2的连续幂,最后一个元素的形式为N-(2 ^ n-1))。但这与找到N的二进制表达式的长度完全相同,这也可以在O(log N)的时间内完成。

C++
// CPP program to find count of integers needed
// to express all numbers from 1 to N.
#include 
using namespace std;
  
// function to count length of binary expression of n
int countBits(int n)
{
    int count = 0;
    while (n) {
        count++;
        n >>= 1;
    }
    return count;
}
  
// Driver code
int main()
{
    int n = 32;
    cout << "Minimum value of K is = " 
         << countBits(n) << endl;
    return 0;
}


Java
// Java  program to find count of integers needed 
// to express all numbers from 1 to N
  
import java.io.*;
  
class GFG {
      
// function to count length of binary expression of n 
static int countBits(int n) 
{ 
    int count = 0; 
    while (n>0) { 
        count++; 
        n >>= 1; 
    } 
    return count; 
} 
  
// Driver code 
    public static void main (String[] args) {
        int n = 32; 
        System.out.println("Minimum value of K is = "+
             countBits(n));
          
    }
}


Python 3
# Python3 program to find count of integers 
# needed to express all numbers from 1 to N. 
  
# function to count length of 
# binary expression of n 
def countBits(n):
  
    count = 0; 
    while (n):
        count += 1; 
        n >>= 1;
          
    return count; 
  
# Driver code 
n = 32; 
print("Minimum value of K is =", 
                  countBits(n)); 
  
# This code is contributed by mits


C#
// C# program to find count of 
// integers needed to express all 
// numbers from 1 to N
using System;
  
class GFG
{
// function to count length of 
// binary expression of n 
static int countBits(int n) 
{ 
    int count = 0; 
    while (n > 0) 
    { 
        count++; 
        n >>= 1; 
    } 
    return count; 
} 
  
// Driver code
static public void Main ()
{
    int n = 32; 
    Console.WriteLine("Minimum value of K is = "+
                                   countBits(n));
}
}
  
// This code is contributed
// by Sach_Code


PHP
>= 1; 
    } 
    return $count; 
} 
  
// Driver code 
$n = 32; 
echo "Minimum value of K is = ",
      countBits($n), "\n"; 
  
// This code is contributed by Sachin
?>


输出:

Minimum value of K is = 6

请参阅计数设置位,以获取更有效的方法来对整数进行计数。