Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2019 /round3 /integers_as_service.html
wjomlex's picture
2019 Problems
e329daf verified
<p>
Looking for a new direction for his company, Carlos wants to break into the most exciting tech field: digital rights management (DRM).
In the world of DRM, mega-corporations wage perpetual war against a vast sea of e-pirates, modern-day Robin Hoods of ambiguous moral character.
</p>
<p>
One of the biggest players in this struggle is Sony, the well-known copyrighter of numbers. Considering the speed with which Sony puts out new products,
it's no wonder they're having trouble coming up with enough secret encryption keys to protect all of their intellectual property. Enter Carlos.
</p>
<p>
<em>
"Gone are the days of paying over-priced number theory PhDs to craft primes by hand. I can make you a system that will generate all the numbers you need,
to your exact specifications."
</em>
</p>
<p>
Sony wants to see a demonstration of Carlos's system before forking over millions of dollars in consultancy fees. They have some new products in development,
each of which requires a secret key, <strong>X</strong>. For each key, Sony has a list of <strong>N</strong> requirements.
The <em>i</em>th requirement has an operator character <strong>O<sub>i</sub></strong>, an integer value <strong>V<sub>i</sub></strong>,
and an integer result <strong>R<sub>i</sub></strong>.
</p>
<p>
When <strong>O<sub>i</sub></strong> is 'G', the <em>i</em>th requirement states that the greatest common divisor of
<strong>X</strong> and <strong>V<sub>i<sub></strong> must be <strong>R<sub>i</sub></strong>. That is, <code>GCD(X, V<sub>i</sub>) = R<sub>i</sub></code>.
</p>
<p>
When <strong>O<sub>i</sub></strong> is 'L', the <em>i</em>th requirement states that the least common multiple of
<strong>X</strong> and <strong>V<sub>i<sub></strong> must be <strong>R<sub>i</sub></strong>. That is, <code>LCM(X, V<sub>i</sub>) = R<sub>i</sub></code>.
</p>
<p>
There is also a global requirement that all of Sony's secret keys must be positive integers no larger than 1,000,000,000.
Help Carlos build any positive integer <strong>X</strong> consistent with all of these requirements, or determine that no such integer exists.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of secret keys that Sony wants Carlos to generate.
For each number, there is first a line containing the integer <strong>N</strong>.
Then, <strong>N</strong> lines follow, the <em>i</em>th of which contains the character <strong>O<sub>i</sub></strong>,
and the integers <strong>V<sub>i</sub></strong> and <strong>R<sub>i</sub></strong>, all separated by spaces.
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th secret key, print a line containing "Case #<em>i</em>: "
followed by a single integer: your chosen value of <strong>X</strong>, or -1 if no valid integer <strong>X</strong> exists.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 250 <br />
1 &le; <strong>N</strong> &le; 2,000 <br />
<strong>O<sub>i</sub></strong> &isin; {'G', 'L'} <br />
1 &le; <strong>V<sub>i</sub></strong>, <strong>R<sub>i</sub></strong> &le; 1,000,000,000 <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, <code>GCD(6, 4) = 2</code>, meaning that <strong>X</strong> = 6 satisfies the one and only requirement.
Note that, for this case and potentially other cases below, <strong>various other outputs will also be accepted</strong>.
</p>
<p>
In the second case, there exists no valid integer <strong>X</strong> such that 1 &le; <strong>X</strong> &le; 1,000,000,000 and <code>LCM(X, 4) = 2</code>.
</p>
<p>
In the third case, <code>GCD(24, 18) = 6</code>, <code>LCM(24, 40) = 120</code>, and <code>GCD(24, 20) = 4</code>, meaning that <strong>X</strong> = 24 is a valid choice.
</p>