📌  相关文章
📜  具有二进制表示形式的最大数字是m 1和m-1 0

📅  最后修改于: 2021-05-04 21:35:30             🧑  作者: Mango

给定n,找到严格地不大于n且其二进制表示形式由m个连续的1组成的最大数,然后由m-1个连续的零组成,并且没有其他值

例子:

Input : n = 7 
Output : 6 
Explanation: 6's binary representation is 110,
and 7's is 111, so 6 consists of 2 consecutive 
1's and then 1 consecutive 0.

Input : 130
Output : 120 
Explanation: 28 and 120 are the only numbers <=120,
28 is 11100 consists of 3 consecutive 1's and then 
2 consecutive 0's. 120 is 1111000 consists of 4 
consecutive 1's and then 3 consecutive 0's. So 120
is the greatest of number<=120 which meets the
given condition. 

天真的方法是从1遍历到N,并检查由m个连续的1和m-1个连续的0组成的每个二进制表示形式,并存储满足给定条件的最大二进制表示形式。

一种有效的方法是观察数字模式,

[ 1(1),6(110),28(11100),120(1111000),496(111110000),…。]

为了得到满足条件的数字的公式,我们以120为例-
120表示为1111000,其中m = 4 1和m = 3 0。将1111000转换为十进制,我们得到:
2 ^ 3 + 2 ^ 4 + 2 ^ 5 + 2 ^ 6,可以表示为(2 ^ m-1 + 2 ^ m + 2 ^ m + 1 +…2 ^ m + 2,2 ^ 2 * m)
2 ^ 3 *(1 + 2 + 2 ^ 2 + 2 ^ 3)可以表示为(2 ^(m-1)*(1 + 2 + 2 ^ 2 + 2 ^ 3 + .. 2 ^(m -1))
2 ^ 3 *(2 ^ 4-1)可以表示为[2 ^(m-1)*(2 ^ m -1)]。
因此,满足给定条件的所有数字都可以表示为

我们可以迭代直到数字不超过N并打印所有可能的元素中的最大元素。仔细观察会发现,在m = 33时,它将超过10 ^ 18标记,因此我们正在计算单位时间中的数字,因为log(32)接近计算pow所需的常数。
因此,总体复杂度将为O(1)。

C++
// CPP program to find largest number
// smaller than equal to n with m set
// bits then m-1 0 bits.
#include 
using namespace std;
  
// Returns largest number with m set
// bits then m-1 0 bits.
long long answer(long long n)
{
    // Start with 2 bits.
    long m = 2;
  
    // initial answer is 1
    // which meets the given condition
    long long ans = 1;
    long long r = 1;
  
    // check for all numbers
    while (r < n) {
  
        // compute the number
        r = (int)(pow(2, m) - 1) * (pow(2, m - 1));
  
        // if less then N
        if (r < n)
            ans = r;
  
        // increment m to get the next number
        m++;
    }
  
    return ans;
}
  
// driver code to check the above condition
int main()
{
    long long n = 7;
    cout << answer(n);
    return 0;
}


Java
// java program to find largest number
// smaller than equal to n with m set
// bits then m-1 0 bits.
public class GFG {
      
    // Returns largest number with 
    // m set bits then m-1 0 bits.
    static long answer(long n)
    {
           
        // Start with 2 bits.
        long m = 2;
       
        // initial answer is 1 which
        // meets the given condition
        long ans = 1;
        long r = 1;
       
        // check for all numbers
        while (r < n) {
       
            // compute the number
            r = ((long)Math.pow(2, m) - 1) * 
                ((long)Math.pow(2, m - 1));
       
            // if less then N
            if (r < n)
                ans = r;
       
            // increment m to get 
            // the next number
            m++;
        }
       
        return ans;
    }
  
    // Driver code   
    public static void main(String args[]) {
          
         long n = 7;
         System.out.println(answer(n));
    }
}
  
// This code is contributed by Sam007


Python3
# Python3 program to find 
# largest number smaller 
# than equal to n with m
# set bits then m-1 0 bits.
import math
  
# Returns largest number 
# with m set bits then
# m-1 0 bits.
def answer(n):
      
    # Start with 2 bits.
    m = 2;
      
    # initial answer is
    # 1 which meets the 
    # given condition
    ans = 1;
    r = 1;
      
    # check for all numbers
    while r < n:
          
        # compute the number
        r = (int)((pow(2, m) - 1) * 
                  (pow(2, m - 1)));
                   
        # if less then N
        if r < n:
            ans = r;
              
        # increment m to get 
        # the next number
        m = m + 1;
    return ans;
  
# Driver Code
print(answer(7));
  
# This code is contributed by mits.


C#
// C# program to find largest number
// smaller than equal to n with m set
// bits then m-1 0 bits.
using System;
  
class GFG {
  
// Returns largest number with 
// m set bits then m-1 0 bits.
static long answer(long n)
{
      
    // Start with 2 bits.
    long m = 2;
  
    // initial answer is 1 which
    // meets the given condition
    long ans = 1;
    long r = 1;
  
    // check for all numbers
    while (r < n) {
  
        // compute the number
        r = ((long)Math.Pow(2, m) - 1) * 
            ((long)Math.Pow(2, m - 1));
  
        // if less then N
        if (r < n)
            ans = r;
  
        // increment m to get 
        // the next number
        m++;
    }
  
    return ans;
}
  
    // Driver Code
    static public void Main ()
    {
        long n = 7;
        Console.WriteLine(answer(n));
    }
}
  
// This code is contributed by vt_m.


PHP


输出:

6