QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#738176#9622. 有限小数ucup-team059#WA 1ms3568kbC++201.4kb2024-11-12 18:05:532024-11-12 18:05:54

Judging History

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

  • [2024-11-12 18:05:54]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3568kb
  • [2024-11-12 18:05:53]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long


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

void solve(){
	int a, b;

	std::cin >> a >> b;
	std::vector<ll> num;

	if (b == 5 || b == 2){
		std::cout << "0 1\n";
		return;
	}

	constexpr int MAX = 1e6;

	for (ll i = 1; i <= MAX; i *= 2){
		for (ll j = 1; i * j <= MAX; j *= 5){
			for (ll k = 1; i * k * j <= MAX; k *= b){
				num.push_back(i * k * j);
			}
		}
	}
	std::sort(num.begin(), num.end());
	std::pair<ll, ll> ans = {1e18, 1e18};
	for (auto d : num){
		// std::cout << d << '\n';
		ll num1 = a * d;
		if (num1 % b == 0){
			ll by = b;
			ll t = d;

			while (t % b == 0){
				by *= b;
				t /= b;
			}
			ll add = by;
			
			ll x, y;
			if (num1 % std::gcd(by, y)){
				continue;
			}

			ll d = exgcd(by, b, x, y);
			y = -y;
			while (x < 0 || y < 0){
				x += b / d;
				y += by / d;
			}
			while (x >= b / d && y >= by / d){
				x -= b / d, y -= by / d;
			}
			by = x * by;
			by -= num1;
			by /= b;
			if (ans > std::make_pair(by, d) && by >= 0){
				ans = std::make_pair(by, d);
			}
		}
	}

	std::cout << ans.first << ' ' << ans.second << '\n';

}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int t;

	std::cin >> t;

	while (t--){
		solve();
	}

	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3568kb

input:

4
1 2
2 3
3 7
19 79

output:

0 1
1 3
1 7
3 79

result:

wrong answer The result is not terminating.(Testcase 3)