📌  相关文章
📜  [L, R] 范围内的数字计数,只有 2 或 7 作为质因数

📅  最后修改于: 2022-05-13 01:56:09.953000             🧑  作者: Mango

[L, R] 范围内的数字计数,只有 2 或 7 作为质因数

给定两个整数LR ,任务是找出[L, R]范围内只有27作为素因数的数字的计数

例子:

朴素方法:简单的方法是生成[L, R]范围内每个数字的所有素因子,并检查因子是否只有 2 或 7。

请按照以下步骤解决问题:

  • i = L遍历到R
    • i的所有质因数存储在一个向量中(比如factor )。
    • 遍历向量以查看是否存在除27之外的因子
    • 如果i只能被27整除,则增加count否则。
  • 返回一对特殊正则作为最终答案。

以下是上述方法的实现:

C++14
// C++ code for the above approach
 
#include 
using namespace std;
 
// Function to find regular or special numbers
pair SpecialorRegular(int L, int R)
{
    int regular = 0, special = 0, temp, i, j;
    vectorfactors;
     
    // Base cases
    if(L > R || L < 0|| R < 0)
    return {-1, -1};
    else if(R < 2)
    regular += R - L + 1;
    else regular += 2 - L;
    L = 2;
     
    for(i = L; i <= R; i++){
        temp = i;
        factors.clear();
         
        for(j = 2; j * j <= i; j++)
        {
            while(temp % j == 0){
            factors.push_back(j);
            temp /= j;
            }
        }
        if(temp > 1)
        factors.push_back(temp);
         
        for(j = 0; j < factors.size(); j++){
            if(factors[j] != 7
               && factors[j] != 2)
            break;
        }
         
        if(j == factors.size())
        special++;
        else regular++;
    }
     
    return {special, regular};
}
 
//Function to print
void print(int L, int R){
    pairans
        = SpecialorRegular(L, R);
    cout << ans.first;
}
 
//Driver code
int main()
{
    int L = 0;
    int R = 2;
     
    // Function Call
    print(L, R);
    return 0;
}


Python3
# Python3 code for the above approach
 
# Function to find regular or special numbers
def SpecialorRegular(L, R):
    regular, special = 0, 0
    factors = []
     
    # base cases
    if L > R or L < 0 or R < 0:
        return [-1, -1]
    elif R < 2:
        regular += R - L + 1
    else:
        regular += 2 - L
    L = 2
    for i in range(L, R + 1):
        temp = i
        factors = []
        for j in range(2, 1 + int(i ** 0.5)):
            while not temp % j:
                factors.append(j)
                temp //= j
        if temp > 1:
            factors.append(temp)
        j = 0
        while j < len(factors):
            if factors[j] != 7 and factors[j] != 2:
                break
            j += 1
        if j == len(factors):
            special += 1
        else:
            regular += 1
    return [special, regular]
   
# Function to print
def prints(L, R):
    ans = SpecialorRegular(L, R)
    print(ans[0])
 
# Driver code
L, R = 0, 2
 
# Function Call
prints(L, R)
 
# This code is contributed by phasing17


Javascript


C++14
// C++ code for the above approach:
 
#include 
using namespace std;
 
int special = 0;
unordered_map visited;
 
// Function to find special numbers
void countSpecial(int L, int R, int temp)
{
 
    // Base cases
    if (L > R) {
        special = -1;
        return;
    }
    else if (L < 0 || R < 0) {
        special = -1;
        return;
    }
    else if (temp > R)
        return;
 
    if (L <= temp && temp <= R && temp != 1
        && !visited[temp]) {
        special++;
        visited[temp] = 1;
    }
 
    countSpecial(L, R, temp * 2);
    countSpecial(L, R, temp * 7);
}
 
// Print function
void print(int L, int R)
{
    countSpecial(L, R, 1);
    if (special == -1)
        cout << -1 << " " << -1;
    else
        cout << special;
}
 
// Driver code
int main()
{
    int L = 0;
    int R = 2;
 
    // Function call
    print(L, R);
    return 0;
}


Javascript


输出
1 2

时间复杂度: O((R – L) (3 / 2) )
辅助空间: O(log R)

有效方法:根据上述观察,可以更有效地解决问题:

观察:

请按照以下步骤解决问题:

  • 将无序地图初始化为已访问以存储已访问的数字并为 0 以存储此类数字的计数。
  • 准备一个递归函数来生成观察中显示的数字。
  • 如果生成的数字(比如temp )是:
    • [L, R]范围内且尚未访问过,然后将其标记为已访问并增加count
    • 超过R或已访问从该递归返回并尝试其他选项。
  • 返回计数作为最终要求的答案。

以下是上述方法的实现:

C++14

// C++ code for the above approach:
 
#include 
using namespace std;
 
int special = 0;
unordered_map visited;
 
// Function to find special numbers
void countSpecial(int L, int R, int temp)
{
 
    // Base cases
    if (L > R) {
        special = -1;
        return;
    }
    else if (L < 0 || R < 0) {
        special = -1;
        return;
    }
    else if (temp > R)
        return;
 
    if (L <= temp && temp <= R && temp != 1
        && !visited[temp]) {
        special++;
        visited[temp] = 1;
    }
 
    countSpecial(L, R, temp * 2);
    countSpecial(L, R, temp * 7);
}
 
// Print function
void print(int L, int R)
{
    countSpecial(L, R, 1);
    if (special == -1)
        cout << -1 << " " << -1;
    else
        cout << special;
}
 
// Driver code
int main()
{
    int L = 0;
    int R = 2;
 
    // Function call
    print(L, R);
    return 0;
}

Javascript


输出
5 10

时间复杂度: O(R)
辅助空间: O(R – L)