UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#215160#2686. Oversleepingnodgd018ms1192kbC++111.9kb2024-11-26 21:34:492024-11-26 23:05:30

answer

#include <bits/stdc++.h>

using namespace std;

// typedef long long i64;

int exgcd(int a, int b, int &x, int &y) {
    if (!b) {
        x = 1, y = 0;
        return a;
    } else {
        int c = exgcd(b, a % b, y, x);
        y -= a / b * x;
        return c;
    }
}

int main() {
    int T;
    for (scanf("%d", &T); T --; ) {
        int X, Y, P, Q, A, B, a, b, c, x, y;
        scanf("%d%d%d%d", &X, &Y, &P, &Q);
        A = P + Q;
        B = X + Y + X + Y;
        // printf("  A=%d,B=%d\n", A, B);
        c = exgcd(A, B, x, y);
        // printf("  c=%d,x=%d,y=%d   c=A*x+B*y\n", c, x, y);
        a = A / c;
        b = B / c;
        // printf("  a=%d,b=%d\n", a, b);
        y = -y;
        if (x < 0 || y < 0) x += b, y += a;
        // printf("  x=%d,y=%d\n", x, y);
        // assert(1ll * x * A - 1ll * y * B == c);
        // assert(x >= 0 && y >= 0);
        // assert(x <= b && y <= a);
        int dl = X - P - Q + 1;
        int dr = X + Y - P - 1;
        // printf("  dl=%d, dr=%d\n", dl, dr);
        if (dl >= 0) dl = (dl + c - 1) / c;
        else dl = dl / c;
        if (dr >= 0) dr = dr / c;
        else dr = (dr - c + 1) / c;
        // printf("  dl=%d, dr=%d\n", dl, dr);
        long long ans = LONG_LONG_MAX;
        for (int d = dl; d <= dr; d ++) {
            // assert(d * c > X - P - Q);
            // assert(d * c < X + Y - P);
            int k = d * x % b;
            if (k < 0) k += b;
            int m = d * y % a;
            if (m < 0) m += a;
            // printf("    d=%d, k=%d, m=%d\n", d, k, m);
            // assert(1ll * k * A - 1ll * m * B == d * c);
            long long t = 1ll * k * A;
            if (X - P <= c * d) t = t + P;
            else t = t + c * d - X;
            ans = min(ans, t);
        }
        if (ans == LONG_LONG_MAX) ans = -1;
        printf("%lld\n", ans);
    }
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
42 337 468 315
179 261 297 332
336 189 230 33
240 398 38 204
465 158 454 462
112 184 90 263
320 4...

output:

8
199
493
-240
-465
-112
475
-492
386
-268

result:

wrong answer 1st lines differ - expected: '1558', found: '8'

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
96 471 276 491
226 87 207 365
451 201 191 173
46 375 100 8
39 179 214 269
135 260 91 431
253 288 ...

output:

276
-226
555
100
-475
-135
359
-374
378
-99

result:

wrong answer 2nd lines differ - expected: '226', found: '-226'

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 1192kb

input:

10
337 285 340 338
484 25 321 156
246 482 122 234
229 441 426 72
6 93 155 338
21 317 406 335
160 298...

output:

340
4920
-246
426
-402
-697
372
363
-437
-492

result:

wrong answer 2nd lines differ - expected: '5574', found: '4920'

Test #4:

score: 0
Wrong Answer
time: 0ms
memory: 1192kb

input:

10
196 433 313 366
262 415 94 30
353 22 242 460
283 172 13 276
56 247 344 4
297 363 415 479
473 26 6...

output:

313
342
-353
-283
692
171
-473
169
-461
176

result:

wrong answer 3rd lines differ - expected: '353', found: '-353'

Test #5:

score: 0
Wrong Answer
time: 0ms
memory: 1192kb

input:

10
437 267 140 198
383 155 226 424
273 308 151 317
15 231 435 285
404 294 253 64
109 199 388 147
425...

output:

478
-383
-273
-507
570
923
427
467
-328
-123

result:

wrong answer 2nd lines differ - expected: '383', found: '-383'

Test #6:

score: 0
Wrong Answer
time: 1ms
memory: 1188kb

input:

10
21 426 135 234
111 51 109 11
185 42 230 22
472 346 269 285
77 128 499 461
173 79 230 382
102 127 ...

output:

135
-111
1527
-472
-897
43
207
297
-268
141

result:

wrong answer 2nd lines differ - expected: '111', found: '-111'

Test #7:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
790950658 153 395731486 328
228237805 89 805489534 371
733742769 407 150980036 116
210282811 256 ...

output:

74664700006122
1842335842473785
163211959994316
147853620524959
1800153847599268
177411960126600
115...

result:

wrong answer 1st lines differ - expected: '493783472749894', found: '74664700006122'

Test #8:

score: 0
Wrong Answer
time: 0ms
memory: 1192kb

input:

10
935151576 163 823994136 316
552819682 244 661178089 95
751586616 375 326537181 182
9424664 344 31...

output:

634378496694914
1748034122888233
2890402939170394
2683171287239
622024525833383
484334150279857
5434...

result:

wrong answer 1st lines differ - expected: '3487138752902582', found: '634378496694914'

Test #9:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
480628854 293 138727635 133
188840646 190 569211831 53
442713615 341 668626439 341
527553711 268 ...

output:

128207763896891
135002828587647
804769221809359
7398402921979275
997542367362628
370824244125708
192...

result:

wrong answer 1st lines differ - expected: '237399557723152', found: '128207763896891'

Test #10:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
128375946 395 45119743 26
591869966 93 644623523 312
553967887 448 714110036 391
129391856 52 673...

output:

17281367844433
1647233070529120
2515131201194629
27050455944748
1858004616770983
556103043785992
132...

result:

wrong answer 1st lines differ - expected: '173096624515978', found: '17281367844433'

Test #11:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
416394745 108 799849287 309
285823581 120 50495267 151
984269713 448 325585187 230
502469917 398 ...

output:

1444565963307295
140909534240987
749591724119283
908507514921557
2718615450016134
25404413604177613
...

result:

wrong answer 1st lines differ - expected: '510932724030159', found: '1444565963307295'

Test #12:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
858503120 86 550141525 344
266071218 423 114926419 32
463189501 184 235629038 219
681438792 48 66...

output:

4114996263379610
142533163647580
267720076769125
1843663858403760
798181195952185
35921097723161
119...

result:

wrong answer 1st lines differ - expected: '1864662953909472', found: '4114996263379610'

Test #13:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
49615553 235 484012586 439
733063300 215 529603390 181
217670208 264 477723402 451
412727927 119 ...

output:

46480254803336
5791949079434530
266549845572502
186423633305161
3886509282534176
4128053356919000
13...

result:

wrong answer 1st lines differ - expected: '163825824623411', found: '46480254803336'

Test #14:

score: 0
Wrong Answer
time: 0ms
memory: 1188kb

input:

10
411060288 247 193494425 14
162692451 151 282497312 400
796796095 370 100991720 365
560074247 197 ...

output:

77057612376242
10793390082509
32245055874690
1311279927483781
40059565047033
713346109538214
2798214...

result:

wrong answer 1st lines differ - expected: '236839515269254', found: '77057612376242'

Test #15:

score: 0
Wrong Answer
time: 3ms
memory: 1192kb

input:

10
733526255 46680 232614188 25143
740109721 35101 664765419 65813
926232187 19192 248710599 18813
7...

output:

6495988046497
1759808328403
68422725198855
6647184754320
935836447774
231763242714
204537152449467
7...

result:

wrong answer 1st lines differ - expected: '483424517485', found: '6495988046497'

Test #16:

score: 0
Wrong Answer
time: 3ms
memory: 1192kb

input:

10
40397805 9224 854461643 50758
463970290 6807 993317097 75888
496579182 67022 428873070 56575
8340...

output:

767352171407
13792268127852
147980670950
5816121745433
2724646923354
207157555425
903298133173
70072...

result:

wrong answer 1st lines differ - expected: '2384944063443', found: '767352171407'

Test #17:

score: 0
Wrong Answer
time: 2ms
memory: 1192kb

input:

10
677231818 45638 135553472 57886
334073954 74682 504512612 32658
528899984 14506 658263816 15374
1...

output:

1584889883060
289104407052
42688747176936
166318096296
306050405492
3838452758472
445098708670
29578...

result:

wrong answer 1st lines differ - expected: '11785305009104', found: '1584889883060'

Test #18:

score: 0
Wrong Answer
time: 3ms
memory: 1192kb

input:

10
591761203 22205 53945697 65976
829627579 78277 112598137 27980
144133461 55044 668263637 96424
30...

output:

514461241433
70503950659
665018164271
9389264420520
3042608781353
26738055849755
910518333443
261024...

result:

wrong answer 1st lines differ - expected: '408922312723', found: '514461241433'

Test #19:

score: 0
Wrong Answer
time: 3ms
memory: 1192kb

input:

10
798158737 58985 625637387 56023
950567276 86709 613154552 23440
2676550 96868 636525260 84971
205...

output:

18804589688117
2939575270208
1909879494
7920914678423
12546119311246
4058994503266
2672836003750
330...

result:

wrong answer 1st lines differ - expected: '1279542967427', found: '18804589688117'

Test #20:

score: 0
Wrong Answer
time: 3ms
memory: 1192kb

input:

10
76769987 47758 832950079 87350
226318103 39942 857729326 28604
700759579 85249 4088534 87174
7300...

output:

1411165317376
148392093286
37527106941
2212225165742
23737849798003
12562895764519
821415012155
1614...

result:

wrong answer 1st lines differ - expected: '845532903085', found: '1411165317376'