Skip to content

Instantly share code, notes, and snippets.

@Flaze07
Last active June 15, 2018 11:29
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 Flaze07/6bf1497ed7de5fb7a968950a92e3472c to your computer and use it in GitHub Desktop.
Save Flaze07/6bf1497ed7de5fb7a968950a92e3472c to your computer and use it in GitHub Desktop.
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