-
-
Save Flaze07/6bf1497ed7de5fb7a968950a92e3472c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
auto factors( T )( T number ) | |
{ | |
import std.math; | |
bool[ T ] result; | |
for( size_t i = 1; i <= cast( uint ) sqrt( cast( double ) number ); ++i ) | |
{ | |
if ( number % i == 0) | |
{ | |
result[ i ] = true; | |
result[ number / i ] = true; | |
} | |
} | |
auto keysArr = result.keys; | |
return keysArr; | |
} | |
auto gcd( T )( ref T first, ref T second ) pure | |
{ | |
T tempOne = first, tempTwo = second, r; | |
while( tempTwo != 0 ) | |
{ | |
r = tempOne % tempTwo; | |
tempOne = tempTwo; | |
tempTwo = r; | |
} | |
return tempOne; | |
} | |
void main(string[] args) | |
{ | |
uint testCases; | |
readf( " %u", &testCases ); | |
while( testCases-- > 0 ) | |
{ | |
import std.container, std.algorithm; | |
uint aliceLength, bertaLength; | |
readf( " %u %u", &aliceLength, &bertaLength ); | |
uint[] aliceNum = new uint[ aliceLength ]; | |
ulong aliceTotal = 1; | |
for( size_t i = 0; i <aliceLength; ++i ) | |
{ | |
readf( " %u", &aliceNum[ i ] ); | |
aliceTotal = aliceTotal * aliceNum[ i ]; | |
} | |
ulong bertaTotal = 1; | |
uint[] bertaNum = new uint[ bertaLength ]; | |
for( size_t i = 0; i < bertaLength; ++i ) | |
{ | |
readf( " %u", &bertaNum[ i ] ); | |
bertaTotal = bertaTotal * bertaNum[ i ]; | |
} | |
auto toRemove = factors( gcd( aliceTotal, bertaTotal ) ); | |
uint similarities; | |
foreach( ref i ; aliceNum ) | |
{ | |
if( canFind( toRemove, i ) ) | |
{ | |
if( canFind( bertaNum, i ) ) | |
{ | |
similarities++; | |
} | |
} | |
} | |
writeln( similarities ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment