给定一个正整数N。任务是找到在整数除法(即q = N / d)上给出商(例如q )的除数(例如d ),以使商的设定位小于或等于除数。换句话说,找到可能值’d’的数量,该值将在整数除以’n’(q = N / d)时产生’q’,从而使’q’的置位位数小于或等于’d’(至少1) ‘。
例子 :
Input : N = 5
Output : 4
for d = 1 (set bit = 1),
q = 5/1 = 5 (set bit = 2), count = 0.
for d = 2 (set bit = 1),
q = 5/2 = 2 (set bit = 1), count = 1.
for d = 3 (set bit = 2),
q = 5/3 = 1 (set bit = 1), count = 2.
for d = 4 (set bit = 1),
q = 5/4 = 1 (set bit = 1), count = 3.
for d = 5 (set bit = 2),
q = 5/5 = 1 (set bit = 1), count = 4.
Input : N = 3
Output : 2
观察到,对于所有d> n ,q的置零位为0,因此没有点可以超过n。
同样,对于n / 2
等于或等于d。并且,d的最小可能值为1。因此,d的可能值为1至n。
现在,观察n除以d,其中1 <= d <= n ,它将按排序顺序给出q(递减)。
因此,随着d的增加,我们将得到q的减少。因此,我们得到了两个排序的序列,一个用于增加d
和其他减少q。现在,最初观察到d的置位位数大于q,但之后
d的设置位的点数小于或等于q。
因此,我们的问题减少了,从1 <= d <= n开始找到d的点,即“ x”,从而使q小于或等于d的置位。
因此,可能的d的计数(使q小于或等于d的置位)将等于n – x + 1。
以下是此方法的实现:
C++
// C++ Program to find number of Divisors
// which on integer division produce quotient
// having less set bit than divisor
#include
using namespace std;
// Return the count of set bit.
int bit(int x)
{
int ans = 0;
while (x) {
x /= 2;
ans++;
}
return ans;
}
// check if q and d have same number of set bit.
bool check(int d, int x)
{
if (bit(x / d) <= bit(d))
return true;
return false;
}
// Binary Search to find the point at which
// number of set in q is less than or equal to d.
int bs(int n)
{
int l = 1, r = sqrt(n);
// while left index is less than right index
while (l < r) {
// finding the middle.
int m = (l + r) / 2;
// check if q and d have same number of
// set it or not.
if (check(m, n))
r = m;
else
l = m + 1;
}
if (!check(l, n))
return l + 1;
else
return l;
}
int countDivisor(int n)
{
return n - bs(n) + 1;
}
// Driven Program
int main()
{
int n = 5;
cout << countDivisor(n) << endl;
return 0;
}
Java
// Java Program to find number
// of Divisors which on integer
// division produce quotient
// having less set bit than divisor
import java .io.*;
class GFG
{
// Return the count of set bit.
static int bit(int x)
{
int ans = 0;
while (x > 0)
{
x /= 2;
ans++;
}
return ans;
}
// check if q and d have
// same number of set bit.
static boolean check(int d, int x)
{
if (bit(x / d) <= bit(d))
return true;
return false;
}
// Binary Search to find
// the point at which
// number of set in q is
// less than or equal to d.
static int bs(int n)
{
int l = 1, r = (int)Math.sqrt(n);
// while left index is
// less than right index
while (l < r)
{
// finding the middle.
int m = (l + r) / 2;
// check if q and d have
// same number of
// set it or not.
if (check(m, n))
r = m;
else
l = m + 1;
}
if (!check(l, n))
return l + 1;
else
return l;
}
static int countDivisor(int n)
{
return n - bs(n) + 1;
}
// Driver Code
static public void main (String[] args)
{
int n = 5;
System.out.println(countDivisor(n));
}
}
// This code is contributed by anuj_67.
Python3
# Python3 Program to find number
# of Divisors which on integer
# division produce quotient
# having less set bit than divisor
import math
# Return the count of set bit.
def bit(x) :
ans = 0
while (x) :
x /= 2
ans = ans + 1
return ans
# check if q and d have
# same number of set bit.
def check(d, x) :
if (bit(x /d) <= bit(d)) :
return True
return False;
# Binary Search to find
# the point at which
# number of set in q is
# less than or equal to d.
def bs(n) :
l = 1
r = int(math.sqrt(n))
# while left index is less
# than right index
while (l < r) :
# finding the middle.
m = int((l + r) / 2)
# check if q and d have
# same number of
# set it or not.
if (check(m, n)) :
r = m
else :
l = m + 1
if (check(l, n) == False) :
return math.floor(l + 1)
else :
return math.floor(l)
def countDivisor(n) :
return n - bs(n) + 1
# Driver Code
n = 5
print (countDivisor(n))
# This code is contributed by
# Manish Shaw (manishshaw1)
C#
// C# Program to find number of Divisors
// which on integer division produce quotient
// having less set bit than divisor
using System;
class GFG {
// Return the count of set bit.
static int bit(int x)
{
int ans = 0;
while (x > 0) {
x /= 2;
ans++;
}
return ans;
}
// check if q and d have
// same number of set bit.
static bool check(int d, int x)
{
if (bit(x / d) <= bit(d))
return true;
return false;
}
// Binary Search to find
// the point at which
// number of set in q is
// less than or equal to d.
static int bs(int n)
{
int l = 1, r = (int)Math.Sqrt(n);
// while left index is
// less than right index
while (l < r) {
// finding the middle.
int m = (l + r) / 2;
// check if q and d have
// same number of
// set it or not.
if (check(m, n))
r = m;
else
l = m + 1;
}
if (!check(l, n))
return l + 1;
else
return l;
}
static int countDivisor(int n)
{
return n - bs(n) + 1;
}
// Driver Code
static public void Main ()
{
int n = 5;
Console.WriteLine(countDivisor(n));
}
}
// This code is contributed by anuj_67.
PHP
输出:
4
时间复杂度: O(logN)
辅助空间: O(1)