1103. Integer Factorization

本文介绍了一种使用深度优先搜索(DFS)解决特定数学问题的方法:给定N、k和p,寻找N等于k个数的p次方之和的所有可能组合,并在这些组合中找到总和最大且字典序最大的一组数。

这里写图片描述

题目的大体意思就是,给出N,k,p三个数字
找出满足N=n_1^P+…n_k^P的所有n的值,并且在所有方法中找到n_1+…+n_k最大的值,如果大小相同,就选择字典序最大的一组数组。
这道题肯定是要通过搜索去寻找最合适的组合,明显是dfs(有点贪心的意思在里面)
首先,先将index^p

#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;

vector<int> tmpAns,Ans,data;
int n,k,p,maxfac=-1;
int myindex=1;


void init()
{
    int tmp=0;
    while(tmp<=n)
    {
        data.push_back(tmp);
        tmp=pow(myindex,p);
        myindex++;
    }
}


void dfs(int index,int tmpNum,int tmpK,int fac)
{
    if(tmpNum==n && tmpK==k)
    {
        if(fac>maxfac)
        {
            Ans=tmpAns;
            maxfac=fac;
        }
        return;
    }
    if(tmpNum>n || tmpK>k) return;
    if(index>0)
    {
        tmpAns.push_back(index);
        dfs(index,tmpNum+data[index],tmpK+1,fac+index);
        tmpAns.pop_back();
        dfs(index-1,tmpNum,tmpK,fac);
    }
}

int  main()
{
    scanf("%d %d %d",&n,&k,&p);
    init();
    dfs(data.size()-1,0,0,0);
    if(maxfac==-1) printf("Impossible\n");
    else
    {
        printf("%d = ",n);
        for(int i=0;i<Ans.size();++i)
        {
            if(i==0) printf("%d^%d",Ans[i],p);
            else printf(" + %d^%d",Ans[i],p);
        }
    }
    return 0;
}
To determine the count of 'good' integers between $ l $ and $ r $, where a 'good' integer is defined as a number whose prime factors all have at least two digits, we need to understand the constraints and properties of such numbers. ### Understanding the Criteria A 'good' integer must have all of its prime factors with at least two digits. This means that all prime factors of the number must be greater than or equal to 11, as 11 is the smallest two-digit prime number. Any number that has a prime factor with only one digit (i.e., 2, 3, 5, or 7) cannot be classified as a 'good' integer. To count such numbers efficiently, we can use a sieve-like approach to precompute all numbers between $ l $ and $ r $ that meet the criteria. ### Algorithm Overview 1. **Sieve of Eratosthenes**: Generate all prime numbers up to a certain limit (e.g., $ r $). 2. **Prime Factorization**: For each number between $ l $ and $ r $, determine its prime factors. 3. **Check Criteria**: Verify if all prime factors of each number have at least two digits. 4. **Count Good Integers**: Count the numbers that satisfy the criteria. ### Code Implementation Here is a Python implementation of the algorithm described above: ```python def sieve(limit): is_prime = [True] * (limit + 1) is_prime[0] = is_prime[1] = False for i in range(2, int(limit**0.5) + 1): if is_prime[i]: for multiple in range(i * i, limit + 1, i): is_prime[multiple] = False return [i for i, prime in enumerate(is_prime) if prime] def is_good(n, primes): # Check if all prime factors of n have at least two digits if n < 2: return False for p in primes: if p * p > n: if n >= 10: break else: return False if n % p == 0: if p < 10: return False while n % p == 0: n //= p return n >= 10 or n == 1 def count_good_integers(l, r): primes = sieve(r) count = 0 for num in range(l, r + 1): if is_good(num, primes): count += 1 return count # Example usage l = 100 r = 200 result = count_good_integers(l, r) print(f"The count of 'good' integers between {l} and {r} is {result}") ``` ### Explanation of the Code - **Sieve of Eratosthenes**: The `sieve` function generates all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm. - **Prime Factorization**: The `is_good` function checks if all prime factors of a given number $ n $ have at least two digits. It iterates through the list of primes and checks if any of them divide $ n $. If a one-digit prime divides $ n $, it returns `False`. - **Counting Good Integers**: The `count_good_integers` function iterates through the range $[l, r]$ and counts the numbers that are classified as 'good' integers based on the criteria defined in the `is_good` function. ### Complexity The time complexity of this approach is dominated by the Sieve of Eratosthenes, which is $ O(n \log \log n) $, where $ n $ is the upper bound $ r $. The factorization step for each number in the range $[l, r]$ contributes additional complexity, but it is generally efficient due to the use of precomputed primes. ### Optimization To further optimize the algorithm, precompute the smallest prime factor (SPF) for each number up to $ r $. This allows for faster factorization of numbers in the range $[l, r]$. ```python def precompute_spf(limit): spf = list(range(limit + 1)) for i in range(2, int(limit**0.5) + 1): if spf[i] == i: for multiple in range(i * i, limit + 1, i): if spf[multiple] == multiple: spf[multiple] = i return spf def is_good_optimized(n, spf): if n < 2: return False while n > 1: p = spf[n] if p < 10: return False while n % p == 0: n //= p return True def count_good_integers_optimized(l, r): spf = precompute_spf(r) count = 0 for num in range(l, r + 1): if is_good_optimized(num, spf): count += 1 return count # Example usage l = 100 r = 200 result = count_good_integers_optimized(l, r) print(f"The count of 'good' integers between {l} and {r} is {result}") ``` This optimized approach reduces the time required for factorization by leveraging precomputed smallest prime factors, making it more efficient for larger ranges. ### Conclusion The count of 'good' integers between $ l $ and $ r $ can be efficiently determined using a sieve-based approach to precompute prime factors and verify the criteria for each number in the range.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值