QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#839760#7230. Fraction FactoryliyujiaWA 1ms9856kbC++171.4kb2025-01-02 09:00:412025-01-02 09:00:46

Judging History

This is the latest submission verdict.

  • [2025-01-02 09:00:46]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 9856kb
  • [2025-01-02 09:00:41]
  • Submitted

answer

#include <bits/stdc++.h>
#define int long long
#define _ (__int128)
using namespace std;
const int N = 500005;
int n, m, q, mod, c[N], d[N];
__int128 a[N], b[N];
int exgcd(int x, int y, int &A, int &B){
	if(!y) return A = 1, B = 0, x;
	int ans = exgcd(y, x % y, B, A);
	B -= x / y * A; return ans;
}
__int128 gcd(__int128 x, __int128 y){
	if(!y) return x;
	return gcd(y, x % y);
}
signed main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> c[i];
	for(int i = 1; i <= m; i++) cin >> d[i];
	for(int i = 1; i <= n; i += 2){
		a[(i + 1) / 2] = 1;
		for(int j = i; j <= min(n, i + 1); j++) a[(i + 1) / 2] *= c[j];
	}
	for(int i = 1; i <= m; i += 2){
		b[(i + 1) / 2] = 1;
		for(int j = i; j <= min(n, i + 1); j++) b[(i + 1) / 2] *= d[j];
	}
	n = (n + 1) / 2, m = (m + 1) / 2;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++){
		int t = gcd(a[i], b[j]);
		a[i] /= t, b[j] /= t;
	}
	cin >> q;
	while(q--){
		cin >> mod;
		int p1 = 1, p2 = 1;
		for(int i = 1; i <= n; i++) p1 = _(1) * p1 * (a[i] % mod) % mod;
		for(int i = 1; i <= m; i++) p2 = _(1) * p2 * (b[i] % mod) % mod;
		if(!p2 || __gcd(p2, mod) != 1) cout << "DIVISION BY ZERO\n";
		else{
			int A, B; exgcd(p2, mod, A, B);
			A = (A % mod + mod) % mod;
			cout << (int)(_(1) * p1 * A % mod) << '\n';
		}
	}
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 9856kb

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: -100
Wrong Answer
time: 1ms
memory: 9612kb

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
500594677218753385
73125855352413240
829278092196944930
5678851108281264
193921879033421090
218625601002834160
69517426990864030

result:

wrong answer 4th lines differ - expected: '5719954405760135', found: '500594677218753385'