6kyu Steps in k-prime

6kyu Steps in k-primes

题目背景:

A natural number is called k-prime if it has exactly k prime factors, counted with multiplicity.

Task:

We will write a function kprimes_step(k, step, start, nd) with parameters:

  • k (integer > 0) which indicates the type of k-primes we are looking for,
  • step (integer > 0) which indicates the step we want to find between two k-primes,
  • start (integer >= 0) which gives the start of the search (start inclusive),
  • nd (integer >= start) which gives the end of the search (nd inclusive)

题目分析:

本道题主要是 如何计数素因子的个数,关于计数素数因子个数的方法,存在非常经典的因子分解的模板思路,先附上计数素数个 数的函数:

int KPrimes::count_primes(long long num){
    long long i = 2;
    int cnt = 0;
    while( i * i <= num ) {
        while( num % i == 0 ) {
            num /= i;
            cnt++;
        }
        i++;
    }
    if ( num != 1 ) cnt++;
    return cnt;
}

最终AC的代码:

#include <vector>

namespace KStep{
    int count_primes(long long num){
        long long i = 2;
        int cnt = 0;
        while( i * i <= num ) {
            while( num % i == 0 ) {
                num /= i;
                cnt++;
            }
            i++;
        }
        if ( num != 1 ) cnt++;
        return cnt;
    }
    
    std::vector<std::pair <long, long>> kprimesStep(int k, int step, long long m, long long n){
        if ( k < 1 || m > n || step < 1 ) return {};
        std::vector<std::pair <long, long>> res;
        for ( long long i = m; i <= n - step; i++) {
            if ( count_primes(i) == k && count_primes(i + step) == k) {
                std::pair <long, long> seg = std::make_pair ( i, i + step );
                res.push_back(seg);
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值