Created
January 19, 2015 15:07
-
-
Save anonymous/d5389eb0ca1df1844ec6 to your computer and use it in GitHub Desktop.
sopj 7190. Guess the Number | GUESSTHE
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
from sys import stdin | |
def main(): | |
def gcd(x, y): | |
while y: | |
x, y = y, x % y | |
return x | |
factors = ((), (), (), (2,), (), (3, 2), (), (4, 2), (3,), (5, 2), (), (6, 4, 3, 2), (), (7, 2), (5, 3), (8, 4, 2), (), (9, 6, 3, 2), (), (10, 5, 4, 2)) | |
def the_number(s): | |
if s[0] == 'N': | |
return '-1' | |
number = 1 | |
for i, x in enumerate(s): | |
if x == 'Y': | |
for y in factors[i]: | |
if s[y - 1] == 'N': | |
return '-1' | |
number *= (i + 1) // gcd(number, i + 1) | |
for i, x in enumerate(s): | |
if x == 'N' and number % (i + 1) == 0: | |
return '-1' | |
return str(number) | |
lines = stdin.read().split() | |
lines.pop() | |
out = [] | |
for s in lines: | |
out.append(the_number(s)) | |
print '\n'.join(out) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment