Skip to content

Instantly share code, notes, and snippets.

@bz0
Last active September 19, 2019 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bz0/2d58dad97703f888b4536f2832767498 to your computer and use it in GitHub Desktop.
Save bz0/2d58dad97703f888b4536f2832767498 to your computer and use it in GitHub Desktop.
【試し中】Divisors of Power:https://yukicoder.me/problems/no/847
<?php
//素因数分解 試し割法
function p_factor($num){
$factor = [];
$max = ceil(sqrt($num))+1; //√n+1 誤差のおそれがあるので+1する
for($i=2; $i<=$max; $i++){
while($num % $i == 0){ //約数判定
$num = $num / $i;
$factor[$i] = isset($factor[$i]) ? $factor[$i] + 1 : 1;
}
}
if (!$factor){
$factor[$num] = 1;
return $factor;
}
if ($num > 1){
$factor[$num] = isset($factor[$num]) ? $factor[$num]++: 1;
}
return $factor;
}
$args = explode(" ", trim(fgets(STDIN)));//入力データを取得
$num = $args[0];
$factor = p_factor($num);
print_r($factor);
<?php
//約数を全部出力する
//数値が大きくなるとInfiniteとなり処理できなくなる・処理が長くタイムアウトする
$args = explode(" ", trim(fgets(STDIN)));
$N = $args[0];
$K = $args[1];
$M = $args[2];
$num = pow($N, $K);
$max = min($M, $num);
$divisor = [];
for($i=1;$i<=$max;$i++){
if($num%$i==0 && $M>$i){
$divisor[] = $num;
}
}
$count = count($divisor);
echo $count;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment