QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#250115#7620. Yet Another Subsequence Problemucup-team2307TL 349ms3828kbC++202.8kb2023-11-12 21:29:182023-11-12 21:29:19

Judging History

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

  • [2023-11-12 21:29:19]
  • 评测
  • 测评结果:TL
  • 用时:349ms
  • 内存:3828kb
  • [2023-11-12 21:29:18]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll

const int MOD=998244353;

#if 1
using row=array<int,3>;
using matr=array<row,3>;
const matr ONE={
        row{1,0,0},
        row{0,1,0},
        row{0,0,1}
};
matr mul(matr l,matr r)
{
    matr res{
            row{(l[0][0]*r[0][0]+l[0][1]*r[1][0]+l[0][2]*r[2][0])%MOD,(l[0][0]*r[0][1]+l[0][1]*r[1][1]+l[0][2]*r[2][1])%MOD,(l[0][0]*r[0][2]+l[0][1]*r[1][2]+l[0][2]*r[2][2])%MOD},
            row{(l[1][0]*r[0][0]+l[1][1]*r[1][0]+l[1][2]*r[2][0])%MOD,(l[1][0]*r[0][1]+l[1][1]*r[1][1]+l[1][2]*r[2][1])%MOD,(l[1][0]*r[0][2]+l[1][1]*r[1][2]+l[1][2]*r[2][2])%MOD},
            row{(l[2][0]*r[0][0]+l[2][1]*r[1][0]+l[2][2]*r[2][0])%MOD,(l[2][0]*r[0][1]+l[2][1]*r[1][1]+l[2][2]*r[2][1])%MOD,(l[2][0]*r[0][2]+l[2][1]*r[1][2]+l[2][2]*r[2][2])%MOD}
    };
    return res;
}
const matr X={
        row{1,0,0},
        row{1,1,0},
        row{1,0,1}
};
const matr Y={
        row{1,1,0},
        row{0,1,0},
        row{0,1,1}
};
const matr invX={
        row{1,0,0},
        row{MOD-1,1,0},
        row{MOD-1,0,1}
};
const matr invY={
        row{1,MOD-1,0},
        row{0,1,0},
        row{0,MOD-1,1}
};
auto ret(matr res)
{
    return (res[2][0]+res[2][1]+1)%MOD;
}
#else
using matr=string;
const matr ONE;
matr mul(const matr& l,const matr& r)
{
    return l+r;
}
const matr X="1";
const matr Y="0";
auto ret(matr res)
{
    return res;
}
#endif

matr pow(matr x,int y)
{
    matr res=ONE;
    while(y)
    {
        if(y&1)
            res=mul(res,x);
        y>>=1;
        x=mul(x,x);
    }
    return res;
}

matr rec(int a, int b, matr x, matr y)
{
    while(a>1||b>1)
    {
//        cout << a << " " << b << " " << x << " " << y << "\n";
        matr xx=mul(pow(x, b / a - 1),y);
        matr yy=mul(x,xx);
        tie(a, b, y, x)=tuple(a - b % a, a, xx, yy);
    }
    return y;
}

auto solve(int a,int b)
{
    int g=__gcd(a,b);
    bool sw=a>b;
    if(sw)
        swap(a,b);
    matr res=rec(a/g,b/g,Y,mul(X,Y));
    if(!sw)
    {
        res=mul(res,invY);
        res=mul(res,invX);
        res=mul(res,Y);
        res=mul(res,X);
    }
    res=pow(res,g);
    return ret(res);
}

std::string gen_string(int64_t a, int64_t b) {
    std::string res;
    int64_t ia = 0, ib = 0;
    while (ia + ib < a + b) {
        if ((__int128)ia * b <= (__int128)ib * a) {
            res += '0';
            ia++;
        } else {
            res += '1';
            ib++;
        }
    }
    reverse(res.begin(),res.end());
    return res;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
//        cout<<gen_string(a,b)<<"\n";
        cout<<solve(a,b)<<"\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3772kb

input:

6
1 1
3 5
4 7
8 20
4 10
27 21

output:

4
70
264
196417
609
667131122

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 290ms
memory: 3612kb

input:

18
5 9
23 30
820 483
5739 9232
86494 55350
606 13336
2768848 1124639
47995594 66053082
1069395 7177
7801842511 4390103762
47882886553 82678306054
193410894 6189355686
51594638 19992922190
59 110
422735115778072 658356435030265
9125338158530266 5328357177709583
60743352262021049 95595862538791630
629...

output:

988
220693002
133474535
202371605
778839228
282057418
935955056
943144752
409056617
627433544
578769776
917438628
24364208
109943645
352575425
68058533
402004723
894026897

result:

ok 18 numbers

Test #3:

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

input:

9
7 6
4 7
1 1
1 1
2 1
8 5
4 7
4 7
8 8

output:

986
264
4
4
7
781
264
264
4180

result:

ok 9 numbers

Test #4:

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

input:

7
10 10
9 5
5 9
5 8
7 4
5 9
4 8

output:

28656
1100
988
702
294
988
361

result:

ok 7 numbers

Test #5:

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

input:

10
4 7
5 9
9 5
5 9
7 4
9 9
3 10
7 4
9 5
6 4

output:

264
988
1100
988
294
10945
253
294
1100
207

result:

ok 10 numbers

Test #6:

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

input:

9
77 47
65 37
96 59
74 45
96 61
66 47
53 73
41 72
47 64

output:

477691493
223541570
510016172
616252466
634241188
463620644
498542012
162791920
984013116

result:

ok 9 numbers

Test #7:

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

input:

8
41 69
86 51
44 88
47 66
71 44
47 77
44 44
9 17

output:

856129900
783572707
168948980
519731719
796410895
739386210
133340520
191860

result:

ok 8 numbers

Test #8:

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

input:

9
47 73
99 15
52 73
29 52
82 53
49 78
71 40
50 87
84 53

output:

769345491
690191730
757588716
658697918
61904949
629708093
220994184
297434348
28621139

result:

ok 9 numbers

Test #9:

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

input:

7
661 462
386 656
797 510
19 2
685 406
994 984
86 165

output:

108717767
243878739
146514460
348
433968238
266547073
518079889

result:

ok 7 numbers

Test #10:

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

input:

7
640 391
187 37
185 77
758 530
690 708
614 422
745 596

output:

568664902
101789251
757511817
887691770
177453450
32893736
437096982

result:

ok 7 numbers

Test #11:

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

input:

8
813 469
744 999
637 437
394 303
773 773
380 643
38 9
703 426

output:

454035451
338288113
916487884
221556201
179854568
52657041
26618143
962558445

result:

ok 8 numbers

Test #12:

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

input:

8
6855 4213
4112 6914
5778 2583
4988 8449
8090 8090
6519 4733
4434 6958
2 3

output:

427280974
44013214
375314206
144262561
427379094
372013990
839270931
18

result:

ok 8 numbers

Test #13:

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

input:

8
8409 5416
8107 4913
95 98
115 3
5013 7048
9328 5735
1680 5712
5197 7557

output:

894035672
40190853
343168705
190160
390361879
183351154
606244444
134913462

result:

ok 8 numbers

Test #14:

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

input:

9
5182 8121
3750 6458
8234 8234
4143 7423
4020 1789
4107 7290
6723 3999
4268 6070
7780 3890

output:

866885729
688535194
823578849
212358139
362917931
67863647
455387876
758964821
193741943

result:

ok 9 numbers

Test #15:

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

input:

7
61155 95200
45410 29617
52271 72405
65732 48477
24291 69219
67575 35139
79612 44714

output:

568124700
703486037
744399547
157090122
340253631
764313623
405183638

result:

ok 7 numbers

Test #16:

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

input:

9
58285 95000
61322 43451
62444 98391
63929 37316
11880 85320
55132 89028
46015 74851
95036 57832
97346 57569

output:

198194769
642221077
706321582
889996800
236557792
438005278
854653054
551428347
412917166

result:

ok 9 numbers

Test #17:

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

input:

9
70070 51175
1 3
97696 89440
76449 76449
39928 68508
53907 53907
48569 83915
64151 44772
423 4

output:

386957046
8
333547672
669126229
622003028
967849140
694422542
68403374
399271894

result:

ok 9 numbers

Test #18:

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

input:

7
791062 495727
446934 701097
575527 973782
601232 973330
1293 33622
654288 790598
582881 422021

output:

787234183
89769894
656957068
144699935
985638
436404539
589184034

result:

ok 7 numbers

Test #19:

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

input:

9
170761 370325
835313 538289
557394 885834
4 1370
457105 725030
170133 373897
606675 954589
478770 675786
529800 529800

output:

938892037
351941869
451634146
177505605
507054806
847951838
454269997
734099694
483445163

result:

ok 9 numbers

Test #20:

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

input:

9
971470 571018
729856 522877
712674 782955
527307 855805
1991 6
646650 479073
664436 461056
543147 736991
1027 2

output:

701913251
360490926
401476354
140864080
439953424
58245462
571144790
226876232
795156

result:

ok 9 numbers

Test #21:

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

input:

7
4220475 7200014
8946114 5607972
9217056 5889071
216495 8018
9263205 6175470
9381418 5709506
1305186 5438275

output:

416147761
504552270
531404017
597473622
227696082
526572165
932407358

result:

ok 7 numbers

Test #22:

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

input:

8
4735393 5463915
50137 654914
2203 4
2925479 5850958
5159591 8973433
7313341 4117661
5899 8
9318992 5940830

output:

830746100
701627501
861951012
734922707
586974926
54838635
54717862
962062441

result:

ok 8 numbers

Test #23:

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

input:

8
4808286 7533233
5427286 7627443
7049100 4420557
8918768 5668723
674651 626518
4540185 161190
1410422 8462532
7049224 4534562

output:

228187053
202526633
99774240
904698472
480292240
855142423
37675392
981266426

result:

ok 8 numbers

Test #24:

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

input:

10
33704480 11375262
63375993 63375993
12 13
58407009 98800752
10 7
381848 5348452
88342222 50792883
55352977 76210207
361275 3998109
58416656 94841469

output:

649109810
36477705
289153
752071377
5394
292760513
85444308
575684169
613771553
675962623

result:

ok 10 numbers

Test #25:

score: 0
Accepted
time: 1ms
memory: 3484kb

input:

7
34852528 49858286
62481622 49765513
75583113 25194371
3229939 804483
58329210 93597303
23508450 62689200
5 8

output:

391402413
137652286
768999263
472921885
836021626
100793612
702

result:

ok 7 numbers

Test #26:

score: 0
Accepted
time: 1ms
memory: 3608kb

input:

8
57310378 88790898
13 9147
1549494 24595
8452791 2087427
21621 172979
73536914 41674942
10163116 31757473
55581659 88587394

output:

41887077
681921806
26003660
68330985
630056979
227526752
694974420
566287628

result:

ok 8 numbers

Test #27:

score: 0
Accepted
time: 1ms
memory: 3616kb

input:

10
698700042 449659396
17 13
43899392 51200357
895050714 895050714
643725501 382794307
458560944 776109451
567286349 962689176
149158675 24522207
440485810 690959859
652728768 598334704

output:

703577031
2520080
194426181
120762097
456471628
53917573
344514670
705330170
462968000
697259102

result:

ok 10 numbers

Test #28:

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

input:

9
172917712 875395917
897335234 563015871
745518604 471032145
745253836 526187524
745397239 521218138
618365020 879980990
2609837 36375280
100674 7248541
405784272 696239702

output:

26528974
235341182
719622291
725483715
162432807
573688992
359069671
123532858
850989940

result:

ok 9 numbers

Test #29:

score: 0
Accepted
time: 1ms
memory: 3560kb

input:

8
19 16
919871160 979626570
652248464 796017040
513535282 890441475
587071118 935031010
566303476 566303476
853646878 518444900
54282 6459567

output:

31270230
469453648
140433663
633373156
490180087
832298148
445105898
409991300

result:

ok 8 numbers

Test #30:

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

input:

10
6316442971 3717676023
5707194159 9738462445
24455 10613497
4573236121 6647697858
4262400710 7167730777
270056596 1125156154
4585295484 4280782059
2130266613 431444507
7969708160 2789397856
6217859616 4582712819

output:

41234471
962654143
239500287
741697872
114104471
165586759
197355975
854304368
867087760
128591444

result:

ok 10 numbers

Test #31:

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

input:

8
4579668169 6390539165
1041646110 571219849
30 251
9 9
4671515135 7856932100
263421185 13851483
8361459884 4180729942
5455444332 8540064262

output:

555952223
137938692
573810687
10945
282167728
941458093
592851385
879877624

result:

ok 8 numbers

Test #32:

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

input:

7
4885592557 6722279544
6698742570 1014720768
5505975848 9085332219
9195021843 5693284264
52988491 378489
8801634660 9771021260
349665 18

output:

246109181
474709504
729257738
320828184
298549274
81287162
154217944

result:

ok 7 numbers

Test #33:

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

input:

9
64432609580 38353880909
43807625765 59263947003
16320158182 19429679437
3847856792 274728700
553640 37
8879682145 2927369495
70346968232 41700514029
78758894183 44807115997
99602254960 34562248040

output:

964237626
724959622
537133394
209972620
263976854
512058860
262701845
742703578
438174844

result:

ok 9 numbers

Test #34:

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

input:

10
40119253121 66288164640
87505712174 51288074660
36048827526 64066054154
84252980684 54190876423
446147 25876536
782712637 196016118
35016479961 14221711055
39589874533 64576938855
50165886770 84481820121
36605810431 60492661182

output:

774798817
342376143
939918052
623589627
125781574
937656495
18553261
784387301
537170792
279832098

result:

ok 10 numbers

Test #35:

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

input:

9
42 40
9305645972 60486698818
64950411971 41558316739
11050148 265460532
58383838184 29191919092
94333382274 34262678535
47483707407 80698978968
87395094281 56380378602
23984822092 35077779619

output:

454830513
458761936
767782510
956566290
722459276
110904025
991606480
829618195
279144874

result:

ok 9 numbers

Test #36:

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

input:

9
781818031945 475948866708
914806537124 601935899892
752123782181 525298689728
635268595617 375679155313
686238431621 478304282979
971027462869 605128611953
336341016872 593492888684
588233640889 936771497917
500339021121 718878308977

output:

416541065
93850085
32575703
927476140
275854305
524817775
115843013
612101788
535274867

result:

ok 9 numbers

Test #37:

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

input:

10
8 18
432360698142 700098703686
731142155950 372514886160
782624421493 445035963546
628123403278 373498610938
983205526516 627208237664
200940478371 558110905836
898239160130 449119580065
942666344678 542710523805
1376288731 33033576253

output:

118497
764002054
697392180
940670421
70833149
514323255
247311517
34986622
571700178
783375859

result:

ok 10 numbers

Test #38:

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

input:

9
463465274957 803405678339
38 39
855122660445 570081773630
565818450403 956900498105
513488249872 938556306902
666312248900 292118350180
311702183821 113258251057
220618909658 104522149028
1795060337 37699752631

output:

698978987
540539945
202456189
653488242
428733485
136622482
213065878
954077526
775245985

result:

ok 9 numbers

Test #39:

score: 0
Accepted
time: 31ms
memory: 3596kb

input:

10
664415605784 78244547126
9379552519928 5883628925869
5542594105687 7672977178418
5178938460625 8730427811713
9973455090260 5841455591693
56 16
734079467418 855862349973
81114853384 2385556446
149237977189 4033376322
5449911732823 8549857410677

output:

80131507
963784310
187103659
334889378
172535468
739514608
650459522
521260897
290739918
73066424

result:

ok 10 numbers

Test #40:

score: 0
Accepted
time: 52ms
memory: 3488kb

input:

9
4447910179998 7097252627075
11399089176 330557089593
4963101702009 6793726551400
9979205795063 2021104971152
8479740266012 4867793261721
5327068 72
9239905637409 3079968545803
5184273848100 8876120459083
4837533534365 8691234741624

output:

124690384
411971946
3773843
405089576
443529567
637322508
781006103
829103982
660929697

result:

ok 9 numbers

Test #41:

score: 0
Accepted
time: 22ms
memory: 3552kb

input:

10
981637513297 2128588091470
4714283499093 7576728679499
7495055678038 7495055678038
8324310665462 4634084762443
5710689724566 9667653254620
4305789682432 370028800834
4093285674016 7134248902838
4493096814116 6169635573974
78 10
55424515696 9293998475773

output:

506907545
175724684
171366228
746665106
993105044
966210024
527864656
70007987
160324237
127232425

result:

ok 10 numbers

Test #42:

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

input:

10
88004137971000 91959380127000
74147231981246 74147231981246
39434756902483 61919587653594
93 111
44181560163067 77615449759899
82796371085733 47668478750195
8801626941360 2517933769615
53330889214706 53330889214706
6872395393805 4602050084977
24278311806726 7716362667254

output:

977270307
951456599
826623512
2716956
757672346
493402036
241574837
936696309
272341593
90420684

result:

ok 10 numbers

Test #43:

score: 0
Accepted
time: 83ms
memory: 3780kb

input:

8
83242392419334 48080013444098
60557700619416 47712127760752
57820551505863 92086606077309
9092143767558 15196561910087
2705177411814 13610421069535
71283624982920 64494708317880
117 10271169
53977647239742 91676524000459

output:

1403378
328258696
841064592
703406177
536145853
91621790
519284514
219786799

result:

ok 8 numbers

Test #44:

score: 0
Accepted
time: 33ms
memory: 3480kb

input:

9
39389615240500 69398774281131
72905550036336 40503083353520
19984637490744 55133565358987
43317040541741 29703946351399
7584546229545 29580701409037
70706903233184 17676725808296
42273197935142 74296418759049
38126570359084 67695095849554
36653327088976 79435973089152

output:

830555900
16837817
240174960
41408696
333831437
851450450
303679113
864417616
66107106

result:

ok 9 numbers

Test #45:

score: 0
Accepted
time: 254ms
memory: 3528kb

input:

10
379809845776040 87521399244044
438651055673092 602633375926797
304488978161392 435733420086439
390988132449818 627934429378498
620167443903906 620167443903906
634916243215561 373673239266171
860296562918704 548434445628832
887229473296772 374750355147012
852714718210047 504067355022314
6138666444...

output:

494725545
57499987
396878960
479604519
257643714
648034103
914472685
883871305
266811385
888604046

result:

ok 10 numbers

Test #46:

score: 0
Accepted
time: 349ms
memory: 3612kb

input:

7
470178883855020 770276128511483
540990885807603 736886350162357
894729244714352 523324079814330
707170196942262 494394608815844
66701980590 180275623
103065153255233 206130306510466
364016287617199 653998959536254

output:

554745842
419154174
499608916
606331369
865725799
935915163
901511310

result:

ok 7 numbers

Test #47:

score: 0
Accepted
time: 123ms
memory: 3752kb

input:

7
381987309708152 675552067008456
456848180019281 711058812075785
668756377240308 468530841380440
90583278 101
531087374826792 590097083140880
99400300 151
739189959164630 522004964125469

output:

612853842
868424710
803022893
735904331
219013483
663905630
340785460

result:

ok 7 numbers

Test #48:

score: -100
Time Limit Exceeded

input:

9
7278964640577131 5036038524182689
5035531063135477 8496714647795219
3504743347919327 908440116241519
4001761136302463 7108707873098478
5149128773287081 6960878819273718
2091457546116447 2772408864757968
4089418491669467 6882229257269958
7486935806713669 4249189240699508
4096171687987639 6528182391...

output:

159753087
823145506
415988363
321650474
845685709
370868935
530244877
481080936
486435045

result: