CSES - Aalto Competitive Programming 2024 - wk12 - Mon - Results
Submission details
Task:GCD and LCM
Sender:aalto2024m_002
Submission time:2024-11-25 17:12:40 +0200
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.04 sdetails
#7ACCEPTED0.04 sdetails
#8ACCEPTED0.04 sdetails
#9ACCEPTED0.04 sdetails
#10ACCEPTED0.04 sdetails
#11ACCEPTED0.04 sdetails
#12ACCEPTED0.04 sdetails
#13ACCEPTED0.05 sdetails
#14ACCEPTED0.04 sdetails
#15ACCEPTED0.04 sdetails
#16ACCEPTED0.05 sdetails
#17ACCEPTED0.04 sdetails
#18ACCEPTED0.04 sdetails
#19ACCEPTED0.04 sdetails
#20ACCEPTED0.17 sdetails
#21ACCEPTED0.04 sdetails
#22ACCEPTED0.04 sdetails
#23ACCEPTED0.04 sdetails
#24ACCEPTED0.04 sdetails
#25ACCEPTED0.04 sdetails
#26ACCEPTED0.22 sdetails
#27ACCEPTED0.04 sdetails
#28ACCEPTED0.04 sdetails
#29ACCEPTED0.04 sdetails
#30ACCEPTED0.04 sdetails
#31ACCEPTED0.06 sdetails
#32ACCEPTED0.05 sdetails
#33ACCEPTED0.04 sdetails
#34ACCEPTED0.05 sdetails
#35ACCEPTED0.04 sdetails
#36ACCEPTED0.04 sdetails
#37ACCEPTED0.04 sdetails
#38ACCEPTED0.04 sdetails
#39ACCEPTED0.04 sdetails
#40ACCEPTED0.04 sdetails
#41ACCEPTED0.05 sdetails
#42ACCEPTED0.05 sdetails
#43--details
#44--details
#45--details
#46--details
#47ACCEPTED0.04 sdetails
#48ACCEPTED0.04 sdetails
#49ACCEPTED0.04 sdetails
#50ACCEPTED0.04 sdetails
#51ACCEPTED0.04 sdetails
#52ACCEPTED0.04 sdetails
#53ACCEPTED0.04 sdetails
#54ACCEPTED0.04 sdetails
#55ACCEPTED1.48 sdetails
#56ACCEPTED1.48 sdetails
#57--details
#58--details
#59--details
#60ACCEPTED1.49 sdetails

Code

import sys
import math

sys.setrecursionlimit(10**6)
def gcd(a, b):
    while b != 0:
        a, b = b, a%b
    return a


def lcm(a, b, g):
    return (a * b) // g


def subset_mul_2(x, n, ss):
    if n == 0:
        return 1, 1
    s = [1]
    d = ss - x[0]
    r = x[0]
    for i in range(n):
        v = len(s)
        for t in range(v):
            k = s[t] * x[i]
            if d >= abs(ss - k):
                d = abs(ss - k)
                r = k
            s.append(k)
    return r, s[-1]

# def subset_mul(x, n, s):
#     if n == 0:
#         return 1, 1
#     total = 1 << n
#     d = s - x[0]
#     r = x[0]
#     m = x[0]
#     for i in range(total):
#         m = 1
#         for j in range(n):
#             if (i & (1 << j)) != 0:
#                 m *= x[j]
#         if d >= abs(s - m):
#             d = abs(s - m)
#             r = m
#     print(s)
#     return r, m


def factors(n):
    f = []
    i = 2
    while i * i <= n:
        if n % i == 0:
            f.append(i)
            n //= i
            while n % i == 0:
                n //= i
                f[-1] *= i
        i += 1
    if n != 1:
        f.append(n)
    return f


a, b = map(int, input().split())
g = gcd(a, b)
l = lcm(a, b, g)
h = l // g
f = factors(h)
# q = subset_mul_2(f, len(f), int(h ** 0.5))
# print(q)
r, fn = subset_mul_2(f, len(f), int(h ** 0.5))

# print(g, l, f, r, fn)
# print(r, g * r)
x, y = g * r, g * (fn // r)
print(min(x, y), max(x, y))

Test details

Test 1

Verdict: ACCEPTED

input
3 4

correct output
3 4

user output
3 4

Test 2

Verdict: ACCEPTED

input
1 12

correct output
3 4

user output
3 4

Test 3

Verdict: ACCEPTED

input
1 1

correct output
1 1

user output
1 1

Test 4

Verdict: ACCEPTED

input
1 2

correct output
1 2

user output
1 2

Test 5

Verdict: ACCEPTED

input
2 2

correct output
2 2

user output
2 2

Test 6

Verdict: ACCEPTED

input
2 1

correct output
1 2

user output
1 2

Test 7

Verdict: ACCEPTED

input
1 6

correct output
2 3

user output
2 3

Test 8

Verdict: ACCEPTED

input
1 9

correct output
1 9

user output
1 9

Test 9

Verdict: ACCEPTED

input
6 1

correct output
2 3

user output
2 3

Test 10

Verdict: ACCEPTED

input
1 1000000000

correct output
512 1953125

user output
512 1953125

Test 11

Verdict: ACCEPTED

input
4 48

correct output
12 16

user output
12 16

Test 12

Verdict: ACCEPTED

input
2 9

correct output
2 9

user output
2 9

Test 13

Verdict: ACCEPTED

input
9 2

correct output
2 9

user output
2 9

Test 14

Verdict: ACCEPTED

input
742625819 979252649

correct output
748840261 971126071

user output
748840261 971126071

Test 15

Verdict: ACCEPTED

input
742625819 628365177

correct output
628375693 742613391

user output
628375693 742613391

Test 16

Verdict: ACCEPTED

input
59737016 594356392

correct output
84908056 418159112

user output
84908056 418159112

Test 17

Verdict: ACCEPTED

input
627952539 395203065

correct output
395203065 627952539

user output
395203065 627952539

Test 18

Verdict: ACCEPTED

input
454674271 444313814

correct output
444313814 454674271

user output
444313814 454674271

Test 19

Verdict: ACCEPTED

input
547252636 317707623

correct output
329474572 527707899

user output
329474572 527707899

Test 20

Verdict: ACCEPTED

input
848011418 576586631

correct output
576586631 848011418

user output
576586631 848011418

Test 21

Verdict: ACCEPTED

input
348776064 196069730

correct output
249125760 274497622

user output
249125760 274497622

Test 22

Verdict: ACCEPTED

input
237025447 149858613

correct output
149858613 237025447

user output
149858613 237025447

Test 23

Verdict: ACCEPTED

input
726392299 531452772

correct output
531452772 726392299

user output
531452772 726392299

Test 24

Verdict: ACCEPTED

input
59151261 839703616

correct output
186553977 266247488

user output
186553977 266247488

Test 25

Verdict: ACCEPTED

input
291928593 552857636

correct output
389238124 414643227

user output
389238124 414643227

Test 26

Verdict: ACCEPTED

input
421465106 509937555

correct output
421465106 509937555

user output
421465106 509937555

Test 27

Verdict: ACCEPTED

input
902518094 821875115

correct output
821875115 902518094

user output
821875115 902518094

Test 28

Verdict: ACCEPTED

input
301536505 988636664

correct output
482458408 617897915

user output
482458408 617897915

Test 29

Verdict: ACCEPTED

input
802229414 316587683

correct output
479117567 530091086

user output
479117567 530091086

Test 30

Verdict: ACCEPTED

input
326192670 575151518

correct output
326192670 575151518

user output
326192670 575151518

Test 31

Verdict: ACCEPTED

input
548703061 432915082

correct output
432915082 548703061

user output
432915082 548703061

Test 32

Verdict: ACCEPTED

input
969217649 547557803

correct output
547557803 969217649

user output
547557803 969217649

Test 33

Verdict: ACCEPTED

input
33451382 190448754

correct output
63482918 100354146

user output
63482918 100354146

Test 34

Verdict: ACCEPTED

input
910804372 355985662

correct output
455402186 711971324

user output
455402186 711971324

Test 35

Verdict: ACCEPTED

input
557993572 616540429

correct output
557993572 616540429

user output
557993572 616540429

Test 36

Verdict: ACCEPTED

input
999999999 1000000000

correct output
999999999 1000000000

user output
999999999 1000000000

Test 37

Verdict: ACCEPTED

input
999999761 1000000000

correct output
999999761 1000000000

user output
999999761 1000000000

Test 38

Verdict: ACCEPTED

input
1 1000000000

correct output
512 1953125

user output
512 1953125

Test 39

Verdict: ACCEPTED

input
500000000 1000000000

correct output
500000000 1000000000

user output
500000000 1000000000

Test 40

Verdict: ACCEPTED

input
200000000 1000000000

correct output
200000000 1000000000

user output
200000000 1000000000

Test 41

Verdict: ACCEPTED

input
971959530 977699359

correct output
973924458 975726815

user output
973924458 975726815

Test 42

Verdict: ACCEPTED

input
991437195 958491586

correct output
973924458 975726815

user output
973924458 975726815

Test 43

Verdict:

input
999999937 999999929

correct output
999999929 999999937

user output
(empty)

Test 44

Verdict:

input
999999751 999999757

correct output
999999751 999999757

user output
(empty)

Test 45

Verdict:

input
999999739 999999599

correct output
999999599 999999739

user output
(empty)

Test 46

Verdict:

input
999997357 999997403

correct output
999997357 999997403

user output
(empty)

Test 47

Verdict: ACCEPTED

input
999999937 2

correct output
2 999999937

user output
2 999999937

Test 48

Verdict: ACCEPTED

input
999999937 5

correct output
5 999999937

user output
5 999999937

Test 49

Verdict: ACCEPTED

input
999999986 2

correct output
2 999999986

user output
2 999999986

Test 50

Verdict: ACCEPTED

input
999999986 3

correct output
6 499999993

user output
6 499999993

Test 51

Verdict: ACCEPTED

input
999999979 2

correct output
22 90909089

user output
22 90909089

Test 52

Verdict: ACCEPTED

input
818181801 3

correct output
9 272727267

user output
9 272727267

Test 53

Verdict: ACCEPTED

input
636363623 5

correct output
35 90909089

user output
35 90909089

Test 54

Verdict: ACCEPTED

input
909090890 12

correct output
60 181818178

user output
60 181818178

Test 55

Verdict: ACCEPTED

input
999999979 999999986

correct output
999999979 999999986

user output
999999979 999999986

Test 56

Verdict: ACCEPTED

input
999999979 999999937

correct output
999999937 999999979

user output
999999937 999999979

Test 57

Verdict:

input
999999986 999999929

correct output
999999929 999999986

user output
(empty)

Test 58

Verdict:

input
999999986 999999757

correct output
999999757 999999986

user output
(empty)

Test 59

Verdict:

input
999999939 999999929

correct output
999999929 999999939

user output
(empty)

Test 60

Verdict: ACCEPTED

input
636363623 999999929

correct output
636363623 999999929

user output
636363623 999999929