QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#267189#5103. Fair DivisionjuampiTL 173ms8240kbPython31.8kb2023-11-27 01:10:032023-11-27 01:10:04

Judging History

你现在查看的是最新测评结果

  • [2023-11-27 01:10:04]
  • 评测
  • 测评结果:TL
  • 用时:173ms
  • 内存:8240kb
  • [2023-11-27 01:10:03]
  • 提交

answer

# Arup Guha
# 11/10/2022
# Solution to 2021 WF Problem C: Fair Division

# Just calculates b to the e power...a speed up.
def mypow(b,e):

    res = 1
    term = b
    while e > 0:

        if e&1 == 1:
            res = res*term

        e >>= 1
        term *= term

    return res

# GCD...maybe I should just call the build in one =)
def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

# Main function.
def main():
    toks = input().split()
    n = int(toks[0])
    m = int(toks[1])

    # Solve it.
    p,q = go(n,m)

    # Here is what they ask us to output.
    if p == -1:
        print("impossible")
    else:
        print(p,q)

# Solve it!
def go(n,m):

    # The fraction just doesn't work...
    if n > 70:
        return -1,-1
    
    # I guessed, but I think about 3000 is the upper bound because there
    # are at least 6 pirates...
    for q in range(2, 6000):

        # We are trying q first and we need this value.
        term = mypow(q,n-1)
        if m < term:
            break

        # We are okay to try it, so multiply by q.
        term *= q
        
        found = False
        
        # Try p...
        for p in range(q-1, 0,-1):

            # Not allowed.
            if gcd(q,p) != 1:
                continue

            # Calculate the desired fraction.
            sub = mypow(p,n)
            div = term-sub
            top = m*(q-p)

            # Has to be an integer...
            if div > top:
                continue

            found = True

            # This is our criteria for a solution.
            if top%div == 0:
                return q-p,q

        if not found:
            break

    # If we get here, no p,q worked...
    return -1,-1

# Run it!
main()

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8068kb

input:

13 382475111752106101

output:

17 28

result:

ok single line: '17 28'

Test #2:

score: 0
Accepted
time: 0ms
memory: 8100kb

input:

59 576460752303423487

output:

1 2

result:

ok single line: '1 2'

Test #3:

score: 0
Accepted
time: 7ms
memory: 8240kb

input:

15 227368755046033249

output:

13 14

result:

ok single line: '13 14'

Test #4:

score: 0
Accepted
time: 6ms
memory: 8160kb

input:

27 72027091647987988

output:

1 4

result:

ok single line: '1 4'

Test #5:

score: 0
Accepted
time: 9ms
memory: 8044kb

input:

12 817283057828223168

output:

10 17

result:

ok single line: '10 17'

Test #6:

score: 0
Accepted
time: 10ms
memory: 8236kb

input:

40 279103330129289325

output:

1 2

result:

ok single line: '1 2'

Test #7:

score: 0
Accepted
time: 10ms
memory: 8136kb

input:

9 200754090585004509

output:

27 31

result:

ok single line: '27 31'

Test #8:

score: 0
Accepted
time: 9ms
memory: 8104kb

input:

13 145272043713167318

output:

11 19

result:

ok single line: '11 19'

Test #9:

score: 0
Accepted
time: 4ms
memory: 8196kb

input:

13 330339892079732537

output:

3 5

result:

ok single line: '3 5'

Test #10:

score: 0
Accepted
time: 8ms
memory: 8112kb

input:

8 518312274023062851

output:

35 81

result:

ok single line: '35 81'

Test #11:

score: 0
Accepted
time: 2ms
memory: 8080kb

input:

8 226575677743060500

output:

3 37

result:

ok single line: '3 37'

Test #12:

score: 0
Accepted
time: 10ms
memory: 8132kb

input:

22 947676267664323372

output:

5 6

result:

ok single line: '5 6'

Test #13:

score: 0
Accepted
time: 9ms
memory: 8128kb

input:

8 884152939068009488

output:

32 87

result:

ok single line: '32 87'

Test #14:

score: 0
Accepted
time: 9ms
memory: 8100kb

input:

10 334992255296783634

output:

1 2

result:

ok single line: '1 2'

Test #15:

score: 0
Accepted
time: 8ms
memory: 8044kb

input:

9 387165762000719100

output:

9 26

result:

ok single line: '9 26'

Test #16:

score: 0
Accepted
time: 8ms
memory: 8160kb

input:

13 966426794141592430

output:

5 23

result:

ok single line: '5 23'

Test #17:

score: 0
Accepted
time: 9ms
memory: 8068kb

input:

30 3882204456

output:

impossible

result:

ok single line: 'impossible'

Test #18:

score: 0
Accepted
time: 3ms
memory: 8132kb

input:

17 388292937745500

output:

impossible

result:

ok single line: 'impossible'

Test #19:

score: 0
Accepted
time: 173ms
memory: 8068kb

input:

7 77777777777777777

output:

impossible

result:

ok single line: 'impossible'

Test #20:

score: 0
Accepted
time: 10ms
memory: 8120kb

input:

6 1

output:

impossible

result:

ok single line: 'impossible'

Test #21:

score: 0
Accepted
time: 10ms
memory: 8156kb

input:

6 6

output:

impossible

result:

ok single line: 'impossible'

Test #22:

score: 0
Accepted
time: 0ms
memory: 8180kb

input:

6 666666666

output:

impossible

result:

ok single line: 'impossible'

Test #23:

score: -100
Time Limit Exceeded

input:

6 982698952174251648

output:


result: