QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#192854#7230. Fraction Factoryjeffqi#WA 0ms3596kbC++203.6kb2023-09-30 15:41:102023-09-30 15:41:11

Judging History

This is the latest submission verdict.

  • [2023-09-30 15:41:11]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3596kb
  • [2023-09-30 15:41:10]
  • Submitted

answer

#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((int)v.size())
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define mset multiset
#define ui unsigned int
#define ull unsigned ll
#define i128 __int128
using namespace std;

namespace qiqi {
	long long gcd(long long a, long long b) {
		if (b == 0) return a;
		return gcd(b, a % b);
	}

	long long quick_pow(long long x, long long p, long long mod) {  // 快速幂
		long long ans = 1;
		while (p) {
			if (p & 1) ans = (__int128)ans * x % mod;
			x = (__int128)x * x % mod;
			p >>= 1;
		}
		return ans;
	}

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

	long long inv(ll k,ll P) {
		ll x,y;
		exgcd(k,P,x,y);
		return x;
	}

	bool Miller_Rabin(long long p) {  // 判断素数
		if (p < 2) return 0;
		if (p == 2) return 1;
		if (p == 3) return 1;
		long long d = p - 1, r = 0;
		while (!(d & 1)) ++r, d >>= 1;  // 将d处理为奇数
		for (long long k = 0; k < 10; ++k) {
			long long a = rand() % (p - 2) + 2;
			long long x = quick_pow(a, d, p);
			if (x == 1 || x == p - 1) continue;
			for (int i = 0; i < r - 1; ++i) {
				x = (__int128)x * x % p;
				if (x == p - 1) break;
			}
			if (x != p - 1) return 0;
		}
		return 1;
	}

	long long Pollard_Rho(long long x) {
		long long s = 0, t = 0;
		long long c = (long long)rand() % (x - 1) + 1;
		int step = 0, goal = 1;
		long long val = 1;
		for (goal = 1;; goal *= 2, s = t, val = 1) {
			for (step = 1; step <= goal; ++step) {
				t = ((__int128)t * t + c) % x;
				val = (__int128)val * abs(t - s) % x;
				if ((step % 127) == 0) {
					long long d = gcd(val, x);
					if (d > 1) return d;
				}
			}
			long long d = gcd(val, x);
			if (d > 1) return d;
		}
	}

	void fac(long long x,vll &vec) {
		if (x < 2) {
			return;
		}
		if (Miller_Rabin(x)) {
			vec.eb(x);
			return;
		}
		long long p = x;
		while (p >= x) {
			p = Pollard_Rho(x);
		}
		while ((x % p) == 0) {
			x /= p;
		}
		fac(x,vec); fac(p,vec);
	}
	void main() {
		int n,m;
		cin >> n >> m;
		vll a(n),b(m);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < m; i++) {
			cin >> b[i];
		}
		int Q;
		cin >> Q;
		while (Q--) {
			ll P,ans = 1;
			cin >> P;
			vll vec;
			fac(P,vec);
			vi c(sz(vec));
			for (int i = 0; i < m; i++) {
				ll k = b[i];
				for (int j = 0; j < sz(vec); j++) {
					ll p = vec[j];
					while (1) {
						if (k%p) {
							break;
						}
						c[j]++; k /= p;
					}
				}
				ans = (i128)ans*k%P;
			}
			ans = inv(ans,P);
			for (int i = 0; i < n; i++) {
				ll k = a[i];
				for (int j = 0; j < sz(vec); j++) {
					ll p = vec[j];
					while (1) {
						if (k%p || !c[j]) {
							break;
						}
						c[j]--; k /= p;
					}
				}
				ans = (i128)ans*k%P;
			}
			bool flg = 1;
			for (auto k:c) {
				if (k > 0) {
					flg = 0;
					break;
				}
			}
			if (!flg) {
				cout << "DIVISION BY ZERO\n";
			}
			else {
				cout << (ll)ans << '\n';
			}
		}
	}
}

int main() {
//	clock_t st = clock();
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int T = 1;
//	cin >> T;
	while (T--) {
		qiqi::main();
	}

//	cout << (double)(clock()-st)/CLOCKS_PER_SEC;
	return 0;
}

详细

Test #1:

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

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

input:

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

output:

-4
6
2
-2
-14
16
4
-24
26
-6

result:

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