QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#179790#1220. 屠龙勇士Sorting100 ✓382ms11864kbC++204.0kb2023-09-15 08:45:142023-09-15 08:45:15

Judging History

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

  • [2023-09-15 08:45:15]
  • 评测
  • 测评结果:100
  • 用时:382ms
  • 内存:11864kb
  • [2023-09-15 08:45:14]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

const int MAXN = 1e5 + 7 ;

int n ;
ll a[ MAXN ] , p[ MAXN ] , dmg[ MAXN ] ;
ll nw_sword[ MAXN ] ;

ll fst[ MAXN ] ;

ll get_lcm ( ll x , ll y ) {
    ll aux = __gcd ( x , y ) ;
    x /= aux ;
    return x * y ;
}

void printout ( __int128 wtf ) {
    vector < int > v ;
    bool neg = false ;
    if ( wtf < 0 ) { neg = true ; wtf = -wtf ; }
    while ( wtf > 0 ) {
        v.push_back ( wtf % 10 ) ;
        wtf /= 10 ;
    }
    reverse ( v.begin ( ) , v.end ( ) ) ;
    if ( v.empty ( ) == true ) { v.push_back ( 0 ) ; }
    if ( neg == true ) { cout << "-" ; }
    for ( auto x : v ) { cout << x ; }
    cout << "\n" ;
}

pair < __int128 , __int128 > ext_gcd ( __int128 x , __int128 y ) {
    pair < __int128 , __int128 > aux1 = { 1 , 0 } , aux2 = { 0 , 1 } ;
    while ( 1 ) {
        if ( x < y ) { swap ( x , y ) ; swap ( aux1 , aux2 ) ; }
        if ( y == 0 ) { break ; }
        __int128 cnt = x / y ;
        aux1.fi -= cnt * aux2.fi , aux1.se -= cnt * aux2.se ;
        x -= cnt * y ;
    }
    return aux1 ;
}

pair < ll , ll > merge ( ll st1 , ll per1 , ll st2 , ll per2 ) {
    ll foo = __gcd ( per1 , per2 ) ;
    if ( ( st1 % foo ) != ( st2 % foo ) ) { return { -1 , -1 } ; }
    if ( st1 > st2 ) { swap ( st1 , st2 ) ; swap ( per1 , per2 ) ; }
    auto [ hh1 , hh2 ] = ext_gcd ( per1 , per2 ) ;
    __int128 ch1 = ( per2 / foo ) , ch2 = ( per1 / foo ) ;
    __int128 coef = hh1 * ( st2 - st1 ) / foo ;
    if ( coef > 0 ) { coef %= ch1 ; }
    else {
        __int128 rm = ( -coef + ch1 - 1 ) / ch1 ;
        coef += rm * ch1 ;
    }
    __int128 cand = coef * per1 + st1 ;
    return { cand , get_lcm ( per1 , per2 ) } ;
}

void solve ( ) {
    int foo ;
    cin >> n >> foo ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ] ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> p[ i ] ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> nw_sword[ i ] ;
    }
    multiset < ll > s ;
    for ( int i = 1 ; i <= foo ; ++ i ) {
        ll x ; cin >> x ;
        s.insert ( x ) ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        auto it = s.upper_bound ( a[ i ] ) ;
        if ( it != s.begin ( ) ) { -- it ; }
        dmg[ i ] = *it ;
        s.erase ( it ) ;
        s.insert ( nw_sword[ i ] ) ;
    }
    ll mn_ops = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        mn_ops = max ( mn_ops , ( a[ i ] + dmg[ i ] - 1 ) / dmg[ i ] ) ;
        a[ i ] %= p[ i ] ;
        ll aux = __gcd ( p[ i ] , dmg[ i ] ) ;
        if ( ( a[ i ] % aux ) != 0 ) {
            cout << "-1\n" ;
            return ;
        }
        p[ i ] /= aux , dmg[ i ] /= aux , a[ i ] /= aux ;
    }
    ll ori = -1 , per = -1 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        __int128 aux = a[ i ] ;
        auto [ hh1 , hh2 ] = ext_gcd ( dmg[ i ] , p[ i ] ) ;
        if ( hh1 > 0 ) { hh1 %= p[ i ] ; }
        else {
            hh1 = ( ( -hh1 ) % p[ i ] ) ;
            hh1 = ( p[ i ] - hh1 ) % p[ i ] ;
        }
        aux = ( aux * hh1 ) % p[ i ] ;
        if ( ori == -1 ) {
            ori = aux , per = p[ i ] ;
        }
        else {
            auto [ ret1 , ret2 ] = merge ( ori , per , aux , p[ i ] ) ;
            if ( ret1 == -1 ) {
                cout << "-1\n" ;
                return ;
            }
            ori = ret1 , per = ret2 ;
        }
    }
    if ( mn_ops <= ori ) { cout << ori << "\n" ; }
    else {
        ll diff = mn_ops - ori ;
        diff = ( diff + per - 1 ) / per ;
        cout << ori + diff * per << "\n" ;
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; cin >> t ; 
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 90ms
memory: 6640kb

input:

5
100000 1
5189 4056 5605 4954 20 3670 5827 1196 5570 1803 6355 1957 2366 564 5137 192 6416 1596 764 4879 1673 6462 390 5763 5999 3382 702 6341 5282 6201 5463 6411 5122 2979 4286 1451 6508 311 4628 5933 4116 1745 45 3694 5272 4101 5294 1611 821 2035 1599 1414 296 4431 4933 4287 6262 3779 5353 5596 3...

output:

6655
6509
5621
4755
4079

result:

ok 5 lines

Test #2:

score: 5
Accepted
time: 89ms
memory: 6648kb

input:

5
100000 1
771 11 1127 1381 4565 3880 3730 6201 7607 5645 7942 6411 2741 1721 5341 6739 1445 7601 579 2433 6232 3935 4730 2518 3889 1707 1196 3948 2431 5178 6543 2961 3813 7139 280 438 5315 4994 5622 3082 1855 1334 4069 6974 7626 7290 4934 4958 6344 6596 153 2708 8029 6789 624 3356 5981 1727 726 323...

output:

8140
7924
6731
5644
5463

result:

ok 5 lines

Test #3:

score: 5
Accepted
time: 97ms
memory: 6668kb

input:

5
100000 1
493 547 1923 5289 1006 341 2786 315 3357 3905 2187 3529 7861 3852 7751 5330 3003 1320 2049 1769 2065 5678 425 3293 1921 434 7410 5492 2687 4759 7529 5853 4350 3554 5596 7502 2535 1855 7260 1948 7719 2367 4788 4347 7930 6194 1033 615 7450 4746 2562 6409 4394 341 576 796 5281 3129 2988 1188...

output:

5009
5461
3033
5584
3170

result:

ok 5 lines

Test #4:

score: 5
Accepted
time: 107ms
memory: 7080kb

input:

5
100000 1
341 1121 104 130 2088 8832 6172 5005 1743 2892 5685 4108 621 4286 1826 4856 8645 5918 5353 3976 7664 8032 266 788 4969 195 2953 8840 4564 3552 2174 6051 4762 5799 8135 3085 8879 5896 5222 7090 7801 1800 4462 8307 7108 1736 6739 2336 3693 7101 6971 3265 2754 8451 1803 1480 2613 5145 570 46...

output:

2899
2134
4879
4066
1754

result:

ok 5 lines

Test #5:

score: 5
Accepted
time: 1ms
memory: 5584kb

input:

5
1000 1000
4 25 4 23076 13786 8288 37280 72 27396 28288 16 4 7 48 2284 29024 104 12 29 12 28 3928 48364 7270 7 17804 112 5 40 6952 4340 8754 30448 54848 80 2247 12 5 2404 8401 2 60 11348 6152 164 11886 204 1780 34 5328 411 220 20 38 10544 15 51660 2756 41324 56 36 96 30262 4012 18882 30 20 340 8 8 ...

output:

761092
842372
922557
931619
861853

result:

ok 5 lines

Test #6:

score: 5
Accepted
time: 4ms
memory: 5668kb

input:

5
1000 1000
6 11469 6 318 4546 60148 66 3455 4389 9 138 12 15615 27 959 35 326 58 5025 6957 9 137 546 27 995 54462 4530 3858 4536 10641 60 7581 81 5748 10720 19764 19318 54 4 2124 2733 2080 9192 24850 9 2 250 21 75 51 2329 105 520 117 100 48 10 4 75 24507 10689 81 35 18559 177 845 18 3 63 163 1056 1...

output:

740067
875101
794460
872933
760438

result:

ok 5 lines

Test #7:

score: 5
Accepted
time: 4ms
memory: 5664kb

input:

5
1000 1000
8 44 2 88 3 3 4426 3 10 42744 3626 52 11914 142 4 20 60 14688 28040 17944 22012 2 92 40 18 6 57364 36788 232 12 132 56 17 10026 3824 3 3940 4812 26680 15484 10 17612 8 50808 73 88 16 28 8 3952 26844 1 90 16 9 64 2 16861 6392 53832 40 12586 3 8 5474 9004 6 1890 1 34724 4 30 26 5592 10 201...

output:

794396
776944
763888
875015
781872

result:

ok 5 lines

Test #8:

score: 5
Accepted
time: 1ms
memory: 5496kb

input:

5
1 1
1249378
99048079
1
801642
1 1
70666110
99056313
1
458586
1 1
61157145
81882641
1
647668
1 1
23532264
78789408
1
170372
1 1
59274385
99984865
1
829644

output:

72463170
6894995
50845317
18971898
82562630

result:

ok 5 lines

Test #9:

score: 5
Accepted
time: 0ms
memory: 5552kb

input:

5
1 1
17904094
99092902
1
119681
1 1
36484496
99896398
1
746110
1 1
4765223
87751171
1
629031
1 1
85241526
91041504
1
131107
1 1
45056037
99090357
1
177585

output:

20567054
14053507
43267852
13220754
30387740

result:

ok 5 lines

Test #10:

score: 5
Accepted
time: 1ms
memory: 5592kb

input:

5
1 1
49632512
99539840
1
886346
1 1
78328552
99820417
1
668799
1 1
26055638
86199301
1
531042
1 1
8968967
84287712
1
410909
1 1
26587288
99182928
1
602251

output:

43220992
98593971
51603213
51143635
3374152

result:

ok 5 lines

Test #11:

score: 5
Accepted
time: 1ms
memory: 5556kb

input:

5
1 1
73431777
99936884
1
570581
1 1
34612234
99831691
1
599139
1 1
34198644
80394179
1
218704
1 1
13500288
76801056
1
211116
1 1
42573845
99926689
1
828565

output:

13437761
33613038
50123742
5454672
26358849

result:

ok 5 lines

Test #12:

score: 5
Accepted
time: 1ms
memory: 5528kb

input:

5
1 1
75853293
99036838
1
428047
1 1
7818163
99350536
1
832005
1 1
40845420
97951681
1
255701
1 1
55215650
83068896
1
181207
1 1
21544076
99859384
1
371731

output:

67405855
32067471
46323807
77922062
87390820

result:

ok 5 lines

Test #13:

score: 5
Accepted
time: 2ms
memory: 5568kb

input:

5
1 1
72407888
99647008
1
964632
1 1
78775996
99166064
1
306638
1 1
49010001
97268177
1
267265
1 1
50714172
85565856
1
39309
1 1
72777420
99864180
1
389910

output:

6282814
30695562
71900715
14407020
1991016

result:

ok 5 lines

Test #14:

score: 5
Accepted
time: 378ms
memory: 11332kb

input:

5
100000 100000
77177366 46762434 47354301 18368057 59540793 34115484 38055445 737866 18944147 61262888 65574806 23550314 57144755 78088985 56200734 75632874 45029080 41326111 7224753 13831837 20650404 64453296 47812189 38675074 46280317 49435169 20773297 46171668 71873403 30447076 47571002 3981511 ...

output:

181
161
158
176
162

result:

ok 5 lines

Test #15:

score: 5
Accepted
time: 382ms
memory: 11328kb

input:

5
100000 100000
50325083 18321821 55084357 3471965 62354431 14101368 65254206 59663617 6785760 70220146 21301482 13915186 43043389 69904779 28091958 31473896 2272617 40260359 10692290 58122569 44894092 1825080 22946243 23335890 27462801 25534699 66592843 54511734 31970494 53631305 21925922 53845809 ...

output:

181
193
159
173
200

result:

ok 5 lines

Test #16:

score: 5
Accepted
time: 373ms
memory: 11360kb

input:

5
100000 100000
62 37336765 30242768 16926534 12729548 34 5044532 53 52 31926885 51 7 18677176 53 35 52240432 40 71722056 10949849 17 71 53922450 47 21 42 68 45650718 60284379 34388567 36112626 78 46259414 66 78 63 20 62 70062423 30661432 78 5765385 48 14 38832027 7 46219457 10207923 34298465 682895...

output:

487472861809
3871865111
7560798679
584853762636
670310334583

result:

ok 5 lines

Test #17:

score: 5
Accepted
time: 351ms
memory: 11488kb

input:

5
100000 100000
63 6872184 73048870 80080294 63 67363090 71 92 72 77924665 49 76 33 43653093 53 42 42505538 39442903 11214653 81 62242278 84 14308712 76804923 70 50523588 57 36 12632269 82 86 64 72 22203825 76 37233555 28443111 68983684 1268788 57 71 35 59398333 69173709 63 4672396 6848195 76 190620...

output:

600797388119
566101514493
587540018
634174803650
552280363387

result:

ok 5 lines

Test #18:

score: 5
Accepted
time: 368ms
memory: 11864kb

input:

5
100000 100000
13267728397 6930065080 11822984059 5 13333680640 13 195 12259373581 1 70 140 42889654017 40756370655 20 10 10518012100 100 20 12 70 22829869610 17786404294 65 4667265343 25 123386330555 1 90 2 2357519480 10 30 848253425 1 1701531557 233151675015 104107358000 11609750778 17930936247 1...

output:

824449922225
751730822926
891096169450
963861894691
902706051916

result:

ok 5 lines

Test #19:

score: 5
Accepted
time: 376ms
memory: 11340kb

input:

5
100000 100000
76 16523038041 38270908511 3277440779 55 68159829433 2 4 306648451 5166718818 3 4 2 35 5 81 31372053306 21 76123260264 9 11 145816805177 14 17 5 4 44840038075 4175916625 3 24 1903150201 28935559998 12274698105 32 10261508810 173638357 60544742197 259671060305 64 20757163417 14 2 2 14...

output:

795995657507
748640976198
920397318025
822349094004
799930587126

result:

ok 5 lines

Test #20:

score: 5
Accepted
time: 369ms
memory: 11360kb

input:

5
100000 100000
275 90 36 8372954616 5731965129 15 144806129205 8571465015 8588056782 45 9164768770 5 25 2609058267 3276662820 270 20 86137617585 9 14484074790 3 55 345 9746540895 50421333448 3 1226440407 360 5 6 10 15527522832 4363886307 1201478170 212159120 1504099884 45 90 15 360 420 63832205850 ...

output:

827477774355
809494382833
785779359157
798956553961
880255400578

result:

ok 5 lines

Extra Test:

score: 0
Extra Test Passed