Skip to content

Instantly share code, notes, and snippets.

@flymio
Created October 7, 2018 00:09
Show Gist options
  • Save flymio/dfaff2ee28e94b0c536f886779d3a487 to your computer and use it in GitHub Desktop.
Save flymio/dfaff2ee28e94b0c536f886779d3a487 to your computer and use it in GitHub Desktop.
<?php
/*
* Complete the getTotalX function below.
*/
function getTotalX($a, $b) {
$f = lcm($a);
$l = gcd($b);
$count = 0;
for($i = $f, $j=2; $i<=$l; $i=$f*$j,$j++){
if($l % $i == 0){ $count++;}
}
return $count;
}
function gcd($a){
$res = $a[0];
for($i=1;$i<sizeof($a);$i++){
$res = gcd_concat($res, $a[$i]);
}
return $res;
}
function gcd_concat($a,$b){
while($b > 0 ){
$temp = $b;
$b = $a % $b;
$a = $temp;
}
return $a;
}
function lcm_concat($a, $b){
return $a * ($b / gcd_concat($a, $b));
}
function lcm($a){
$res = $a[0];
for ($i = 1; $i < sizeof($a); $i++) {
$res = lcm_concat($res, $a[$i]);
}
return $res;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$__fp = fopen("php://stdin", "r");
fscanf($__fp, "%[^\n]", $nm_temp);
$nm = explode(' ', $nm_temp);
$n = intval($nm[0]);
$m = intval($nm[1]);
fscanf($__fp, "%[^\n]", $a_temp);
$a = array_map('intval', preg_split('/ /', $a_temp, -1, PREG_SPLIT_NO_EMPTY));
fscanf($__fp, "%[^\n]", $b_temp);
$b = array_map('intval', preg_split('/ /', $b_temp, -1, PREG_SPLIT_NO_EMPTY));
$total = getTotalX($a, $b);
echo $total."\n";
fwrite($fptr, $total . "\n");
fclose($__fp);
fclose($fptr);
Не смог решить сразу, почитал борду, узнал что такое LCM и GCD:
Наименьшее общее кратное и наибольший общий делитель.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment