QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#14895#1451. Bus ConnectionsQingyuAC ✓45ms8480kbPython31.4kb2021-10-25 10:38:252022-05-17 01:13:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-17 01:13:35]
  • 评测
  • 测评结果:AC
  • 用时:45ms
  • 内存:8480kb
  • [2021-10-25 10:38:25]
  • 提交

answer

def extended_gcd(a, b):
    """returns gcd(a, b), s, r s.t. a * s + b * r == gcd(a, b)"""
    s, old_s = 0, 1
    r, old_r = b, a
    while r:
        q = old_r // r
        old_r, r = r, old_r - q * r
        old_s, s = s, old_s - q * s
    return old_r, old_s, (old_r - old_s * a) // b if b else 0


def crt(b, m):
    """returns x s.t. x = b[i] (mod m[i]) for all i"""
    x, m_prod = 0, 1
    for bi, mi in zip(b, m):
        g, s, _ = extended_gcd(m_prod, mi)
        if ((bi - x) % mi) % g:
            return (None, None)
        x += m_prod * (s * ((bi - x) % mi) // g)
        m_prod = (m_prod * mi) // g
    return (x % m_prod, m_prod)


b = int(input())
mx = 0
sum_d = 0
q = 1010;
x = [0]*q
vis = [False]*q
bad = False

for i in range(b):
    t,m,d = [int(x) for x in input().split()]
    t -= sum_d
    if t > mx:
        mx = t
    t = (t % m + m) % m
    if not vis[m]:
        vis[m] = True
        x[m] = t
    if vis[m] and x[m] != t:
        bad = True
    sum_d += d

if bad:
    print("-1")
else:
    j = 0
    for v in vis:
        if v:
            j += 1

    xx = [0]*j
    mm = [0]*j
    j = 0
    for i in range(len(vis)):
        if vis[i]:
            xx[j] = x[i]
            mm[j] = i
            j += 1


    (ans, mod) = crt(xx,mm)
    if ans is None:
        print("-1")
    else:
        q = (mx-ans) // mod
        ans += q*mod
        while ans > mx:
            ans -= mod
        while ans < mx:
            ans += mod
        print(ans)

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 20ms
memory: 8436kb

input:

3
101 5 100
100000 7 200
0 4 300

output:

99956

result:

ok answer is '99956'

Test #2:

score: 0
Accepted
time: 17ms
memory: 8308kb

input:

2
0 6 9
0 10 9

output:

-1

result:

ok answer is '-1'

Test #3:

score: 0
Accepted
time: 23ms
memory: 8480kb

input:

8
122 997 491
808 991 290
172 983 560
928 977 101
570 971 592
357 967 123
429 953 835
199 947 295

output:

526173665553865384027818

result:

ok answer is '526173665553865384027818'

Test #4:

score: 0
Accepted
time: 25ms
memory: 8384kb

input:

1000
0 1 1
2 2 1
4 3 1
6 4 1
8 5 1
10 6 1
12 7 1
14 8 1
16 9 1
18 10 1
20 11 1
22 12 1
24 13 1
26 14 1
28 15 1
30 16 1
32 17 1
34 18 1
36 19 1
38 20 1
40 21 1
42 22 1
44 23 1
46 24 1
48 25 1
50 26 1
52 27 1
54 28 1
56 29 1
58 30 1
60 31 1
62 32 1
64 33 1
66 34 1
68 35 1
70 36 1
72 37 1
74 38 1
76 39...

output:

712886527466509305316638415571427292066835886188589304045200199115432408758111149947644415191387158691171781701957525651298026406762100925146587100430513107268626814320019660997486274593718834370501543445252373974529896314567498212823695623282379401106880926231770886197954079124775455804932647573782...

result:

ok answer is '712886527466509305316638415571...0891687487672697950931603519999'

Test #5:

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

input:

1
156957 587 168

output:

156957

result:

ok answer is '156957'

Test #6:

score: 0
Accepted
time: 14ms
memory: 8312kb

input:

2
89314 125 27
256664 449 935

output:

278189

result:

ok answer is '278189'

Test #7:

score: 0
Accepted
time: 18ms
memory: 8300kb

input:

5
908635 956 793
349235 479 881
163141 260 510
616963 622 990
111432 949 55

output:

422888801147

result:

ok answer is '422888801147'

Test #8:

score: 0
Accepted
time: 19ms
memory: 8368kb

input:

10
141480 288 747
486435 894 712
87279 346 448
342891 368 688
330267 912 413
347554 415 541
47610 93 629
23358 86 716
342940 355 207
127445 832 472

output:

394841999014713576

result:

ok answer is '394841999014713576'

Test #9:

score: 0
Accepted
time: 29ms
memory: 8372kb

input:

100
400988 414 970
374528 457 805
73545 86 825
438190 752 68
505814 583 521
600203 698 656
265555 732 808
495569 735 651
388518 994 263
50101 132 291
289770 842 195
133981 282 899
253638 744 418
89632 744 773
27219 705 432
81297 938 61
132667 675 699
404361 530 183
25564 888 333
10825 792 908
393525...

output:

902539236883832658836947782924937373518585895688919656233547002175206588302324064985977369606

result:

ok answer is '902539236883832658836947782924...2175206588302324064985977369606'

Test #10:

score: 0
Accepted
time: 15ms
memory: 8384kb

input:

999
92016 223 512
87675 140 725
23305 115 782
5980 42 577
681038 867 949
60388 76 719
14912 85 75
427691 973 316
143886 564 302
134120 756 804
20012 168 558
35766 59 452
32650 492 154
129848 230 629
113818 149 854
462560 871 403
396142 928 860
276623 377 990
730206 843 386
114142 994 959
433561 816 ...

output:

605381916603408451678890381312120433165717133672823217882440978237372544784538280394796770882873715435501263020690662878913154367571509750344435582023801583501845281350030076195421727775292706474723261113773116083073313210884982543760913396009161713329620710693752606339563107671969800885682927916530...

result:

ok answer is '605381916603408451678890381312...8497788748824512092521386923203'

Test #11:

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

input:

1000
612763 830 297
42990 248 911
80786 371 370
342529 798 170
870685 886 833
656147 827 158
372296 628 184
610028 924 584
939914 991 603
20814 37 360
33493 510 186
65242 179 670
224659 616 518
7762 219 482
1994 65 321
166224 171 515
166259 542 450
45950 855 963
415286 446 609
696629 926 968
335257 ...

output:

643104769302475344230275935970762711419528839436995274373686243061653811686483006578598764761744049235820775508514122791701241530041954137679591680608581939868779714558935788960921854282289594075558355653565483989609375315507870858197948016337189591771966477336591006942785976882675511441412276234849...

result:

ok answer is '643104769302475344230275935970...4549181033027029835883207991613'

Test #12:

score: 0
Accepted
time: 15ms
memory: 8296kb

input:

1
285049 292 730

output:

285049

result:

ok answer is '285049'

Test #13:

score: 0
Accepted
time: 13ms
memory: 8256kb

input:

2
635239 876 154
12885 247 613

output:

705319

result:

ok answer is '705319'

Test #14:

score: 0
Accepted
time: 23ms
memory: 8476kb

input:

5
533543 820 530
375915 881 473
166219 168 738
166858 287 590
38354 972 695

output:

-1

result:

ok answer is '-1'

Test #15:

score: 0
Accepted
time: 19ms
memory: 8296kb

input:

10
534507 559 824
51177 154 733
364858 394 222
111747 713 104
514836 975 631
21526 183 452
24471 41 697
281026 945 379
308918 999 852
1083 314 165

output:

93739935847201659703

result:

ok answer is '93739935847201659703'

Test #16:

score: 0
Accepted
time: 20ms
memory: 8368kb

input:

100
199933 645 634
919463 927 86
76903 440 29
12516 16 120
185212 400 132
44843 863 827
72671 125 333
6218 133 528
299688 608 433
420957 981 452
10562 23 5
267372 610 212
556928 838 399
115388 361 160
264553 461 476
570507 897 685
262929 527 392
389206 896 920
196076 831 93
538179 632 713
547315 969...

output:

-1

result:

ok answer is '-1'

Test #17:

score: 0
Accepted
time: 23ms
memory: 8348kb

input:

999
381916 707 39
341014 703 584
78865 278 173
54640 88 44
247842 291 829
173425 689 356
103781 414 176
303837 512 966
12711 28 520
301645 410 1000
44261 106 809
173300 572 790
46152 929 249
42753 73 410
52109 456 135
65785 67 568
10044 276 25
357235 894 824
205965 285 674
61607 99 966
94455 305 811...

output:

-1

result:

ok answer is '-1'

Test #18:

score: 0
Accepted
time: 37ms
memory: 8320kb

input:

1000
329501 817 374
195119 529 174
592572 795 101
67893 817 211
109264 125 179
165128 714 261
173972 337 781
181908 262 123
100395 322 222
457315 688 689
59801 423 998
307982 580 335
378157 794 933
66430 140 641
273119 348 505
398836 524 878
53350 131 47
411821 440 916
324847 910 551
339838 995 436
...

output:

-1

result:

ok answer is '-1'

Test #19:

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

input:

1
0 7 322

output:

0

result:

ok answer is '0'

Test #20:

score: 0
Accepted
time: 18ms
memory: 8360kb

input:

1
1000000 94 45

output:

1000000

result:

ok answer is '1000000'

Test #21:

score: 0
Accepted
time: 18ms
memory: 8436kb

input:

2
1000000 738 119
1000000 574 740

output:

-1

result:

ok answer is '-1'

Test #22:

score: 0
Accepted
time: 11ms
memory: 8432kb

input:

5
0 491 183
0 775 719
0 88 179
0 420 308
0 575 845

output:

-1

result:

ok answer is '-1'

Test #23:

score: 0
Accepted
time: 16ms
memory: 8296kb

input:

5
380193 723 193
815289 959 623
766463 800 465
363982 858 220
301526 521 177

output:

-1

result:

ok answer is '-1'

Test #24:

score: 0
Accepted
time: 25ms
memory: 8300kb

input:

1000
0 418 333
0 135 446
0 831 526
0 569 467
0 940 653
0 657 889
0 965 55
0 464 820
0 1000 582
0 535 263
0 948 539
0 105 525
0 934 485
0 500 579
0 605 555
0 257 220
0 584 246
0 321 620
0 694 155
0 627 625
0 462 903
0 627 895
0 512 699
0 612 411
0 467 483
0 345 522
0 912 105
0 321 610
0 911 478
0 777...

output:

-1

result:

ok answer is '-1'

Test #25:

score: 0
Accepted
time: 25ms
memory: 8444kb

input:

1000
1000000 215 416
1000000 361 878
1000000 50 52
1000000 412 103
1000000 700 316
1000000 947 123
1000000 398 300
1000000 182 988
1000000 86 392
1000000 315 649
1000000 225 423
1000000 891 651
1000000 545 195
1000000 397 436
1000000 586 932
1000000 153 697
1000000 169 990
1000000 39 530
1000000 198...

output:

-1

result:

ok answer is '-1'

Test #26:

score: 0
Accepted
time: 45ms
memory: 8296kb

input:

1000
961792 491 728
824378 702 970
647023 949 816
248997 223 28
271058 168 419
536188 939 457
50943 953 38
858986 380 741
63633 327 70
330460 220 167
374965 790 441
993924 985 90
149982 27 806
63572 917 898
267601 800 902
260976 673 570
769314 505 748
381702 36 621
11374 880 842
882827 727 490
22128...

output:

-1

result:

ok answer is '-1'