6kyu Steps in k-prime
生活随笔
收集整理的這篇文章主要介紹了
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;} }總結
以上是生活随笔為你收集整理的6kyu Steps in k-prime的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codewars 开篇
- 下一篇: 5kyu k-Primes