QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#838754#7230. Fraction FactorycheryTL 2868ms3696kbC++144.3kb2024-12-31 20:59:352024-12-31 20:59:36

Judging History

This is the latest submission verdict.

  • [2024-12-31 20:59:36]
  • Judged
  • Verdict: TL
  • Time: 2868ms
  • Memory: 3696kb
  • [2024-12-31 20:59:35]
  • Submitted

answer

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define fir first.first
#define sec first.second
#define thi second
#define mp(x, y) make_pair((x), (y))
#define mt(x, y, z) mp(mp(x, y), z)
#define pp(x, y) push(mp(x, y))
#define pbp(x, y) pb(mp(x, y))
#define pbt(x, y, z) pb(mt(x, y, z))
#define re(i, n) for (int _a = (n), i = 0; i < _a; ++i)
#define ree(i, n) for (int _a = (n), i = 1; i <= _a; ++i)
#define rep(i, l, r) for (int _a = (r), i = l; i <= _a; ++i)
#define repd(i, r, l) for (int _a = (l), i = r; i >= _a; --i)
#define re0(i, x, ni) for (int i = x; i; i = ni)
#define fe(i, x) re0(i, pnt[x], nxt[i])
#define ress(i, S) for (int _S = (S), i = _S; i; i = _S & (i - 1))
#define clr(a, n, v) { re(_i, n) a[_i] = v; }
#define sqr(x) ((x) * (x))
#define inc(x, y) { x = (1LL * x + (y)); if ((x) >= P) x -= P; }
#define mul(x, y) { x = (1LL * x * (y)) % P; }
#define dec(x, y) { x = (x - (y) % P); if (x < 0) x += P; }

#define enc(x, y) ((x - 1) * m + (y))

template<typename T> inline T abs(T x) { return x < 0 ? -x : x; }
template<typename T> inline void ma(T &x, const T y){ if (x < y) x = y; }
template<typename T> inline void mi(T &x, const T y){ if (x > y) x = y; }
// template<typename T> inline T max(const T &x, const T &y){ return x < y ? y : x; }
// template<typename T> inline T min(const T &x, const T &y){ return x > y ? y : x; }


using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
typedef pair<ll, ll> PLL;
typedef pair<PLL, ll> PLLL;
// typedef __int128 lll;

// #define getchar() (SB==TB&&(TB=(SB=BB)+fread(BB,1,1<<15,stdin),SB==TB)?EOF:*SB++)
// char BB[1 << 16], *SB = BB, *TB = BB;

template<typename T> inline void read(T &x) {
    int s = 1, d = getchar(); x = 0;
    for ( ;!isdigit(d); d = getchar()) if (!(d^'-')) s = -1;
    while (isdigit(d)) x = x * 10 + (d&15), d = getchar();
    x *= s;
}
template<typename T, typename...L> inline void read(T &x, L &...l) { read(x), read(l...); }

inline int gi() { int x; read(x); return x; }

template<class T> void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10); putchar(x % 10 + '0');
}

int mex(const set<int> &s) {
    int l = -1;
    for (auto i : s) {
        if (i - l > 1) return l + 1;
        l = i;
    }
    return l + 1;
}

int P = 998244353;

template<typename T>
T qpow(T a, T b) {
	T r = 1;
	while (b) {
		if (b&1) r = 1LL * r * a % P;
		a = 1LL * a * a % P; b >>= 1;
	}
	return r;
}

const int T = 1e2 + 10;
const int N1e7 = 1e7 + 10;
const int N1e6 = 1e6 + 10;
const int N5e5 = 5e5 + 10;
const int N5e3 = 5e3 + 10;
const int N8e2 = 8e2 + 10;
const int N1e2 = 1e2 + 10;
const int N50 = 50 + 10;
const int M = 8e5 + 10;
const int K = 3e2 + 10;
const int LG = 20;
// const int S = 1 << N;
const ll inf = ~0ULL>>2;
const long long infll = ~0ULL>>1; 
const double eps = 1e-9;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

//random_device rd;
//mt19937 g(rd());
//int randi(int x) { return g() % x; }

const int N = N5e3;
const int Q = N * 2;

int n, m;
ll a[N], b[N];

ll gcd(ll a, ll b) {
    return b ? gcd(b, a % b) : a;
}

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

ll inv(ll a, ll p) {
    ll x, y;
    exgcd(a, p, x, y);
    return (x % p + p) % p;
}

void solve() {
	read(n, m);
    re(i, n) read(a[i]);
    re(i, m) {
        read(b[i]);
        re(j, n) {
            ll x = gcd(a[j], b[i]);
            a[j] /= x, b[i] /= x;
        }
    }
    int q = gi();
    while (q--) {
        ll mod; read(mod);
        ll r = 1, f = 0, den = 1; re(i, n) r = (__int128)r * a[i] % mod;
        re(i, m) {
            ll x = gcd(mod, b[i]);
            if (x == 1) {
                den = (__int128)den * b[i] % mod;
            } else {
                f = 1;
                break;
            }
        }
        if (f) cout << "DIVISION BY ZERO\n";
        else cout << ll((__int128)r * inv(den, mod) % mod) << '\n';
    }
}

int main() {
	// freopen("in", "r", stdin);
    int t = 1;
	// t = gi();
	while (t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
8 11 14
9 12
4
8
9
10
11

output:

4
DIVISION BY ZERO
4
0

result:

ok 4 lines

Test #2:

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

input:

2 2
1 2
4 3
10
5
7
11
13
17
19
23
29
31
37

output:

1
6
2
11
3
16
4
5
26
31

result:

ok 10 lines

Test #3:

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

input:

4 6
111111111111111111 1000000000000000000 1000000000000000 135792468013579246
10000000000000000 100000000000000 999999999999999999 123456789123456789 239239239932932932 1357924680
10
1161978962132362
539566136338457734
758923339452654441
655309120486827715
84306884606421160
911731869459297905
82243...

output:

DIVISION BY ZERO
DIVISION BY ZERO
DIVISION BY ZERO
5719954405760135
DIVISION BY ZERO
892796932685565430
DIVISION BY ZERO
DIVISION BY ZERO
DIVISION BY ZERO
DIVISION BY ZERO

result:

ok 10 lines

Test #4:

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

input:

8 5
1 1 1 1 1 1 1 1
1 1 1 1 1
33
519802560097260385
442596183335011282
688602833488446065
473642221531625139
336139490404077831
411067731885836516
348529798548197702
559996805776212339
324766630988403651
740258357792871391
670233519941996489
131286432198704669
193327986539006166
922036187121404691
2...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

ok 33 lines

Test #5:

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

input:

2 1
999999929 999999937
999999937
3
999999929
999999937
999999866000004473

output:

0
999999929
999999929

result:

ok 3 lines

Test #6:

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

input:

20 20
598463 808739 504181 886643 804161 162703 42409 66179 479599 14939 435103 1278181 484019 1177019 553171 763423 1098221 628939 863377 130349
291877 228847 1150139 1008017 1057853 312107 818371 226643 446309 629569 142223 517373 90989 415073 120749 1075619 1058041 893653 454919 25747
50
1299899
...

output:

602146
881975
266900
136515
266900
1071154
693499
244967
1060614
977384
26884
364814
850337
787165
919943
635052
482194
601195
841086
266212
754504
1079624
458235
34996
26072
865453
391778
406195
930981
78961
1104083
65599
602146
161608
949246
1267514
1071154
678271
220844
602955
244967
967494
22860...

result:

ok 50 lines

Test #7:

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

input:

500 500
387371 1004137 65647 352021 112069 456623 45863 104911 687151 522061 1042183 57991 428801 1110817 45263 961871 134443 376589 395377 161459 886381 1085753 674761 729041 444007 1218449 309937 594179 58049 10453 88117 434933 1177619 1086703 572087 675629 387791 640621 547453 594421 657313 12257...

output:

383179
888275
182734
185609
872863
59626
837128
391943
1194073
750514
634775
775567
1239102
407018
987868
695749
777370
215628
412673
272425
775567
1221783
1200408
775567
845048
827229
426950
838606
845048
626180
739737
383179
925291
998734
591167
870060
591167
845048
775567
964211
775567
592363
998...

result:

ok 50 lines

Test #8:

score: 0
Accepted
time: 1865ms
memory: 3660kb

input:

5000 5000
46237 985451 469367 284423 516653 397469 1143269 530183 1181183 330641 410353 101561 22067 686837 488339 1044697 711523 1042399 1092331 915697 1089047 1004527 1181923 1297421 865771 785207 833179 1046701 1138867 431077 1146709 1159127 419249 876041 486091 616799 301901 504001 889703 601189...

output:

815409
894989
6379
492271
185760
75492
1221630
1228920
878065
15324
78386
1219609
1238304
1241440
1010201
1203934
978181
944584
1010201
787803
244893
464400
583569
95906
45296
721934
518863
1286321
48086
798733
78386
271489
99969
221437
383353
1290376
908368
1078270
407153
815409
818442
177234
10650...

result:

ok 50 lines

Test #9:

score: 0
Accepted
time: 1799ms
memory: 3648kb

input:

4843 4989
282311 1224131 288947 1381 708733 832957 592507 193441 643073 625763 197293 951781 993841 1247581 908377 1164409 820429 1131769 544199 1089133 362951 1139407 504983 453157 668141 1191947 666697 1182767 17317 825107 1051319 275083 415687 166669 1244357 287863 891647 135463 1194581 782263 22...

output:

686206
64764
587541
854266
1047316
142248
1037628
919664
699909
889977
519197
830228
170110
451078
273750
691678
284952
830228
370585
380037
1278741
1022439
336430
258606
1047316
15133
298290
188086
273750
760625
911592
759463
862385
297412
144024
164772
442621
885396
1014203
316106
919664
171896
60...

result:

ok 50 lines

Test #10:

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

input:

1 1
999999999426987301
999999999446177371
50
999999999230595023
999999999459528149
999999999055719173
999999999347239897
999999999080279609
999999999817973431
999999999308902993
999999999390597539
999999999416910001
999999999447428923
999999999004310363
999999999974038457
999999999094972633
99999999...

output:

756632012721641517
457106544405250940
440266340208005663
324513075591986058
499343753384200875
985938662000008459
744506450645645896
739813282899850512
783165688841699050
3322275061445653
640237529836849823
14791088426240426
91526031664494402
122173425113395265
135014011813139239
272785455764605788
...

result:

ok 50 lines

Test #11:

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

input:

111 55
999999999437952739 999999999727121671 999999999952083791 999999999603858779 999999999035103917 999999999148643791 999999999192594061 999999999300412063 999999999128184707 999999999464910839 999999999460599923 999999999870821689 999999999980529061 999999999601005607 999999999211292561 99999999...

output:

708901826487331864
968740921462851986
528434535223783010
925628093565076668
526894146655099327
727396951083284217
45912440578684688
480612489151189098
504337517746168956
294993557461828069
560169763847527076
277064786474648892
321763466459407739
109945913705886746
988379719199747304
1569602508498478...

result:

ok 50 lines

Test #12:

score: 0
Accepted
time: 85ms
memory: 3536kb

input:

617 1172
999999999422370301 999999999279606739 999999999378912107 999999999364760257 999999999451911457 999999999938125993 999999999975745057 999999999065716031 999999999100397237 999999999530841719 999999999401480507 999999999238898551 999999999569670571 999999999560191109 999999999220028347 999999...

output:

824586476778382647
478890352822045637
686218461446538755
502090793279450771
394983069806446996
923532826225112601
864465032116294624
779905508741707265
903087500023092787
428436909658805039
239470801026877319
300887758894243769
349742234633818613
84753050449013963
56049586974938995
12135540187201907...

result:

ok 50 lines

Test #13:

score: 0
Accepted
time: 2505ms
memory: 3696kb

input:

4494 4920
999999999067434397 999999999269373331 999999999907980557 999999999530340857 999999999124595467 999999999168087251 999999999730737389 999999999331612633 999999999279031469 999999999328851233 999999999831783637 999999999543585427 999999999133465261 999999999667696567 999999999175495691 99999...

output:

198935648876742382
694325433912839534
351254060755570076
831073884976136269
407353710233817702
338238026247663782
306378523377933445
818714774356353280
906243522491418926
867399791448174488
707273994099362460
708101482059544384
87038106095552003
194318080401319673
298181879329306649
6552434725303427...

result:

ok 50 lines

Test #14:

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

input:

5000 5000
999999999280582667 999999999430144927 999999999621742387 999999999735098749 999999999864759527 999999999270579247 999999999495623849 999999999764228761 999999999168685303 999999999274868833 999999999420287267 999999999023317709 999999999050968213 999999999048439217 999999999596454203 99999...

output:

113776408893401412
749914750049534368
181644927923148832
799557251986405560
407976607821476607
412756092597615130
757821607726535946
311667021889869068
288950617997452359
770805464527200602
351892137846000340
607041955547440541
803186803439143161
236009152289366873
633055553846381543
738952398532960...

result:

ok 50 lines

Test #15:

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

input:

36 35
27 64 6 91 24 7 73 57 99 42 8 73 70 80 88 10 24 50 58 76 54 59 10 50 36 12 65 7 12 32 81 63 23 10 25 57
35 87 44 22 51 68 13 13 97 83 20 57 50 80 26 80 75 67 95 53 71 70 49 30 85 90 48 16 18 37 76 65 96 67 50
50
999999999427949167
999999999704949589
999999999557609507
999999999295222633
999999...

output:

747804889325795662
188003811165407649
217309273993744900
368232595702853574
827670646487309160
672020307154135282
84234857844696506
660680964009647475
943835241023926753
965492614085256119
95044668525121341
860981528114036180
746487653376257121
615433443159458709
380902367371537668
58203887179108566...

result:

ok 50 lines

Test #16:

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

input:

4999 4994
86 59 39 65 12 52 13 32 26 40 34 12 2 61 23 63 44 50 93 31 6 91 34 87 64 47 78 90 59 12 77 6 5 2 14 75 22 77 12 88 78 26 16 42 51 40 100 68 77 50 90 44 99 33 93 37 84 13 72 35 4 52 30 87 91 7 70 23 28 83 80 55 11 59 48 65 9 49 38 4 37 58 42 58 57 72 59 37 79 78 78 3 30 68 23 35 60 18 60 33...

output:

861723131063049679
95541078379334901
106164505334731654
807442073022110051
883631144466365780
848314277576703122
462061743142136614
989722242432740183
6108027258673396
867048465049627188
127155501785214902
355041522060440030
395852175430898337
432116982985081547
494379295142447492
807488088451816405...

result:

ok 50 lines

Test #17:

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

input:

4988 4989
854749883 585419399 715017419 876125792 75203803 27132450 908774208 22510083 474853935 381602083 965554714 601953629 805894096 755744947 40539219 198113370 861066872 132413051 620170177 376019746 423419898 164819970 355514133 289766712 150771516 885184197 375483998 759226209 708678504 6137...

output:

620768292406483252
311877342255813066
82974092796763066
725031014186609505
929091035947574843
547740946174668874
792916205350564043
175154443423041596
351079733598629215
726333941089938324
517551642521061732
466777105231939719
76578114564147936
728164196613538913
950881055317990914
96855420101207050...

result:

ok 50 lines

Test #18:

score: 0
Accepted
time: 315ms
memory: 3588kb

input:

5000 4987
3 5 1 3 3 6 3 3 10 8 9 6 4 3 2 6 1 1 2 6 9 6 4 8 4 2 1 8 10 4 2 3 1 10 3 5 5 1 3 8 4 5 4 1 1 2 3 3 9 6 4 10 2 7 1 6 7 5 3 1 9 8 8 10 6 10 8 1 3 1 6 1 7 8 3 5 3 8 5 1 1 9 7 3 10 2 6 3 10 3 1 7 7 1 8 9 10 2 2 5 5 2 2 2 1 4 3 4 5 6 2 10 7 5 7 3 1 4 10 5 1 5 8 3 10 9 7 7 4 7 10 2 8 10 8 5 4 7 ...

output:

351321620415086335
927428146834734452
218752774427774748
817223771722976704
234836800911611068
646492200549716885
35371599693950478
466808677308234919
289516333381936030
742853505424555792
635211796787056138
830927695525134788
607420811778435479
253665193657544724
820459723312652789
2605755226926721...

result:

ok 50 lines

Test #19:

score: 0
Accepted
time: 329ms
memory: 3696kb

input:

4987 5000
3 26 16 21 48 9 33 7 18 42 36 18 30 14 22 4 40 37 36 50 1 32 28 41 11 26 7 36 27 2 46 38 14 6 23 27 23 23 39 9 29 33 31 25 22 6 31 5 14 25 13 32 2 25 48 9 22 49 44 18 12 37 41 1 39 12 24 46 35 26 26 46 35 26 13 43 45 32 34 21 13 24 15 41 38 2 50 10 35 36 22 4 8 47 9 17 17 38 25 21 10 20 41...

output:

326719017200388000
219293875829586668
991568189324207521
496969090505119990
129534692242296342
12422144174968560
201015008531430796
412990577091052067
300625697261622531
979154323902162544
932660596736096230
562811740637350344
621642952691923722
209254816262692640
890833938407302227
4548445288984003...

result:

ok 50 lines

Test #20:

score: -100
Time Limit Exceeded

input:

4979 4993
145279705638272397 139412126440411827 910703813948079246 88995563250416747 98060984156598767 698835874365051137 783095494460896214 401987570065874386 665927783241087453 458658464282754627 519443898741165378 355409608503747011 816204269553866923 428027305585424178 17748471866477405 99691374...

output:


result: