QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#749420#9622. 有限小数wei177WA 0ms3588kbC++17960b2024-11-15 00:36:022024-11-15 00:36:04

Judging History

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

  • [2024-11-15 00:36:04]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3588kb
  • [2024-11-15 00:36:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a, ll b, ll& x, ll& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	ll d = exgcd(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
ll cal(ll a, ll b, ll c) {
	ll x, y;
	ll d = exgcd(a, b, x, y);
	if (c % d != 0) return 1e9;
	x *= c / d;
	return (x % (b / d) + (b / d)) % (b / d);
}
void slove() {
	ll a, b;
	cin >> a >> b;
	ll w = b;
	while (w % 2 == 0) w /= 2;
	while (w % 5 == 0) w /= 5;
	if (w == 1) {
		cout << "0 1\n";
		return;
	}
	ll ansc = 1e9, ansd = 1e9;
	for (ll t1 = 1; w * t1 <= 1e9; t1 *= 2) {
		for (ll t2 = 1; t1 * t2 * w <= 1e9; t2 *= 5) {
			ll d = t1 * t2 * w;
			ll c = cal(b, -w * w, a * d);
			if (c < ansc) {
				ansc = c;
				ansd = d;
			}
		}
	}
	cout << ansc << ' ' << ansd << '\n';
}
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while (t--) slove();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 2
2 3
3 7
19 79

output:

0 1
-2 15
-6 35
-78 1975

result:

wrong answer Integer -2 violates the range [0, 10^9]