日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

6kyu Steps in k-prime

發(fā)布時間:2025/3/21 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 6kyu Steps in k-prime 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)

題目分析:

本道題主要是 如何計數(shù)素因子的個數(shù),關(guān)于計數(shù)素數(shù)因子個數(shù)的方法,存在非常經(jīng)典的因子分解的模板思路,先附上計數(shù)素數(shù)個 數(shù)的函數(shù):

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;} }

總結(jié)

以上是生活随笔為你收集整理的6kyu Steps in k-prime的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。