1103. Integer Factorization (30)

1103. Integer Factorization (30)
分析:显然有明显的递归特征:计算n,k,p,实际上只需求出n-i^p,k-1,p;考察回溯;
思路:每次保存该条路径,直至当前情况的递归终止,递归终止时,判断是否满足题设条件;用vmax记录最大因子序列和值,cmax记录当前的最大因子序列和值

#include <vector>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int vmax=-1,cmax=0;
vector<int> cv;
vector<vector<int>> ans;
bool comp(const vector<int> a,const vector<int> b)
{
    int i=0,len=(a.size()<b.size())?a.size():b.size();
    while(i<len&&a[i]==b[i]) ++i;
    return a[i]>b[i];
}
void Calculate(int n,int k, int p,int s)
{
    if(n<0||k<0)return;
    if(n==0&&k==0)
    {
        if(vmax<cmax)
        {
            ans.clear();
            vmax=cmax;
            ans.push_back(cv);
        }
        else if(vmax==cmax)
            ans.push_back(cv);
        return;
    }
    for(int i=s;i>=1;--i)
    {
        cv.push_back(i);
        cmax+=i;
        Calculate(n-(int)pow(i*1.0,p*1.0),k-1,p,i);
        cv.pop_back();
        cmax-=i;
    }
}
int main()
{
    int n,k,p;
    cin>>n>>k>>p;
    Calculate(n,k,p,n);
    if(!ans.size()){cout<<"Impossible";return 0;}
    sort(ans.begin(),ans.end(),comp);
    cout<<n<<" = ";
    for(auto it=ans[0].begin();it!=ans[0].end();++it)
        it!=ans[0].end()-1?cout<<*it<<"^"<<p<<" + ":cout<<*it<<"^"<<p;
}

以下2017.9.7更新

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int maxsum=-1;
vector<int> ans;
void solve(vector<int> &ans,vector<int> &rp,int sum,int cur,int n,int k,int p)
{
    if(n<0||k<0) return;
    if(k==0&&n==0&&(sum>maxsum||(sum==maxsum&&ans<rp)))
    {
        ans=rp;
        maxsum=sum;
        return;
    }
    for(int i=cur;i>=1;--i)
    {
        rp.push_back(i);
        solve(ans,rp,sum+i,i,n-pow(i*1.0,p*1.0),k-1,p);
        rp.pop_back();
    }
}
int main()
{
    int n,k,p;
    cin>>n>>k>>p;
    vector<int> rp;
    solve(ans,rp,0,n,n,k,p);
    if(ans.size())
    {
        printf("%d = ",n);
        for(auto it=ans.begin();it!=ans.end();++it)
        {
            printf("%d^%d",*it,p);
            if(it!=ans.end()-1) printf(" + ");
        }
    }
    else
        printf("Impossible");
    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.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值